From a56e30c8d33048216567753d9d3fefc2152af8ac Mon Sep 17 00:00:00 2001 From: Jiyoung Yun Date: Thu, 13 Apr 2017 14:17:19 +0900 Subject: Imported Upstream version 2.0.0.11353 --- .../Collections/ObjectModel/ReadOnlyDictionary.cs | 371 ++++++++++++++------- 1 file changed, 245 insertions(+), 126 deletions(-) (limited to 'src/mscorlib/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs') diff --git a/src/mscorlib/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs b/src/mscorlib/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs index 5c9e8c44c6..ebf86cdc58 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 : IDictionary, IDictionary, IReadOnlyDictionary + internal class ReadOnlyDictionary : IDictionary, IDictionary, IReadOnlyDictionary { private readonly IDictionary m_dictionary; [NonSerialized] @@ -32,32 +32,41 @@ namespace System.Collections.ObjectModel [NonSerialized] private ValueCollection m_values; - public ReadOnlyDictionary(IDictionary dictionary) { - if (dictionary == null) { + public ReadOnlyDictionary(IDictionary dictionary) + { + if (dictionary == null) + { throw new ArgumentNullException(nameof(dictionary)); } Contract.EndContractBlock(); m_dictionary = dictionary; } - protected IDictionary Dictionary { + protected IDictionary Dictionary + { get { return m_dictionary; } } - public KeyCollection Keys { - get { + public KeyCollection Keys + { + get + { Contract.Ensures(Contract.Result() != 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() != 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 Members - public bool ContainsKey(TKey key) { + public bool ContainsKey(TKey key) + { return m_dictionary.ContainsKey(key); } - ICollection IDictionary.Keys { - get { + ICollection IDictionary.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 IDictionary.Values { - get { + ICollection IDictionary.Values + { + get + { return Values; } } - public TValue this[TKey key] { - get { + public TValue this[TKey key] + { + get + { return m_dictionary[key]; } } - void IDictionary.Add(TKey key, TValue value) { + void IDictionary.Add(TKey key, TValue value) + { ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); } - bool IDictionary.Remove(TKey key) { + bool IDictionary.Remove(TKey key) + { ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); return false; } - TValue IDictionary.this[TKey key] { - get { + TValue IDictionary.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> Members - public int Count { + public int Count + { get { return m_dictionary.Count; } } - bool ICollection>.Contains(KeyValuePair item) { + bool ICollection>.Contains(KeyValuePair item) + { return m_dictionary.Contains(item); } - void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) { + void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) + { m_dictionary.CopyTo(array, arrayIndex); } - bool ICollection>.IsReadOnly { + bool ICollection>.IsReadOnly + { get { return true; } } - void ICollection>.Add(KeyValuePair item) { + void ICollection>.Add(KeyValuePair item) + { ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); } - void ICollection>.Clear() { + void ICollection>.Clear() + { ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); } - bool ICollection>.Remove(KeyValuePair item) { + bool ICollection>.Remove(KeyValuePair item) + { ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); return false; } @@ -147,7 +176,8 @@ namespace System.Collections.ObjectModel #region IEnumerable> Members - public IEnumerator> GetEnumerator() { + public IEnumerator> 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[] pairs = array as KeyValuePair[]; - 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(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(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 m_dictionary; private IEnumerator> m_enumerator; - public DictionaryEnumerator(IDictionary dictionary) { + public DictionaryEnumerator(IDictionary 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 IReadOnlyDictionary.Keys { - get { + IEnumerable IReadOnlyDictionary.Keys + { + get + { return Keys; } } - IEnumerable IReadOnlyDictionary.Values { - get { + IEnumerable IReadOnlyDictionary.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, ICollection, IReadOnlyCollection { + public sealed class KeyCollection : ICollection, ICollection, IReadOnlyCollection + { private readonly ICollection m_collection; [NonSerialized] private Object m_syncRoot; internal KeyCollection(ICollection 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.IsReadOnly { + bool ICollection.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(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(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, ICollection, IReadOnlyCollection { + public sealed class ValueCollection : ICollection, ICollection, IReadOnlyCollection + { private readonly ICollection m_collection; [NonSerialized] private Object m_syncRoot; internal ValueCollection(ICollection 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.IsReadOnly { + bool ICollection.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(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(ref m_syncRoot, new Object(), null); } } @@ -555,38 +661,46 @@ namespace System.Collections.ObjectModel // Abstracted away to avoid redundant implementations. internal static void CopyToNonGenericICollectionHelper(ICollection 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 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(); } } -- cgit v1.2.3