Skip to content

Commit

Permalink
remove the freeze API
Browse files Browse the repository at this point in the history
  • Loading branch information
jtattermusch committed Jul 30, 2015
1 parent 74810c6 commit 3783d9a
Show file tree
Hide file tree
Showing 17 changed files with 8 additions and 306 deletions.
1 change: 0 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ csharp_EXTRA_DIST= \
csharp/src/Google.Protobuf/Collections/RepeatedField.cs \
csharp/src/Google.Protobuf/FieldCodec.cs \
csharp/src/Google.Protobuf/FrameworkPortability.cs \
csharp/src/Google.Protobuf/Freezable.cs \
csharp/src/Google.Protobuf/Google.Protobuf.csproj \
csharp/src/Google.Protobuf/Google.Protobuf.nuspec \
csharp/src/Google.Protobuf/IMessage.cs \
Expand Down
56 changes: 0 additions & 56 deletions csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,53 +45,6 @@ namespace Google.Protobuf.Collections
/// </summary>
public class MapFieldTest
{
// Protobuf-specific tests
[Test]
public void Freeze_FreezesMessages()
{
var message = new ForeignMessage { C = 20 };
var map = new MapField<string, ForeignMessage> { { "x", message } };
map.Freeze();
Assert.IsTrue(message.IsFrozen);
}

[Test]
public void Freeze_Idempotent()
{
var message = new ForeignMessage { C = 20 };
var map = new MapField<string, ForeignMessage> { { "x", message } };
Assert.IsFalse(map.IsFrozen);
map.Freeze();
Assert.IsTrue(message.IsFrozen);
map.Freeze();
Assert.IsTrue(message.IsFrozen);
}

[Test]
public void Freeze_PreventsMutation()
{
var map = new MapField<string, string>();
map.Freeze();
Assert.IsTrue(map.IsFrozen);
Assert.IsTrue(map.IsReadOnly);
ICollection<KeyValuePair<string, string>> collection = map;
Assert.Throws<InvalidOperationException>(() => map["x"] = "y");
Assert.Throws<InvalidOperationException>(() => map.Add("x", "y"));
Assert.Throws<InvalidOperationException>(() => map.Remove("x"));
Assert.Throws<InvalidOperationException>(() => map.Clear());
Assert.Throws<InvalidOperationException>(() => collection.Add(NewKeyValuePair("x", "y")));
Assert.Throws<InvalidOperationException>(() => collection.Remove(NewKeyValuePair("x", "y")));
}

[Test]
public void Clone_ReturnsNonFrozen()
{
var map = new MapField<string, string>();
map.Freeze();
var clone = map.Clone();
clone.Add("x", "y");
}

[Test]
public void Clone_ClonesMessages()
{
Expand Down Expand Up @@ -422,10 +375,6 @@ public void IDictionary_Remove()
dictionary.Remove("x");
Assert.AreEqual(0, dictionary.Count);
Assert.Throws<ArgumentNullException>(() => dictionary.Remove(null));

map.Freeze();
// Call should fail even though it clearly doesn't contain 5 as a key.
Assert.Throws<InvalidOperationException>(() => dictionary.Remove(5));
}

[Test]
Expand All @@ -449,8 +398,6 @@ public void IDictionary_IsFixedSize()
var map = new MapField<string, string> { { "x", "y" } };
IDictionary dictionary = map;
Assert.IsFalse(dictionary.IsFixedSize);
map.Freeze();
Assert.IsTrue(dictionary.IsFixedSize);
}

[Test]
Expand Down Expand Up @@ -504,9 +451,6 @@ public void IDictionary_Indexer_Set()
Assert.Throws<InvalidCastException>(() => dictionary["x"] = 5);
Assert.Throws<ArgumentNullException>(() => dictionary[null] = "z");
Assert.Throws<ArgumentNullException>(() => dictionary["x"] = null);
map.Freeze();
// Note: Not InvalidOperationException.
Assert.Throws<NotSupportedException>(() => dictionary["a"] = "c");
}

[Test]
Expand Down
36 changes: 0 additions & 36 deletions csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,44 +193,10 @@ public void Indexer_Set()
Assert.Throws<ArgumentOutOfRangeException>(() => list[2] = "bad");
}

[Test]
public void Freeze_FreezesElements()
{
var list = new RepeatedField<TestAllTypes> { new TestAllTypes() };
Assert.IsFalse(list[0].IsFrozen);
list.Freeze();
Assert.IsTrue(list[0].IsFrozen);
}

[Test]
public void Freeze_PreventsMutations()
{
var list = new RepeatedField<int> { 0 };
list.Freeze();
Assert.Throws<InvalidOperationException>(() => list.Add(1));
Assert.Throws<InvalidOperationException>(() => list[0] = 1);
Assert.Throws<InvalidOperationException>(() => list.Clear());
Assert.Throws<InvalidOperationException>(() => list.RemoveAt(0));
Assert.Throws<InvalidOperationException>(() => list.Remove(0));
Assert.Throws<InvalidOperationException>(() => list.Insert(0, 0));
}

[Test]
public void Freeze_ReportsFrozen()
{
var list = new RepeatedField<int> { 0 };
Assert.IsFalse(list.IsFrozen);
Assert.IsFalse(list.IsReadOnly);
list.Freeze();
Assert.IsTrue(list.IsFrozen);
Assert.IsTrue(list.IsReadOnly);
}

[Test]
public void Clone_ReturnsMutable()
{
var list = new RepeatedField<int> { 0 };
list.Freeze();
var clone = list.Clone();
clone[0] = 1;
}
Expand Down Expand Up @@ -585,8 +551,6 @@ public void IList_IsFixedSize()
var field = new RepeatedField<string> { "first", "second" };
IList list = field;
Assert.IsFalse(list.IsFixedSize);
field.Freeze();
Assert.IsTrue(list.IsFixedSize);
}

[Test]
Expand Down
17 changes: 0 additions & 17 deletions csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,23 +503,6 @@ public void CloneOneofField()
Assert.AreNotEqual(original, clone);
}

[Test]
public void Freeze()
{
var frozen = new TestAllTypes();
frozen.Freeze();
Assert.IsTrue(frozen.IsFrozen);

Assert.Throws<InvalidOperationException>(() => frozen.ClearOneofField());
Assert.Throws<InvalidOperationException>(() => frozen.SingleInt32 = 0);
Assert.Throws<InvalidOperationException>(() => frozen.SingleNestedMessage = null);
Assert.Throws<InvalidOperationException>(() => frozen.SingleNestedEnum = 0);
Assert.Throws<InvalidOperationException>(() => frozen.OneofString = null);
Assert.Throws<InvalidOperationException>(() => frozen.OneofUint32 = 0U);
Assert.Throws<InvalidOperationException>(() => frozen.RepeatedDouble.Add(0.0));
Assert.Throws<InvalidOperationException>(() => frozen.RepeatedNestedMessage.Add(new TestAllTypes.Types.NestedMessage()));
}

[Test]
public void OneofProperties()
{
Expand Down
34 changes: 2 additions & 32 deletions csharp/src/Google.Protobuf/Collections/MapField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace Google.Protobuf.Collections
/// </remarks>
/// <typeparam name="TKey">Key type in the map. Must be a type supported by Protocol Buffer map keys.</typeparam>
/// <typeparam name="TValue">Value type in the map. Must be a type supported by Protocol Buffers.</typeparam>
public sealed class MapField<TKey, TValue> : IDeepCloneable<MapField<TKey, TValue>>, IFreezable, IDictionary<TKey, TValue>, IEquatable<MapField<TKey, TValue>>, IDictionary
public sealed class MapField<TKey, TValue> : IDeepCloneable<MapField<TKey, TValue>>, IDictionary<TKey, TValue>, IEquatable<MapField<TKey, TValue>>, IDictionary
{
// TODO: Don't create the map/list until we have an entry. (Assume many maps will be empty.)
private readonly bool allowNullValues;
Expand Down Expand Up @@ -119,7 +119,6 @@ public bool ContainsKey(TKey key)

public bool Remove(TKey key)
{
this.CheckMutable();
ThrowHelper.ThrowIfNull(key, "key");
LinkedListNode<KeyValuePair<TKey, TValue>> node;
if (map.TryGetValue(key, out node))
Expand Down Expand Up @@ -169,7 +168,6 @@ public TValue this[TKey key]
{
ThrowHelper.ThrowIfNull(value, "value");
}
this.CheckMutable();
LinkedListNode<KeyValuePair<TKey, TValue>> node;
var pair = new KeyValuePair<TKey, TValue>(key, value);
if (map.TryGetValue(key, out node))
Expand Down Expand Up @@ -214,7 +212,6 @@ void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item

public void Clear()
{
this.CheckMutable();
list.Clear();
map.Clear();
}
Expand All @@ -233,7 +230,6 @@ void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[]

bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
{
this.CheckMutable();
if (item.Key == null)
{
throw new ArgumentException("Key is null", "item");
Expand All @@ -260,31 +256,6 @@ bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> i
public int Count { get { return list.Count; } }
public bool IsReadOnly { get { return frozen; } }

public void Freeze()
{
if (IsFrozen)
{
return;
}
frozen = true;
// Only values can be frozen, as all the key types are simple.
// Everything can be done in-place, as we're just freezing objects.
if (typeof(IFreezable).IsAssignableFrom(typeof(TValue)))
{
for (var node = list.First; node != null; node = node.Next)
{
var pair = node.Value;
IFreezable freezableValue = pair.Value as IFreezable;
if (freezableValue != null)
{
freezableValue.Freeze();
}
}
}
}

public bool IsFrozen { get { return frozen; } }

public override bool Equals(object other)
{
return Equals(other as MapField<TKey, TValue>);
Expand Down Expand Up @@ -405,7 +376,6 @@ IDictionaryEnumerator IDictionary.GetEnumerator()
void IDictionary.Remove(object key)
{
ThrowHelper.ThrowIfNull(key, "key");
this.CheckMutable();
if (!(key is TKey))
{
return;
Expand All @@ -420,7 +390,7 @@ void ICollection.CopyTo(Array array, int index)
temp.CopyTo(array, index);
}

bool IDictionary.IsFixedSize { get { return IsFrozen; } }
bool IDictionary.IsFixedSize { get { return false; } }

ICollection IDictionary.Keys { get { return (ICollection)Keys; } }

Expand Down
29 changes: 3 additions & 26 deletions csharp/src/Google.Protobuf/Collections/RepeatedField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace Google.Protobuf.Collections
/// restrictions (no null values) and capabilities (deep cloning and freezing).
/// </summary>
/// <typeparam name="T">The element type of the repeated field.</typeparam>
public sealed class RepeatedField<T> : IList<T>, IList, IDeepCloneable<RepeatedField<T>>, IEquatable<RepeatedField<T>>, IFreezable
public sealed class RepeatedField<T> : IList<T>, IList, IDeepCloneable<RepeatedField<T>>, IEquatable<RepeatedField<T>>
{
private static readonly T[] EmptyArray = new T[0];
private const int MinArraySize = 8;
Expand Down Expand Up @@ -190,21 +190,6 @@ public void WriteTo(CodedOutputStream output, FieldCodec<T> codec)
}
}

public bool IsFrozen { get { return frozen; } }

public void Freeze()
{
frozen = true;
IFreezable[] freezableArray = array as IFreezable[];
if (freezableArray != null)
{
for (int i = 0; i < count; i++)
{
freezableArray[i].Freeze();
}
}
}

private void EnsureSize(int size)
{
if (array.Length < size)
Expand All @@ -223,14 +208,12 @@ public void Add(T item)
{
throw new ArgumentNullException("item");
}
this.CheckMutable();
EnsureSize(count + 1);
array[count++] = item;
}

public void Clear()
{
this.CheckMutable();
array = EmptyArray;
count = 0;
}
Expand All @@ -247,7 +230,6 @@ public void CopyTo(T[] array, int arrayIndex)

public bool Remove(T item)
{
this.CheckMutable();
int index = IndexOf(item);
if (index == -1)
{
Expand All @@ -261,15 +243,14 @@ public bool Remove(T item)

public int Count { get { return count; } }

public bool IsReadOnly { get { return IsFrozen; } }
public bool IsReadOnly { get { return false; } }

public void Add(RepeatedField<T> values)
{
if (values == null)
{
throw new ArgumentNullException("values");
}
this.CheckMutable();
EnsureSize(count + values.count);
// We know that all the values will be valid, because it's a RepeatedField.
Array.Copy(values.array, 0, array, count, values.count);
Expand All @@ -282,7 +263,6 @@ public void Add(IEnumerable<T> values)
{
throw new ArgumentNullException("values");
}
this.CheckMutable();
// TODO: Check for ICollection and get the Count?
foreach (T item in values)
{
Expand Down Expand Up @@ -372,7 +352,6 @@ public void Insert(int index, T item)
{
throw new ArgumentOutOfRangeException("index");
}
this.CheckMutable();
EnsureSize(count + 1);
Array.Copy(array, index, array, index + 1, count - index);
array[index] = item;
Expand All @@ -385,7 +364,6 @@ public void RemoveAt(int index)
{
throw new ArgumentOutOfRangeException("index");
}
this.CheckMutable();
Array.Copy(array, index + 1, array, index, count - index - 1);
count--;
array[count] = default(T);
Expand All @@ -407,7 +385,6 @@ public T this[int index]
{
throw new ArgumentOutOfRangeException("index");
}
this.CheckMutable();
if (value == null)
{
throw new ArgumentNullException("value");
Expand All @@ -417,7 +394,7 @@ public T this[int index]
}

#region Explicit interface implementation for IList and ICollection.
bool IList.IsFixedSize { get { return IsFrozen; } }
bool IList.IsFixedSize { get { return false; } }

void ICollection.CopyTo(Array array, int index)
{
Expand Down
Loading

0 comments on commit 3783d9a

Please sign in to comment.