aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/ProtocolBuffers/Collections/MapField.cs
diff options
context:
space:
mode:
authorJon Skeet <jonskeet@google.com>2015-07-03 11:41:37 +0100
committerJon Skeet <jonskeet@google.com>2015-07-09 08:26:07 +0100
commit14f2222a50a18ff0d8772095587b9cdad455a7bb (patch)
tree1422e99fad875a04921a0b8c881f48c63937f84c /csharp/src/ProtocolBuffers/Collections/MapField.cs
parentaf259b77bf04fcfb68609776cb27f04d289a2c39 (diff)
downloadprotobuf-14f2222a50a18ff0d8772095587b9cdad455a7bb.tar.gz
protobuf-14f2222a50a18ff0d8772095587b9cdad455a7bb.tar.bz2
protobuf-14f2222a50a18ff0d8772095587b9cdad455a7bb.zip
Lots more tests for FieldCodec, MapField, RepeatedField
... and some implementation changes to go with them.
Diffstat (limited to 'csharp/src/ProtocolBuffers/Collections/MapField.cs')
-rw-r--r--csharp/src/ProtocolBuffers/Collections/MapField.cs45
1 files changed, 41 insertions, 4 deletions
diff --git a/csharp/src/ProtocolBuffers/Collections/MapField.cs b/csharp/src/ProtocolBuffers/Collections/MapField.cs
index 779ff061..0f379eaa 100644
--- a/csharp/src/ProtocolBuffers/Collections/MapField.cs
+++ b/csharp/src/ProtocolBuffers/Collections/MapField.cs
@@ -367,11 +367,13 @@ namespace Google.Protobuf.Collections
IDictionaryEnumerator IDictionary.GetEnumerator()
{
- throw new NotImplementedException();
+ return new DictionaryEnumerator(GetEnumerator());
}
void IDictionary.Remove(object key)
{
+ ThrowHelper.ThrowIfNull(key, "key");
+ this.CheckMutable();
if (!(key is TKey))
{
return;
@@ -381,7 +383,9 @@ namespace Google.Protobuf.Collections
void ICollection.CopyTo(Array array, int index)
{
- throw new NotImplementedException();
+ // This is ugly and slow as heck, but with any luck it will never be used anyway.
+ ICollection temp = this.Select(pair => new DictionaryEntry(pair.Key, pair.Value)).ToList();
+ temp.CopyTo(array, index);
}
bool IDictionary.IsFixedSize { get { return IsFrozen; } }
@@ -392,12 +396,13 @@ namespace Google.Protobuf.Collections
bool ICollection.IsSynchronized { get { return false; } }
- object ICollection.SyncRoot { get { return null; } }
+ object ICollection.SyncRoot { get { return this; } }
object IDictionary.this[object key]
{
get
{
+ ThrowHelper.ThrowIfNull(key, "key");
if (!(key is TKey))
{
return null;
@@ -407,10 +412,42 @@ namespace Google.Protobuf.Collections
return value;
}
- set { this[(TKey)key] = (TValue)value; }
+ set
+ {
+ if (frozen)
+ {
+ throw new NotSupportedException("Dictionary is frozen");
+ }
+ this[(TKey)key] = (TValue)value;
+ }
}
#endregion
+ private class DictionaryEnumerator : IDictionaryEnumerator
+ {
+ private readonly IEnumerator<KeyValuePair<TKey, TValue>> enumerator;
+
+ internal DictionaryEnumerator(IEnumerator<KeyValuePair<TKey, TValue>> enumerator)
+ {
+ this.enumerator = enumerator;
+ }
+
+ public bool MoveNext()
+ {
+ return enumerator.MoveNext();
+ }
+
+ public void Reset()
+ {
+ enumerator.Reset();
+ }
+
+ public object Current { get { return Entry; } }
+ public DictionaryEntry Entry { get { return new DictionaryEntry(Key, Value); } }
+ public object Key { get { return enumerator.Current.Key; } }
+ public object Value { get { return enumerator.Current.Value; } }
+ }
+
/// <summary>
/// A codec for a specific map field. This contains all the information required to encoded and
/// decode the nested messages.