aboutsummaryrefslogtreecommitdiffhomepage
path: root/csharp/src/ProtocolBuffers/Collections
diff options
context:
space:
mode:
Diffstat (limited to 'csharp/src/ProtocolBuffers/Collections')
-rw-r--r--csharp/src/ProtocolBuffers/Collections/Dictionaries.cs122
-rw-r--r--csharp/src/ProtocolBuffers/Collections/Enumerables.cs74
-rw-r--r--csharp/src/ProtocolBuffers/Collections/IPopsicleList.cs58
-rw-r--r--csharp/src/ProtocolBuffers/Collections/Lists.cs110
-rw-r--r--csharp/src/ProtocolBuffers/Collections/PopsicleList.cs208
-rw-r--r--csharp/src/ProtocolBuffers/Collections/ReadOnlyDictionary.cs146
6 files changed, 718 insertions, 0 deletions
diff --git a/csharp/src/ProtocolBuffers/Collections/Dictionaries.cs b/csharp/src/ProtocolBuffers/Collections/Dictionaries.cs
new file mode 100644
index 00000000..1f3791b1
--- /dev/null
+++ b/csharp/src/ProtocolBuffers/Collections/Dictionaries.cs
@@ -0,0 +1,122 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://github.com/jskeet/dotnet-protobufs/
+// Original C++/Java/Python code:
+// http://code.google.com/p/protobuf/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+using System;
+using System.Collections;
+using System.Collections.Generic;
+
+namespace Google.ProtocolBuffers.Collections
+{
+ /// <summary>
+ /// Utility class for dictionaries.
+ /// </summary>
+ public static class Dictionaries
+ {
+ /// <summary>
+ /// Compares two dictionaries for equality. Each value is compared with equality using Equals
+ /// for non-IEnumerable implementations, and using EnumerableEquals otherwise.
+ /// TODO(jonskeet): This is clearly pretty slow, and involves lots of boxing/unboxing...
+ /// </summary>
+ public static bool Equals<TKey, TValue>(IDictionary<TKey, TValue> left, IDictionary<TKey, TValue> right)
+ {
+ if (left.Count != right.Count)
+ {
+ return false;
+ }
+ foreach (KeyValuePair<TKey, TValue> leftEntry in left)
+ {
+ TValue rightValue;
+ if (!right.TryGetValue(leftEntry.Key, out rightValue))
+ {
+ return false;
+ }
+
+ IEnumerable leftEnumerable = leftEntry.Value as IEnumerable;
+ IEnumerable rightEnumerable = rightValue as IEnumerable;
+ if (leftEnumerable == null || rightEnumerable == null)
+ {
+ if (!Equals(leftEntry.Value, rightValue))
+ {
+ return false;
+ }
+ }
+ else
+ {
+ if (!Enumerables.Equals(leftEnumerable, rightEnumerable))
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ public static IDictionary<TKey, TValue> AsReadOnly<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
+ {
+ return dictionary.IsReadOnly ? dictionary : new ReadOnlyDictionary<TKey, TValue>(dictionary);
+ }
+
+ /// <summary>
+ /// Creates a hashcode for a dictionary by XORing the hashcodes of all the fields
+ /// and values. (By XORing, we avoid ordering issues.)
+ /// TODO(jonskeet): Currently XORs other stuff too, and assumes non-null values.
+ /// </summary>
+ public static int GetHashCode<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
+ {
+ int ret = 31;
+ foreach (KeyValuePair<TKey, TValue> entry in dictionary)
+ {
+ int hash = entry.Key.GetHashCode() ^ GetDeepHashCode(entry.Value);
+ ret ^= hash;
+ }
+ return ret;
+ }
+
+ /// <summary>
+ /// Determines the hash of a value by either taking it directly or hashing all the elements
+ /// for IEnumerable implementations.
+ /// </summary>
+ private static int GetDeepHashCode(object value)
+ {
+ IEnumerable iterable = value as IEnumerable;
+ if (iterable == null)
+ {
+ return value.GetHashCode();
+ }
+ int hash = 29;
+ foreach (object element in iterable)
+ {
+ hash = hash*37 + element.GetHashCode();
+ }
+ return hash;
+ }
+ }
+} \ No newline at end of file
diff --git a/csharp/src/ProtocolBuffers/Collections/Enumerables.cs b/csharp/src/ProtocolBuffers/Collections/Enumerables.cs
new file mode 100644
index 00000000..7ad9a832
--- /dev/null
+++ b/csharp/src/ProtocolBuffers/Collections/Enumerables.cs
@@ -0,0 +1,74 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://github.com/jskeet/dotnet-protobufs/
+// Original C++/Java/Python code:
+// http://code.google.com/p/protobuf/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+using System;
+using System.Collections;
+
+namespace Google.ProtocolBuffers.Collections
+{
+ /// <summary>
+ /// Utility class for IEnumerable (and potentially the generic version in the future).
+ /// </summary>
+ public static class Enumerables
+ {
+ public static bool Equals(IEnumerable left, IEnumerable right)
+ {
+ IEnumerator leftEnumerator = left.GetEnumerator();
+ try
+ {
+ foreach (object rightObject in right)
+ {
+ if (!leftEnumerator.MoveNext())
+ {
+ return false;
+ }
+ if (!Equals(leftEnumerator.Current, rightObject))
+ {
+ return false;
+ }
+ }
+ if (leftEnumerator.MoveNext())
+ {
+ return false;
+ }
+ }
+ finally
+ {
+ IDisposable leftEnumeratorDisposable = leftEnumerator as IDisposable;
+ if (leftEnumeratorDisposable != null)
+ {
+ leftEnumeratorDisposable.Dispose();
+ }
+ }
+ return true;
+ }
+ }
+} \ No newline at end of file
diff --git a/csharp/src/ProtocolBuffers/Collections/IPopsicleList.cs b/csharp/src/ProtocolBuffers/Collections/IPopsicleList.cs
new file mode 100644
index 00000000..a1a75815
--- /dev/null
+++ b/csharp/src/ProtocolBuffers/Collections/IPopsicleList.cs
@@ -0,0 +1,58 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://github.com/jskeet/dotnet-protobufs/
+// Original C++/Java/Python code:
+// http://code.google.com/p/protobuf/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+using System.Collections.Generic;
+
+namespace Google.ProtocolBuffers.Collections
+{
+ /// <summary>
+ /// A list which has an Add method which accepts an IEnumerable[T].
+ /// This allows whole collections to be added easily using collection initializers.
+ /// It causes a potential overload confusion if T : IEnumerable[T], but in
+ /// practice that won't happen in protocol buffers.
+ /// </summary>
+ /// <remarks>This is only currently implemented by PopsicleList, and it's likely
+ /// to stay that way - hence the name. More genuinely descriptive names are
+ /// horribly ugly. (At least, the ones the author could think of...)</remarks>
+ /// <typeparam name="T">The element type of the list</typeparam>
+ public interface IPopsicleList<T> : IList<T>
+ {
+ void Add(IEnumerable<T> collection);
+ }
+
+ /// <summary>
+ /// Used to efficiently cast the elements of enumerations
+ /// </summary>
+ internal interface ICastArray
+ {
+ IEnumerable<TItemType> CastArray<TItemType>();
+ }
+} \ No newline at end of file
diff --git a/csharp/src/ProtocolBuffers/Collections/Lists.cs b/csharp/src/ProtocolBuffers/Collections/Lists.cs
new file mode 100644
index 00000000..a24e1d9b
--- /dev/null
+++ b/csharp/src/ProtocolBuffers/Collections/Lists.cs
@@ -0,0 +1,110 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://github.com/jskeet/dotnet-protobufs/
+// Original C++/Java/Python code:
+// http://code.google.com/p/protobuf/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+
+namespace Google.ProtocolBuffers.Collections
+{
+ /// <summary>
+ /// Utility non-generic class for calling into Lists{T} using type inference.
+ /// </summary>
+ public static class Lists
+ {
+ /// <summary>
+ /// Returns a read-only view of the specified list.
+ /// </summary>
+ public static IList<T> AsReadOnly<T>(IList<T> list)
+ {
+ return Lists<T>.AsReadOnly(list);
+ }
+
+ public static bool Equals<T>(IList<T> left, IList<T> right)
+ {
+ if (left == right)
+ {
+ return true;
+ }
+ if (left == null || right == null)
+ {
+ return false;
+ }
+ if (left.Count != right.Count)
+ {
+ return false;
+ }
+ IEqualityComparer<T> comparer = EqualityComparer<T>.Default;
+ for (int i = 0; i < left.Count; i++)
+ {
+ if (!comparer.Equals(left[i], right[i]))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public static int GetHashCode<T>(IList<T> list)
+ {
+ int hash = 31;
+ foreach (T element in list)
+ {
+ hash = hash*29 + element.GetHashCode();
+ }
+ return hash;
+ }
+ }
+
+ /// <summary>
+ /// Utility class for dealing with lists.
+ /// </summary>
+ public static class Lists<T>
+ {
+ private static readonly ReadOnlyCollection<T> empty = new ReadOnlyCollection<T>(new T[0]);
+
+ /// <summary>
+ /// Returns an immutable empty list.
+ /// </summary>
+ public static ReadOnlyCollection<T> Empty
+ {
+ get { return empty; }
+ }
+
+ /// <summary>
+ /// Returns either the original reference if it's already read-only,
+ /// or a new ReadOnlyCollection wrapping the original list.
+ /// </summary>
+ public static IList<T> AsReadOnly(IList<T> list)
+ {
+ return list.IsReadOnly ? list : new ReadOnlyCollection<T>(list);
+ }
+ }
+} \ No newline at end of file
diff --git a/csharp/src/ProtocolBuffers/Collections/PopsicleList.cs b/csharp/src/ProtocolBuffers/Collections/PopsicleList.cs
new file mode 100644
index 00000000..3de97f89
--- /dev/null
+++ b/csharp/src/ProtocolBuffers/Collections/PopsicleList.cs
@@ -0,0 +1,208 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://github.com/jskeet/dotnet-protobufs/
+// Original C++/Java/Python code:
+// http://code.google.com/p/protobuf/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+using System;
+using System.Collections;
+using System.Collections.Generic;
+
+namespace Google.ProtocolBuffers.Collections
+{
+ /// <summary>
+ /// Proxies calls to a <see cref="List{T}" />, but allows the list
+ /// to be made read-only (with the <see cref="MakeReadOnly" /> method),
+ /// after which any modifying methods throw <see cref="NotSupportedException" />.
+ /// </summary>
+ public sealed class PopsicleList<T> : IPopsicleList<T>, ICastArray
+ {
+ private static readonly bool CheckForNull = default(T) == null;
+ private static readonly T[] EmptySet = new T[0];
+
+ private List<T> items;
+ private bool readOnly;
+
+ /// <summary>
+ /// Makes this list read-only ("freezes the popsicle"). From this
+ /// point on, mutating methods (Clear, Add etc) will throw a
+ /// NotSupportedException. There is no way of "defrosting" the list afterwards.
+ /// </summary>
+ public void MakeReadOnly()
+ {
+ readOnly = true;
+ }
+
+ public int IndexOf(T item)
+ {
+ return items == null ? -1 : items.IndexOf(item);
+ }
+
+ public void Insert(int index, T item)
+ {
+ ValidateModification();
+ if (CheckForNull)
+ {
+ ThrowHelper.ThrowIfNull(item);
+ }
+ items.Insert(index, item);
+ }
+
+ public void RemoveAt(int index)
+ {
+ ValidateModification();
+ items.RemoveAt(index);
+ }
+
+ public T this[int index]
+ {
+ get
+ {
+ if (items == null)
+ {
+ throw new ArgumentOutOfRangeException();
+ }
+ return items[index];
+ }
+ set
+ {
+ ValidateModification();
+ if (CheckForNull)
+ {
+ ThrowHelper.ThrowIfNull(value);
+ }
+ items[index] = value;
+ }
+ }
+
+ public void Add(T item)
+ {
+ ValidateModification();
+ if (CheckForNull)
+ {
+ ThrowHelper.ThrowIfNull(item);
+ }
+ items.Add(item);
+ }
+
+ public void Clear()
+ {
+ ValidateModification();
+ items.Clear();
+ }
+
+ public bool Contains(T item)
+ {
+ return items == null ? false : items.Contains(item);
+ }
+
+ public void CopyTo(T[] array, int arrayIndex)
+ {
+ if (items != null)
+ {
+ items.CopyTo(array, arrayIndex);
+ }
+ }
+
+ public int Count
+ {
+ get { return items == null ? 0 : items.Count; }
+ }
+
+ public bool IsReadOnly
+ {
+ get { return readOnly; }
+ }
+
+ public bool Remove(T item)
+ {
+ ValidateModification();
+ return items.Remove(item);
+ }
+
+ public IEnumerator<T> GetEnumerator()
+ {
+ IEnumerable<T> tenum = (IEnumerable<T>)items ?? EmptySet;
+ return tenum.GetEnumerator();
+ }
+
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return GetEnumerator();
+ }
+
+ public void Add(IEnumerable<T> collection)
+ {
+ ValidateModification();
+ ThrowHelper.ThrowIfNull(collection);
+
+ if (!CheckForNull || collection is PopsicleList<T>)
+ {
+ items.AddRange(collection);
+ }
+ else
+ {
+ // Assumption, it's ok to enumerate collections more than once.
+ if (collection is ICollection<T>)
+ {
+ ThrowHelper.ThrowIfAnyNull(collection);
+ items.AddRange(collection);
+ }
+ else
+ {
+ foreach (T item in collection)
+ {
+ ThrowHelper.ThrowIfNull(item);
+ items.Add(item);
+ }
+ }
+ }
+ }
+
+ private void ValidateModification()
+ {
+ if (readOnly)
+ {
+ throw new NotSupportedException("List is read-only");
+ }
+ if (items == null)
+ {
+ items = new List<T>();
+ }
+ }
+
+ IEnumerable<TItemType> ICastArray.CastArray<TItemType>()
+ {
+ if (items == null)
+ {
+ return PopsicleList<TItemType>.EmptySet;
+ }
+ return (TItemType[]) (object) items.ToArray();
+ }
+ }
+} \ No newline at end of file
diff --git a/csharp/src/ProtocolBuffers/Collections/ReadOnlyDictionary.cs b/csharp/src/ProtocolBuffers/Collections/ReadOnlyDictionary.cs
new file mode 100644
index 00000000..b0bc55ff
--- /dev/null
+++ b/csharp/src/ProtocolBuffers/Collections/ReadOnlyDictionary.cs
@@ -0,0 +1,146 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://github.com/jskeet/dotnet-protobufs/
+// Original C++/Java/Python code:
+// http://code.google.com/p/protobuf/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+using System;
+using System.Collections;
+using System.Collections.Generic;
+
+namespace Google.ProtocolBuffers.Collections
+{
+ /// <summary>
+ /// Read-only wrapper around another dictionary.
+ /// </summary>
+ public sealed class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
+ {
+ private readonly IDictionary<TKey, TValue> wrapped;
+
+ public ReadOnlyDictionary(IDictionary<TKey, TValue> wrapped)
+ {
+ this.wrapped = wrapped;
+ }
+
+ public void Add(TKey key, TValue value)
+ {
+ throw new InvalidOperationException();
+ }
+
+ public bool ContainsKey(TKey key)
+ {
+ return wrapped.ContainsKey(key);
+ }
+
+ public ICollection<TKey> Keys
+ {
+ get { return wrapped.Keys; }
+ }
+
+ public bool Remove(TKey key)
+ {
+ throw new InvalidOperationException();
+ }
+
+ public bool TryGetValue(TKey key, out TValue value)
+ {
+ return wrapped.TryGetValue(key, out value);
+ }
+
+ public ICollection<TValue> Values
+ {
+ get { return wrapped.Values; }
+ }
+
+ public TValue this[TKey key]
+ {
+ get { return wrapped[key]; }
+ set { throw new InvalidOperationException(); }
+ }
+
+ public void Add(KeyValuePair<TKey, TValue> item)
+ {
+ throw new InvalidOperationException();
+ }
+
+ public void Clear()
+ {
+ throw new InvalidOperationException();
+ }
+
+ public bool Contains(KeyValuePair<TKey, TValue> item)
+ {
+ return wrapped.Contains(item);
+ }
+
+ public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
+ {
+ wrapped.CopyTo(array, arrayIndex);
+ }
+
+ public int Count
+ {
+ get { return wrapped.Count; }
+ }
+
+ public bool IsReadOnly
+ {
+ get { return true; }
+ }
+
+ public bool Remove(KeyValuePair<TKey, TValue> item)
+ {
+ throw new InvalidOperationException();
+ }
+
+ public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
+ {
+ return wrapped.GetEnumerator();
+ }
+
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return ((IEnumerable) wrapped).GetEnumerator();
+ }
+
+ public override bool Equals(object obj)
+ {
+ return wrapped.Equals(obj);
+ }
+
+ public override int GetHashCode()
+ {
+ return wrapped.GetHashCode();
+ }
+
+ public override string ToString()
+ {
+ return wrapped.ToString();
+ }
+ }
+} \ No newline at end of file