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, 14 years ago

Refactored HeuristicLab.Collections (#977)

File size: 8.3 KB
RevLine 
[2572]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2572]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;
[2620]25using System.ComponentModel;
[2572]26using System.Linq;
27
28namespace HeuristicLab.Collections {
29  [Serializable]
[2574]30  public class ObservableDictionary<TKey, TValue> : IObservableDictionary<TKey, TValue> {
[3390]31    protected Dictionary<TKey, TValue> dict;
[2572]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 {
[2573]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        }
[2572]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    }
[2573]98
[2572]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);
[2620]107      OnPropertyChanged("Item[]");
108      OnPropertyChanged("Keys");
109      OnPropertyChanged("Values");
110      OnPropertyChanged("Count");
[2572]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);
[2620]115      OnPropertyChanged("Item[]");
116      OnPropertyChanged("Keys");
117      OnPropertyChanged("Values");
118      OnPropertyChanged("Count");
[2572]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);
[2620]126        OnPropertyChanged("Item[]");
127        OnPropertyChanged("Keys");
128        OnPropertyChanged("Values");
129        OnPropertyChanged("Count");
[2572]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)) {
[2620]137        OnPropertyChanged("Item[]");
138        OnPropertyChanged("Keys");
139        OnPropertyChanged("Values");
140        OnPropertyChanged("Count");
[2572]141        OnItemsRemoved(new KeyValuePair<TKey, TValue>[] { item });
142        return true;
143      }
144      return false;
145    }
146
147    public void Clear() {
[2620]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      }
[2572]157    }
158    #endregion
159
160    #region Conversion
[2618]161    public ReadOnlyObservableDictionary<TKey, TValue> AsReadOnly() {
162      return new ReadOnlyObservableDictionary<TKey, TValue>(this);
163    }
[2572]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
[2623]170    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
[2572]171      return dict.GetEnumerator();
172    }
173    IEnumerator IEnumerable.GetEnumerator() {
[2623]174      return dict.GetEnumerator();
[2572]175    }
176    #endregion
[2574]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) {
[3317]182      CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = ItemsAdded;
183      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items));
[2574]184    }
185
186    [field: NonSerialized]
187    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsRemoved;
188    protected virtual void OnItemsRemoved(IEnumerable<KeyValuePair<TKey, TValue>> items) {
[3317]189      CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = ItemsRemoved;
190      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items));
[2574]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) {
[3317]196      CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = ItemsReplaced;
197      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items, oldItems));
[2574]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) {
[3317]203      CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = CollectionReset;
204      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items, oldItems));
[2574]205    }
[2620]206
207    [field: NonSerialized]
208    public event PropertyChangedEventHandler PropertyChanged;
209    protected virtual void OnPropertyChanged(string propertyName) {
[3317]210      PropertyChangedEventHandler handler = PropertyChanged;
211      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
[2620]212    }
[2574]213    #endregion
[2572]214  }
215}
Note: See TracBrowser for help on using the repository browser.