|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Linq; |
| 4 | +using System.ComponentModel; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Collections.Specialized; |
| 7 | + |
| 8 | +namespace Jojatekok.PoloniexAPI |
| 9 | +{ |
| 10 | + public class ObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, INotifyCollectionChanged, INotifyPropertyChanged |
| 11 | + { |
| 12 | + private const string CountString = "Count"; |
| 13 | + private const string IndexerName = "Item[]"; |
| 14 | + private const string KeysName = "Keys"; |
| 15 | + private const string ValuesName = "Values"; |
| 16 | + |
| 17 | + private IDictionary<TKey, TValue> Dictionary { get; set; } |
| 18 | + |
| 19 | + #region Constructors |
| 20 | + |
| 21 | + public ObservableDictionary() |
| 22 | + { |
| 23 | + Dictionary = new Dictionary<TKey, TValue>(); |
| 24 | + } |
| 25 | + public ObservableDictionary(IDictionary<TKey, TValue> dictionary) |
| 26 | + { |
| 27 | + Dictionary = new Dictionary<TKey, TValue>(dictionary); |
| 28 | + } |
| 29 | + public ObservableDictionary(IEqualityComparer<TKey> comparer) |
| 30 | + { |
| 31 | + Dictionary = new Dictionary<TKey, TValue>(comparer); |
| 32 | + } |
| 33 | + public ObservableDictionary(int capacity) |
| 34 | + { |
| 35 | + Dictionary = new Dictionary<TKey, TValue>(capacity); |
| 36 | + } |
| 37 | + public ObservableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) |
| 38 | + { |
| 39 | + Dictionary = new Dictionary<TKey, TValue>(dictionary, comparer); |
| 40 | + } |
| 41 | + public ObservableDictionary(int capacity, IEqualityComparer<TKey> comparer) |
| 42 | + { |
| 43 | + Dictionary = new Dictionary<TKey, TValue>(capacity, comparer); |
| 44 | + } |
| 45 | + |
| 46 | + #endregion |
| 47 | + |
| 48 | + #region IDictionary<TKey, TValue> Members |
| 49 | + |
| 50 | + public void Add(TKey key, TValue value) |
| 51 | + { |
| 52 | + Insert(key, value, true); |
| 53 | + } |
| 54 | + |
| 55 | + public bool ContainsKey(TKey key) |
| 56 | + { |
| 57 | + return Dictionary.ContainsKey(key); |
| 58 | + } |
| 59 | + |
| 60 | + public ICollection<TKey> Keys |
| 61 | + { |
| 62 | + get { return Dictionary.Keys; } |
| 63 | + } |
| 64 | + |
| 65 | + public bool Remove(TKey key) |
| 66 | + { |
| 67 | + if (key == null) throw new ArgumentNullException("key"); |
| 68 | + |
| 69 | + TValue value; |
| 70 | + Dictionary.TryGetValue(key, out value); |
| 71 | + var isRemoved = Dictionary.Remove(key); |
| 72 | + if (isRemoved) OnCollectionChanged(); |
| 73 | + |
| 74 | + return isRemoved; |
| 75 | + } |
| 76 | + |
| 77 | + public bool TryGetValue(TKey key, out TValue value) |
| 78 | + { |
| 79 | + return Dictionary.TryGetValue(key, out value); |
| 80 | + } |
| 81 | + |
| 82 | + public ICollection<TValue> Values |
| 83 | + { |
| 84 | + get { return Dictionary.Values; } |
| 85 | + } |
| 86 | + |
| 87 | + public TValue this[TKey key] |
| 88 | + { |
| 89 | + get { return Dictionary[key]; } |
| 90 | + set { Insert(key, value, false); } |
| 91 | + } |
| 92 | + |
| 93 | + #endregion |
| 94 | + |
| 95 | + #region ICollection<KeyValuePair<TKey, TValue>> Members |
| 96 | + |
| 97 | + public void Add(KeyValuePair<TKey, TValue> item) |
| 98 | + { |
| 99 | + Insert(item.Key, item.Value, true); |
| 100 | + } |
| 101 | + |
| 102 | + public void Clear() |
| 103 | + { |
| 104 | + if (Dictionary.Count != 0) { |
| 105 | + Dictionary.Clear(); |
| 106 | + OnCollectionChanged(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public bool Contains(KeyValuePair<TKey, TValue> item) |
| 111 | + { |
| 112 | + return Dictionary.Contains(item); |
| 113 | + } |
| 114 | + |
| 115 | + public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) |
| 116 | + { |
| 117 | + Dictionary.CopyTo(array, arrayIndex); |
| 118 | + } |
| 119 | + |
| 120 | + public int Count |
| 121 | + { |
| 122 | + get { return Dictionary.Count; } |
| 123 | + } |
| 124 | + |
| 125 | + public bool IsReadOnly |
| 126 | + { |
| 127 | + get { return Dictionary.IsReadOnly; } |
| 128 | + } |
| 129 | + |
| 130 | + public bool Remove(KeyValuePair<TKey, TValue> item) |
| 131 | + { |
| 132 | + return Remove(item.Key); |
| 133 | + } |
| 134 | + |
| 135 | + #endregion |
| 136 | + |
| 137 | + #region IEnumerable<KeyValuePair<TKey, TValue>> Members |
| 138 | + |
| 139 | + public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() |
| 140 | + { |
| 141 | + return Dictionary.GetEnumerator(); |
| 142 | + } |
| 143 | + |
| 144 | + #endregion |
| 145 | + |
| 146 | + #region IEnumerable Members |
| 147 | + |
| 148 | + IEnumerator IEnumerable.GetEnumerator() |
| 149 | + { |
| 150 | + return ((IEnumerable)Dictionary).GetEnumerator(); |
| 151 | + } |
| 152 | + |
| 153 | + #endregion |
| 154 | + |
| 155 | + #region INotifyCollectionChanged Members |
| 156 | + |
| 157 | + public event NotifyCollectionChangedEventHandler CollectionChanged; |
| 158 | + |
| 159 | + #endregion |
| 160 | + |
| 161 | + #region INotifyPropertyChanged Members |
| 162 | + |
| 163 | + public event PropertyChangedEventHandler PropertyChanged; |
| 164 | + |
| 165 | + #endregion |
| 166 | + |
| 167 | + public void AddRange(IDictionary<TKey, TValue> items) |
| 168 | + { |
| 169 | + if (items == null) throw new ArgumentNullException("items"); |
| 170 | + |
| 171 | + if (items.Count != 0) { |
| 172 | + if (Dictionary.Count != 0) { |
| 173 | + if (items.Keys.Any(x => Dictionary.ContainsKey(x))) { |
| 174 | + throw new ArgumentException("An item with the same key has already been added."); |
| 175 | + } |
| 176 | + |
| 177 | + foreach (var item in items) { |
| 178 | + Dictionary.Add(item); |
| 179 | + } |
| 180 | + |
| 181 | + } else { |
| 182 | + Dictionary = new Dictionary<TKey, TValue>(items); |
| 183 | + } |
| 184 | + |
| 185 | + OnCollectionChanged(NotifyCollectionChangedAction.Add, items.ToArray()); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + private void Insert(TKey key, TValue value, bool add) |
| 190 | + { |
| 191 | + if (key == null) throw new ArgumentNullException("key"); |
| 192 | + |
| 193 | + TValue item; |
| 194 | + if (Dictionary.TryGetValue(key, out item)) { |
| 195 | + if (add) throw new ArgumentException("An item with the same key has already been added."); |
| 196 | + if (Equals(item, value)) return; |
| 197 | + Dictionary[key] = value; |
| 198 | + |
| 199 | + OnCollectionChanged(NotifyCollectionChangedAction.Replace, new KeyValuePair<TKey, TValue>(key, value), new KeyValuePair<TKey, TValue>(key, item)); |
| 200 | + |
| 201 | + } else { |
| 202 | + Dictionary[key] = value; |
| 203 | + |
| 204 | + OnCollectionChanged(NotifyCollectionChangedAction.Add, new KeyValuePair<TKey, TValue>(key, value)); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + private void OnPropertyChanged() |
| 209 | + { |
| 210 | + OnPropertyChanged(CountString); |
| 211 | + OnPropertyChanged(IndexerName); |
| 212 | + OnPropertyChanged(KeysName); |
| 213 | + OnPropertyChanged(ValuesName); |
| 214 | + } |
| 215 | + |
| 216 | + protected virtual void OnPropertyChanged(string propertyName) |
| 217 | + { |
| 218 | + if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); |
| 219 | + } |
| 220 | + |
| 221 | + private void OnCollectionChanged() |
| 222 | + { |
| 223 | + OnPropertyChanged(); |
| 224 | + if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); |
| 225 | + } |
| 226 | + |
| 227 | + private void OnCollectionChanged(NotifyCollectionChangedAction action, KeyValuePair<TKey, TValue> changedItem) |
| 228 | + { |
| 229 | + OnPropertyChanged(); |
| 230 | + if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, changedItem)); |
| 231 | + } |
| 232 | + |
| 233 | + private void OnCollectionChanged(NotifyCollectionChangedAction action, KeyValuePair<TKey, TValue> newItem, KeyValuePair<TKey, TValue> oldItem) |
| 234 | + { |
| 235 | + OnPropertyChanged(); |
| 236 | + if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, newItem, oldItem)); |
| 237 | + } |
| 238 | + |
| 239 | + private void OnCollectionChanged(NotifyCollectionChangedAction action, IList newItems) |
| 240 | + { |
| 241 | + OnPropertyChanged(); |
| 242 | + if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, newItems)); |
| 243 | + } |
| 244 | + } |
| 245 | +} |
0 commit comments