summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/mscorlib/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs')
-rw-r--r--src/mscorlib/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs371
1 files changed, 245 insertions, 126 deletions
diff --git a/src/mscorlib/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs b/src/mscorlib/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs
index 5c9e8c4..ebf86cd 100644
--- a/src/mscorlib/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs
+++ b/src/mscorlib/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs
@@ -11,18 +11,18 @@
**
===========================================================*/
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Diagnostics.Contracts;
+
namespace System.Collections.ObjectModel
{
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Diagnostics.Contracts;
-
[Serializable]
[DebuggerTypeProxy(typeof(Mscorlib_DictionaryDebugView<,>))]
[DebuggerDisplay("Count = {Count}")]
- public class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary, IReadOnlyDictionary<TKey, TValue>
+ internal class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary, IReadOnlyDictionary<TKey, TValue>
{
private readonly IDictionary<TKey, TValue> m_dictionary;
[NonSerialized]
@@ -32,32 +32,41 @@ namespace System.Collections.ObjectModel
[NonSerialized]
private ValueCollection m_values;
- public ReadOnlyDictionary(IDictionary<TKey, TValue> dictionary) {
- if (dictionary == null) {
+ public ReadOnlyDictionary(IDictionary<TKey, TValue> dictionary)
+ {
+ if (dictionary == null)
+ {
throw new ArgumentNullException(nameof(dictionary));
}
Contract.EndContractBlock();
m_dictionary = dictionary;
}
- protected IDictionary<TKey, TValue> Dictionary {
+ protected IDictionary<TKey, TValue> Dictionary
+ {
get { return m_dictionary; }
}
- public KeyCollection Keys {
- get {
+ public KeyCollection Keys
+ {
+ get
+ {
Contract.Ensures(Contract.Result<KeyCollection>() != null);
- if (m_keys == null) {
+ if (m_keys == null)
+ {
m_keys = new KeyCollection(m_dictionary.Keys);
}
return m_keys;
}
}
- public ValueCollection Values {
- get {
+ public ValueCollection Values
+ {
+ get
+ {
Contract.Ensures(Contract.Result<ValueCollection>() != null);
- if (m_values == null) {
+ if (m_values == null)
+ {
m_values = new ValueCollection(m_dictionary.Values);
}
return m_values;
@@ -66,46 +75,59 @@ namespace System.Collections.ObjectModel
#region IDictionary<TKey, TValue> Members
- public bool ContainsKey(TKey key) {
+ public bool ContainsKey(TKey key)
+ {
return m_dictionary.ContainsKey(key);
}
- ICollection<TKey> IDictionary<TKey, TValue>.Keys {
- get {
+ ICollection<TKey> IDictionary<TKey, TValue>.Keys
+ {
+ get
+ {
return Keys;
}
}
- public bool TryGetValue(TKey key, out TValue value) {
+ public bool TryGetValue(TKey key, out TValue value)
+ {
return m_dictionary.TryGetValue(key, out value);
}
- ICollection<TValue> IDictionary<TKey, TValue>.Values {
- get {
+ ICollection<TValue> IDictionary<TKey, TValue>.Values
+ {
+ get
+ {
return Values;
}
}
- public TValue this[TKey key] {
- get {
+ public TValue this[TKey key]
+ {
+ get
+ {
return m_dictionary[key];
}
}
- void IDictionary<TKey, TValue>.Add(TKey key, TValue value) {
+ void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
+ {
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}
- bool IDictionary<TKey, TValue>.Remove(TKey key) {
+ bool IDictionary<TKey, TValue>.Remove(TKey key)
+ {
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
return false;
}
- TValue IDictionary<TKey, TValue>.this[TKey key] {
- get {
+ TValue IDictionary<TKey, TValue>.this[TKey key]
+ {
+ get
+ {
return m_dictionary[key];
}
- set {
+ set
+ {
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}
}
@@ -114,31 +136,38 @@ namespace System.Collections.ObjectModel
#region ICollection<KeyValuePair<TKey, TValue>> Members
- public int Count {
+ public int Count
+ {
get { return m_dictionary.Count; }
}
- bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) {
+ bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
+ {
return m_dictionary.Contains(item);
}
- void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
+ void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
+ {
m_dictionary.CopyTo(array, arrayIndex);
}
- bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
+ bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
+ {
get { return true; }
}
- void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) {
+ void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
+ {
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}
- void ICollection<KeyValuePair<TKey, TValue>>.Clear() {
+ void ICollection<KeyValuePair<TKey, TValue>>.Clear()
+ {
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}
- bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) {
+ bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
+ {
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
return false;
}
@@ -147,7 +176,8 @@ namespace System.Collections.ObjectModel
#region IEnumerable<KeyValuePair<TKey, TValue>> Members
- public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
+ public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
+ {
return m_dictionary.GetEnumerator();
}
@@ -155,7 +185,8 @@ namespace System.Collections.ObjectModel
#region IEnumerable Members
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
+ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
+ {
return ((IEnumerable)m_dictionary).GetEnumerator();
}
@@ -163,131 +194,170 @@ namespace System.Collections.ObjectModel
#region IDictionary Members
- private static bool IsCompatibleKey(object key) {
- if (key == null) {
+ private static bool IsCompatibleKey(object key)
+ {
+ if (key == null)
+ {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
}
return key is TKey;
}
- void IDictionary.Add(object key, object value) {
+ void IDictionary.Add(object key, object value)
+ {
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}
- void IDictionary.Clear() {
+ void IDictionary.Clear()
+ {
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}
- bool IDictionary.Contains(object key) {
+ bool IDictionary.Contains(object key)
+ {
return IsCompatibleKey(key) && ContainsKey((TKey)key);
}
- IDictionaryEnumerator IDictionary.GetEnumerator() {
+ IDictionaryEnumerator IDictionary.GetEnumerator()
+ {
IDictionary d = m_dictionary as IDictionary;
- if (d != null) {
+ if (d != null)
+ {
return d.GetEnumerator();
}
return new DictionaryEnumerator(m_dictionary);
}
- bool IDictionary.IsFixedSize {
+ bool IDictionary.IsFixedSize
+ {
get { return true; }
}
- bool IDictionary.IsReadOnly {
+ bool IDictionary.IsReadOnly
+ {
get { return true; }
}
- ICollection IDictionary.Keys {
- get {
+ ICollection IDictionary.Keys
+ {
+ get
+ {
return Keys;
}
}
- void IDictionary.Remove(object key) {
+ void IDictionary.Remove(object key)
+ {
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}
- ICollection IDictionary.Values {
- get {
+ ICollection IDictionary.Values
+ {
+ get
+ {
return Values;
}
}
- object IDictionary.this[object key] {
- get {
- if (IsCompatibleKey(key)) {
+ object IDictionary.this[object key]
+ {
+ get
+ {
+ if (IsCompatibleKey(key))
+ {
return this[(TKey)key];
}
return null;
}
- set {
+ set
+ {
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}
}
- void ICollection.CopyTo(Array array, int index) {
- if (array == null) {
+ void ICollection.CopyTo(Array array, int index)
+ {
+ if (array == null)
+ {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
}
- if (array.Rank != 1) {
+ if (array.Rank != 1)
+ {
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
}
- if (array.GetLowerBound(0) != 0) {
+ if (array.GetLowerBound(0) != 0)
+ {
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);
}
- if (index < 0 || index > array.Length) {
+ if (index < 0 || index > array.Length)
+ {
ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
}
- if (array.Length - index < Count) {
+ if (array.Length - index < Count)
+ {
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
}
KeyValuePair<TKey, TValue>[] pairs = array as KeyValuePair<TKey, TValue>[];
- if (pairs != null) {
+ if (pairs != null)
+ {
m_dictionary.CopyTo(pairs, index);
}
- else {
+ else
+ {
DictionaryEntry[] dictEntryArray = array as DictionaryEntry[];
- if (dictEntryArray != null) {
- foreach (var item in m_dictionary) {
+ if (dictEntryArray != null)
+ {
+ foreach (var item in m_dictionary)
+ {
dictEntryArray[index++] = new DictionaryEntry(item.Key, item.Value);
}
}
- else {
+ else
+ {
object[] objects = array as object[];
- if (objects == null) {
+ if (objects == null)
+ {
ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType();
}
- try {
- foreach (var item in m_dictionary) {
+ try
+ {
+ foreach (var item in m_dictionary)
+ {
objects[index++] = new KeyValuePair<TKey, TValue>(item.Key, item.Value);
}
}
- catch (ArrayTypeMismatchException) {
+ catch (ArrayTypeMismatchException)
+ {
ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType();
}
}
}
}
- bool ICollection.IsSynchronized {
+ bool ICollection.IsSynchronized
+ {
get { return false; }
}
- object ICollection.SyncRoot {
- get {
- if (m_syncRoot == null) {
+ object ICollection.SyncRoot
+ {
+ get
+ {
+ if (m_syncRoot == null)
+ {
ICollection c = m_dictionary as ICollection;
- if (c != null) {
+ if (c != null)
+ {
m_syncRoot = c.SyncRoot;
}
- else {
+ else
+ {
System.Threading.Interlocked.CompareExchange<Object>(ref m_syncRoot, new Object(), null);
}
}
@@ -296,36 +366,44 @@ namespace System.Collections.ObjectModel
}
[Serializable]
- private struct DictionaryEnumerator : IDictionaryEnumerator {
+ private struct DictionaryEnumerator : IDictionaryEnumerator
+ {
private readonly IDictionary<TKey, TValue> m_dictionary;
private IEnumerator<KeyValuePair<TKey, TValue>> m_enumerator;
- public DictionaryEnumerator(IDictionary<TKey, TValue> dictionary) {
+ public DictionaryEnumerator(IDictionary<TKey, TValue> dictionary)
+ {
m_dictionary = dictionary;
m_enumerator = m_dictionary.GetEnumerator();
}
- public DictionaryEntry Entry {
+ public DictionaryEntry Entry
+ {
get { return new DictionaryEntry(m_enumerator.Current.Key, m_enumerator.Current.Value); }
}
- public object Key {
+ public object Key
+ {
get { return m_enumerator.Current.Key; }
}
- public object Value {
+ public object Value
+ {
get { return m_enumerator.Current.Value; }
}
- public object Current {
+ public object Current
+ {
get { return Entry; }
}
- public bool MoveNext() {
+ public bool MoveNext()
+ {
return m_enumerator.MoveNext();
}
- public void Reset() {
+ public void Reset()
+ {
m_enumerator.Reset();
}
}
@@ -334,14 +412,18 @@ namespace System.Collections.ObjectModel
#region IReadOnlyDictionary members
- IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys {
- get {
+ IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys
+ {
+ get
+ {
return Keys;
}
}
- IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values {
- get {
+ IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values
+ {
+ get
+ {
return Values;
}
}
@@ -351,14 +433,16 @@ namespace System.Collections.ObjectModel
[DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
[DebuggerDisplay("Count = {Count}")]
[Serializable]
- public sealed class KeyCollection : ICollection<TKey>, ICollection, IReadOnlyCollection<TKey> {
+ public sealed class KeyCollection : ICollection<TKey>, ICollection, IReadOnlyCollection<TKey>
+ {
private readonly ICollection<TKey> m_collection;
[NonSerialized]
private Object m_syncRoot;
internal KeyCollection(ICollection<TKey> collection)
{
- if (collection == null) {
+ if (collection == null)
+ {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
}
m_collection = collection;
@@ -386,11 +470,13 @@ namespace System.Collections.ObjectModel
m_collection.CopyTo(array, arrayIndex);
}
- public int Count {
+ public int Count
+ {
get { return m_collection.Count; }
}
- bool ICollection<TKey>.IsReadOnly {
+ bool ICollection<TKey>.IsReadOnly
+ {
get { return true; }
}
@@ -413,7 +499,8 @@ namespace System.Collections.ObjectModel
#region IEnumerable Members
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
+ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
+ {
return ((IEnumerable)m_collection).GetEnumerator();
}
@@ -421,22 +508,29 @@ namespace System.Collections.ObjectModel
#region ICollection Members
- void ICollection.CopyTo(Array array, int index) {
+ void ICollection.CopyTo(Array array, int index)
+ {
ReadOnlyDictionaryHelpers.CopyToNonGenericICollectionHelper<TKey>(m_collection, array, index);
}
- bool ICollection.IsSynchronized {
+ bool ICollection.IsSynchronized
+ {
get { return false; }
}
- object ICollection.SyncRoot {
- get {
- if (m_syncRoot == null) {
+ object ICollection.SyncRoot
+ {
+ get
+ {
+ if (m_syncRoot == null)
+ {
ICollection c = m_collection as ICollection;
- if (c != null) {
+ if (c != null)
+ {
m_syncRoot = c.SyncRoot;
}
- else {
+ else
+ {
System.Threading.Interlocked.CompareExchange<Object>(ref m_syncRoot, new Object(), null);
}
}
@@ -450,14 +544,16 @@ namespace System.Collections.ObjectModel
[DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
[DebuggerDisplay("Count = {Count}")]
[Serializable]
- public sealed class ValueCollection : ICollection<TValue>, ICollection, IReadOnlyCollection<TValue> {
+ public sealed class ValueCollection : ICollection<TValue>, ICollection, IReadOnlyCollection<TValue>
+ {
private readonly ICollection<TValue> m_collection;
[NonSerialized]
private Object m_syncRoot;
internal ValueCollection(ICollection<TValue> collection)
{
- if (collection == null) {
+ if (collection == null)
+ {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
}
m_collection = collection;
@@ -485,11 +581,13 @@ namespace System.Collections.ObjectModel
m_collection.CopyTo(array, arrayIndex);
}
- public int Count {
+ public int Count
+ {
get { return m_collection.Count; }
}
- bool ICollection<TValue>.IsReadOnly {
+ bool ICollection<TValue>.IsReadOnly
+ {
get { return true; }
}
@@ -512,7 +610,8 @@ namespace System.Collections.ObjectModel
#region IEnumerable Members
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
+ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
+ {
return ((IEnumerable)m_collection).GetEnumerator();
}
@@ -520,22 +619,29 @@ namespace System.Collections.ObjectModel
#region ICollection Members
- void ICollection.CopyTo(Array array, int index) {
+ void ICollection.CopyTo(Array array, int index)
+ {
ReadOnlyDictionaryHelpers.CopyToNonGenericICollectionHelper<TValue>(m_collection, array, index);
}
- bool ICollection.IsSynchronized {
+ bool ICollection.IsSynchronized
+ {
get { return false; }
}
- object ICollection.SyncRoot {
- get {
- if (m_syncRoot == null) {
+ object ICollection.SyncRoot
+ {
+ get
+ {
+ if (m_syncRoot == null)
+ {
ICollection c = m_collection as ICollection;
- if (c != null) {
+ if (c != null)
+ {
m_syncRoot = c.SyncRoot;
}
- else {
+ else
+ {
System.Threading.Interlocked.CompareExchange<Object>(ref m_syncRoot, new Object(), null);
}
}
@@ -555,38 +661,46 @@ namespace System.Collections.ObjectModel
// Abstracted away to avoid redundant implementations.
internal static void CopyToNonGenericICollectionHelper<T>(ICollection<T> collection, Array array, int index)
{
- if (array == null) {
+ if (array == null)
+ {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
}
- if (array.Rank != 1) {
+ if (array.Rank != 1)
+ {
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
}
- if (array.GetLowerBound(0) != 0) {
+ if (array.GetLowerBound(0) != 0)
+ {
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);
}
- if (index < 0) {
+ if (index < 0)
+ {
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
}
- if (array.Length - index < collection.Count) {
+ if (array.Length - index < collection.Count)
+ {
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
}
// Easy out if the ICollection<T> implements the non-generic ICollection
ICollection nonGenericCollection = collection as ICollection;
- if (nonGenericCollection != null) {
+ if (nonGenericCollection != null)
+ {
nonGenericCollection.CopyTo(array, index);
return;
}
T[] items = array as T[];
- if (items != null) {
+ if (items != null)
+ {
collection.CopyTo(items, index);
}
- else {
+ else
+ {
//
// Catch the obvious case assignment will fail.
// We can found all possible problems by doing the check though.
@@ -595,7 +709,8 @@ namespace System.Collections.ObjectModel
//
Type targetType = array.GetType().GetElementType();
Type sourceType = typeof(T);
- if (!(targetType.IsAssignableFrom(sourceType) || sourceType.IsAssignableFrom(targetType))) {
+ if (!(targetType.IsAssignableFrom(sourceType) || sourceType.IsAssignableFrom(targetType)))
+ {
ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType();
}
@@ -604,16 +719,20 @@ namespace System.Collections.ObjectModel
// widening of primitive types here.
//
object[] objects = array as object[];
- if (objects == null) {
+ if (objects == null)
+ {
ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType();
}
- try {
- foreach (var item in collection) {
+ try
+ {
+ foreach (var item in collection)
+ {
objects[index++] = item;
}
}
- catch (ArrayTypeMismatchException) {
+ catch (ArrayTypeMismatchException)
+ {
ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType();
}
}