1 using System.Collections;
  2 using System.Collections.Generic;
  3 using metrics.Core;
  4 
  5 namespace metrics.Support
  6 {
  7     /// <summary>
  8     /// Provides an immutable dictionary
  9     /// </summary>
 10     internal class ReadOnlyDictionary<T, TK> : IDictionary<T, TK> where TK : ICopyable<TK>
 11     {
 12         private readonly IDictionary<T, TK> _inner;
 13 
 14         public ReadOnlyDictionary(ICollection<KeyValuePair<T, TK>> inner)
 15         {
 16             _inner = new Dictionary<T, TK>(inner.Count);
 17             foreach(var entry in inner)
 18             {
 19                 _inner.Add(entry.Key, entry.Value.Copy);
 20             }
 21         }
 22 
 23         public IEnumerator<KeyValuePair<T, TK>> GetEnumerator()
 24         {
 25             return _inner.GetEnumerator();
 26         }
 27 
 28         IEnumerator IEnumerable.GetEnumerator()
 29         {
 30             return GetEnumerator();
 31         }
 32 
 33         public void Add(KeyValuePair<T, TK> item) { /* read-only */ }
 34 
 35         public void Clear() { /* read-only */ }
 36 
 37         public bool Contains(KeyValuePair<T, TK> item)
 38         {
 39             return _inner.Contains(item);
 40         }
 41 
 42         public void CopyTo(KeyValuePair<T, TK>[] array, int arrayIndex)
 43         {
 44             CopyInner().CopyTo(array, arrayIndex);
 45         }
 46 
 47         private IDictionary<T, TK> CopyInner()
 48         {
 49             IDictionary<T, TK> copy = new Dictionary<T, TK>(_inner.Count);
 50             foreach (var entry in _inner)
 51             {
 52                 copy.Add(entry.Key, entry.Value.Copy);
 53             }
 54             return copy;
 55         }
 56 
 57         public bool Remove(KeyValuePair<T, TK> item) { return false; /* read-only */ }
 58 
 59         public int Count
 60         {
 61             get { return _inner.Count; }
 62         }
 63 
 64         public bool IsReadOnly
 65         {
 66             get { return true; }
 67         }
 68 
 69         public bool ContainsKey(T key)
 70         {
 71             return _inner.ContainsKey(key);
 72         }
 73 
 74         public void Add(T key, TK value) { /* read-only */ }
 75 
 76         public bool Remove(T key) { return false; /* read-only */ }
 77 
 78         public bool TryGetValue(T key, out TK value)
 79         {
 80             var result = _inner.TryGetValue(key, out value);
 81             value = value.Copy;
 82             return result;
 83         }
 84 
 85         public TK this[T key]
 86         {
 87             get
 88             {
 89                 return _inner[key].Copy;
 90             }
 91             set { /* read-only */ }
 92         }
 93 
 94         public ICollection<T> Keys
 95         {
 96             get { return CopyInner().Keys; }
 97         }
 98 
 99         public ICollection<TK> Values
100         {
101             get { return CopyInner().Values; }
102         }
103     }
104 }