Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Collections/3.3/ObservableDictionary.cs @ 3555

Last change on this file since 3555 was 3390, checked in by swagner, 15 years ago

Refactored HeuristicLab.Collections (#977)

File size: 8.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.ComponentModel;
26using System.Linq;
27
28namespace HeuristicLab.Collections {
29  [Serializable]
30  public class ObservableDictionary<TKey, TValue> : IObservableDictionary<TKey, TValue> {
31    protected Dictionary<TKey, TValue> dict;
32
33    #region Properties
34    public ICollection<TKey> Keys {
35      get { return dict.Keys; }
36    }
37    public ICollection<TValue> Values {
38      get { return dict.Values; }
39    }
40    public int Count {
41      get { return dict.Count; }
42    }
43    public IEqualityComparer<TKey> Comparer {
44      get { return dict.Comparer; }
45    }
46    bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
47      get { return ((ICollection<KeyValuePair<TKey, TValue>>)dict).IsReadOnly; }
48    }
49
50    public TValue this[TKey key] {
51      get {
52        return dict[key];
53      }
54      set {
55        if (dict.ContainsKey(key)) {
56          KeyValuePair<TKey, TValue> item = new KeyValuePair<TKey, TValue>(key, dict[key]);
57          dict[key] = value;
58          OnItemsReplaced(new KeyValuePair<TKey, TValue>[] { new KeyValuePair<TKey, TValue>(key, value) }, new KeyValuePair<TKey, TValue>[] { item });
59        } else {
60          dict[key] = value;
61          OnItemsAdded(new KeyValuePair<TKey, TValue>[] { new KeyValuePair<TKey, TValue>(key, value) });
62        }
63      }
64    }
65    #endregion
66
67    #region Constructors
68    public ObservableDictionary() {
69      dict = new Dictionary<TKey, TValue>();
70    }
71    public ObservableDictionary(int capacity) {
72      dict = new Dictionary<TKey, TValue>(capacity);
73    }
74    public ObservableDictionary(IEqualityComparer<TKey> comparer) {
75      dict = new Dictionary<TKey, TValue>(comparer);
76    }
77    public ObservableDictionary(IDictionary<TKey, TValue> dictionary) {
78      dict = new Dictionary<TKey, TValue>(dictionary);
79    }
80    public ObservableDictionary(int capacity, IEqualityComparer<TKey> comparer) {
81      dict = new Dictionary<TKey, TValue>(capacity, comparer);
82    }
83    public ObservableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) {
84      dict = new Dictionary<TKey, TValue>(dictionary, comparer);
85    }
86    #endregion
87
88    #region Access
89    public bool ContainsKey(TKey key) {
90      return dict.ContainsKey(key);
91    }
92    public bool ContainsValue(TValue value) {
93      return dict.ContainsValue(value);
94    }
95    bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) {
96      return dict.Contains(item);
97    }
98
99    public bool TryGetValue(TKey key, out TValue value) {
100      return dict.TryGetValue(key, out value);
101    }
102    #endregion
103
104    #region Manipulation
105    public void Add(TKey key, TValue value) {
106      dict.Add(key, value);
107      OnPropertyChanged("Item[]");
108      OnPropertyChanged("Keys");
109      OnPropertyChanged("Values");
110      OnPropertyChanged("Count");
111      OnItemsAdded(new KeyValuePair<TKey, TValue>[] { new KeyValuePair<TKey, TValue>(key, value) });
112    }
113    void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) {
114      ((ICollection<KeyValuePair<TKey, TValue>>)dict).Add(item);
115      OnPropertyChanged("Item[]");
116      OnPropertyChanged("Keys");
117      OnPropertyChanged("Values");
118      OnPropertyChanged("Count");
119      OnItemsAdded(new KeyValuePair<TKey, TValue>[] { item });
120    }
121
122    public bool Remove(TKey key) {
123      TValue value;
124      if (dict.TryGetValue(key, out value)) {
125        dict.Remove(key);
126        OnPropertyChanged("Item[]");
127        OnPropertyChanged("Keys");
128        OnPropertyChanged("Values");
129        OnPropertyChanged("Count");
130        OnItemsRemoved(new KeyValuePair<TKey, TValue>[] { new KeyValuePair<TKey, TValue>(key, value) });
131        return true;
132      }
133      return false;
134    }
135    bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) {
136      if (((ICollection<KeyValuePair<TKey, TValue>>)dict).Remove(item)) {
137        OnPropertyChanged("Item[]");
138        OnPropertyChanged("Keys");
139        OnPropertyChanged("Values");
140        OnPropertyChanged("Count");
141        OnItemsRemoved(new KeyValuePair<TKey, TValue>[] { item });
142        return true;
143      }
144      return false;
145    }
146
147    public void Clear() {
148      if (dict.Count > 0) {
149        KeyValuePair<TKey, TValue>[] items = dict.ToArray();
150        dict.Clear();
151        OnPropertyChanged("Item[]");
152        OnPropertyChanged("Keys");
153        OnPropertyChanged("Values");
154        OnPropertyChanged("Count");
155        OnCollectionReset(new KeyValuePair<TKey, TValue>[0], items);
156      }
157    }
158    #endregion
159
160    #region Conversion
161    public ReadOnlyObservableDictionary<TKey, TValue> AsReadOnly() {
162      return new ReadOnlyObservableDictionary<TKey, TValue>(this);
163    }
164    void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
165      ((ICollection<KeyValuePair<TKey, TValue>>)dict).CopyTo(array, arrayIndex);
166    }
167    #endregion
168
169    #region Enumeration
170    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
171      return dict.GetEnumerator();
172    }
173    IEnumerator IEnumerable.GetEnumerator() {
174      return dict.GetEnumerator();
175    }
176    #endregion
177
178    #region Events
179    [field: NonSerialized]
180    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsAdded;
181    protected virtual void OnItemsAdded(IEnumerable<KeyValuePair<TKey, TValue>> items) {
182      CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = ItemsAdded;
183      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items));
184    }
185
186    [field: NonSerialized]
187    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsRemoved;
188    protected virtual void OnItemsRemoved(IEnumerable<KeyValuePair<TKey, TValue>> items) {
189      CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = ItemsRemoved;
190      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items));
191    }
192
193    [field: NonSerialized]
194    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsReplaced;
195    protected virtual void OnItemsReplaced(IEnumerable<KeyValuePair<TKey, TValue>> items, IEnumerable<KeyValuePair<TKey, TValue>> oldItems) {
196      CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = ItemsReplaced;
197      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items, oldItems));
198    }
199
200    [field: NonSerialized]
201    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> CollectionReset;
202    protected virtual void OnCollectionReset(IEnumerable<KeyValuePair<TKey, TValue>> items, IEnumerable<KeyValuePair<TKey, TValue>> oldItems) {
203      CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = CollectionReset;
204      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items, oldItems));
205    }
206
207    [field: NonSerialized]
208    public event PropertyChangedEventHandler PropertyChanged;
209    protected virtual void OnPropertyChanged(string propertyName) {
210      PropertyChangedEventHandler handler = PropertyChanged;
211      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
212    }
213    #endregion
214  }
215}
Note: See TracBrowser for help on using the repository browser.