//note: ripped from openstacknetsdk using System; using System.Collections; using System.Collections.Generic; namespace Svelto.DataStructures { public struct ReadOnlyDictionary : IDictionary, IDictionary { private readonly IDictionary _dictionary; /// /// Initializes a new instance of the class /// that is a wrapper around the specified dictionary. /// /// The dictionary to wrap. public ReadOnlyDictionary(IDictionary dictionary) { if (dictionary == null) throw new ArgumentNullException("dictionary"); _dictionary = dictionary; } public bool isInitialized { get { return _dictionary != null; } } /// /// Gets the element that has the specified key. /// /// The key of the element to get. /// The element that has the specified key. /// If is . /// If property is retrieved and is not found. public TValue this[TKey key] { get { return _dictionary[key]; } } /// /// /// Gets the element that has the specified key. /// /// If the property is set. TValue IDictionary.this[TKey key] { get { return this[key]; } set { throw new NotSupportedException(); } } /// /// /// Gets the element that has the specified key. /// /// If the property is set. object IDictionary.this[object key] { get { if (!(key is TKey)) return null; return this[(TKey)key]; } set { throw new NotSupportedException(); } } /// /// Gets the number of items in the dictionary. /// /// /// The number of items in the dictionary. /// public int Count { get { return _dictionary.Count; } } /// /// Gets a key collection that contains the keys of the dictionary. /// /// /// A key collection that contains the keys of the dictionary. /// public KeyCollection Keys { get { return new KeyCollection(_dictionary.Keys); } } /// ICollection IDictionary.Keys { get { return Keys; } } /// ICollection IDictionary.Keys { get { return Keys; } } /// /// Gets a collection that contains the values in the dictionary. /// /// /// A collection that contains the values in the object that implements . /// public ValueCollection Values { get { return new ValueCollection(_dictionary.Values); } } /// ICollection IDictionary.Values { get { return Values; } } /// ICollection IDictionary.Values { get { return Values; } } /// bool ICollection>.IsReadOnly { get { return true; } } /// bool IDictionary.IsFixedSize { get { return true; } } /// bool IDictionary.IsReadOnly { get { return true; } } /// bool ICollection.IsSynchronized { get { return false; } } /// object ICollection.SyncRoot { get { ICollection collection = this as ICollection; if (collection == null) return collection.SyncRoot; throw new NotSupportedException("The current object does not support the SyncRoot property."); } } /// /// Determines whether the dictionary contains an element that has the specified key. /// /// The key to locate in the dictionary. /// if the dictionary contains an element that has the specified key; otherwise, . public bool ContainsKey(TKey key) { return _dictionary.ContainsKey(key); } /// bool IDictionary.Contains(object key) { if (key == null) throw new ArgumentNullException("key"); if (key is TKey) return ContainsKey((TKey)key); return false; } /// /// Returns an enumerator that iterates through the . /// /// An enumerator that can be used to iterate through the collection. public IEnumerator> GetEnumerator() { return _dictionary.GetEnumerator(); } /// IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } /// IDictionaryEnumerator IDictionary.GetEnumerator() { IDictionary dictionary = _dictionary as IDictionary; if (dictionary != null) return dictionary.GetEnumerator(); return new DictionaryEnumerator(_dictionary); } /// /// Retrieves the value that is associated with the specified key. /// /// The key whose value will be retrieved. /// When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the parameter. This parameter is passed uninitialized. /// if the object that implements contains an element with the specified key; otherwise, . public bool TryGetValue(TKey key, out TValue value) { return _dictionary.TryGetValue(key, out value); } /// void IDictionary.Add(TKey key, TValue value) { throw new NotSupportedException(); } /// void IDictionary.Add(object key, object value) { throw new NotSupportedException(); } /// bool IDictionary.Remove(TKey key) { throw new NotSupportedException(); } /// void IDictionary.Remove(object key) { throw new NotSupportedException(); } /// void ICollection>.Add(KeyValuePair item) { throw new NotSupportedException(); } /// void ICollection>.Clear() { throw new NotSupportedException(); } /// bool ICollection>.Contains(KeyValuePair item) { throw new NotImplementedException(); } /// void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) { throw new NotImplementedException(); } /// bool ICollection>.Remove(KeyValuePair item) { throw new NotSupportedException(); } /// void IDictionary.Clear() { throw new NotSupportedException(); } /// void ICollection.CopyTo(Array array, int index) { throw new NotImplementedException(); } /// /// Represents a read-only collection of the keys of a object. /// public struct KeyCollection : ICollection, ICollection { /// /// The wrapped collection of keys. /// private readonly ICollection _keys; /// /// Initializes a new instance of the class /// as a wrapper around the specified collection of keys. /// /// The collection of keys to wrap. /// If is . internal KeyCollection(ICollection keys) { if (keys == null) throw new ArgumentNullException("keys"); _keys = keys; } /// /// Gets the number of elements in the collection. /// /// /// The number of elements in the collection. /// public int Count { get { return _keys.Count; } } /// bool ICollection.IsReadOnly { get { return true; } } /// bool ICollection.IsSynchronized { get { return false; } } /// object ICollection.SyncRoot { get { throw new NotImplementedException(); } } /// /// Copies the elements of the collection to an array, starting at a specific array index. /// /// The one-dimensional array that is the destination of the elements copied from the collection. The array must have zero-based indexing. /// The zero-based index in at which copying begins. /// If is . /// If is less than 0. /// /// If is multidimensional. /// -or- /// If the number of elements in the source collection is greater than the available space from to the end of the destination . /// -or- /// If the type cannot be cast automatically to the type of the destination . /// public void CopyTo(TKey[] array, int arrayIndex) { _keys.CopyTo(array, arrayIndex); } /// void ICollection.CopyTo(Array array, int index) { throw new NotImplementedException(); } /// /// Returns an enumerator that iterates through the collection. /// /// An enumerator that can be used to iterate through the collection. public IEnumerator GetEnumerator() { return _keys.GetEnumerator(); } /// IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } /// bool ICollection.Contains(TKey item) { return _keys.Contains(item); } /// void ICollection.Add(TKey item) { throw new NotSupportedException(); } /// bool ICollection.Remove(TKey item) { throw new NotSupportedException(); } /// void ICollection.Clear() { throw new NotSupportedException(); } } /// /// Represents a read-only collection of the values of a object. /// public struct ValueCollection : ICollection, ICollection { /// /// The wrapped collection of values. /// private readonly ICollection _values; /// /// Initializes a new instance of the class /// as a wrapper around the specified collection of values. /// /// The collection of values to wrap. /// If is . internal ValueCollection(ICollection values) { if (values == null) throw new ArgumentNullException("values"); _values = values; } /// /// Gets the number of elements in the collection. /// /// /// The number of elements in the collection. /// public int Count { get { return _values.Count; } } /// bool ICollection.IsReadOnly { get { return true; } } /// bool ICollection.IsSynchronized { get { return false; } } /// object ICollection.SyncRoot { get { throw new NotImplementedException(); } } /// /// Copies the elements of the collection to an array, starting at a specific array index. /// /// The one-dimensional array that is the destination of the elements copied from the collection. The array must have zero-based indexing. /// The zero-based index in at which copying begins. /// If is . /// If is less than 0. /// /// If is multidimensional. /// -or- /// If the number of elements in the source collection is greater than the available space from to the end of the destination . /// -or- /// If the type cannot be cast automatically to the type of the destination . /// public void CopyTo(TValue[] array, int arrayIndex) { _values.CopyTo(array, arrayIndex); } /// void ICollection.CopyTo(Array array, int index) { throw new NotImplementedException(); } /// /// Returns an enumerator that iterates through the collection. /// /// An enumerator that can be used to iterate through the collection. public IEnumerator GetEnumerator() { return _values.GetEnumerator(); } /// IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } /// bool ICollection.Contains(TValue item) { return _values.Contains(item); } /// void ICollection.Add(TValue item) { throw new NotSupportedException(); } /// bool ICollection.Remove(TValue item) { throw new NotSupportedException(); } /// void ICollection.Clear() { throw new NotSupportedException(); } } struct DictionaryEnumerator : IDictionaryEnumerator { private readonly IEnumerator> _enumerator; public DictionaryEnumerator(IDictionary dictionary) { if (dictionary == null) throw new ArgumentNullException("dictionary"); _enumerator = dictionary.GetEnumerator(); } /// public DictionaryEntry Entry { get { KeyValuePair current = _enumerator.Current; return new DictionaryEntry(current.Key, current.Value); } } /// public object Key { get { return _enumerator.Current.Key; } } /// public object Value { get { return _enumerator.Current.Value; } } /// public object Current { get { return Entry; } } /// public bool MoveNext() { return _enumerator.MoveNext(); } /// public void Reset() { _enumerator.Reset(); } } } }