Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9480 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 8.4 KB
RevLine 
[2572]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 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;
[3560]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2572]28
29namespace HeuristicLab.Collections {
[3560]30  [StorableClass]
[2572]31  [Serializable]
[2574]32  public class ObservableDictionary<TKey, TValue> : IObservableDictionary<TKey, TValue> {
[3560]33    [Storable]
[3390]34    protected Dictionary<TKey, TValue> dict;
[2572]35
36    #region Properties
37    public ICollection<TKey> Keys {
38      get { return dict.Keys; }
39    }
40    public ICollection<TValue> Values {
41      get { return dict.Values; }
42    }
43    public int Count {
44      get { return dict.Count; }
45    }
46    public IEqualityComparer<TKey> Comparer {
47      get { return dict.Comparer; }
48    }
49    bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
50      get { return ((ICollection<KeyValuePair<TKey, TValue>>)dict).IsReadOnly; }
51    }
52
53    public TValue this[TKey key] {
54      get {
55        return dict[key];
56      }
57      set {
[2573]58        if (dict.ContainsKey(key)) {
59          KeyValuePair<TKey, TValue> item = new KeyValuePair<TKey, TValue>(key, dict[key]);
60          dict[key] = value;
61          OnItemsReplaced(new KeyValuePair<TKey, TValue>[] { new KeyValuePair<TKey, TValue>(key, value) }, new KeyValuePair<TKey, TValue>[] { item });
62        } else {
63          dict[key] = value;
64          OnItemsAdded(new KeyValuePair<TKey, TValue>[] { new KeyValuePair<TKey, TValue>(key, value) });
65        }
[2572]66      }
67    }
68    #endregion
69
70    #region Constructors
71    public ObservableDictionary() {
72      dict = new Dictionary<TKey, TValue>();
73    }
74    public ObservableDictionary(int capacity) {
75      dict = new Dictionary<TKey, TValue>(capacity);
76    }
77    public ObservableDictionary(IEqualityComparer<TKey> comparer) {
78      dict = new Dictionary<TKey, TValue>(comparer);
79    }
80    public ObservableDictionary(IDictionary<TKey, TValue> dictionary) {
81      dict = new Dictionary<TKey, TValue>(dictionary);
82    }
83    public ObservableDictionary(int capacity, IEqualityComparer<TKey> comparer) {
84      dict = new Dictionary<TKey, TValue>(capacity, comparer);
85    }
86    public ObservableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) {
87      dict = new Dictionary<TKey, TValue>(dictionary, comparer);
88    }
[3560]89    [StorableConstructor]
90    protected ObservableDictionary(bool deserializing) { }
[2572]91    #endregion
92
93    #region Access
94    public bool ContainsKey(TKey key) {
95      return dict.ContainsKey(key);
96    }
97    public bool ContainsValue(TValue value) {
98      return dict.ContainsValue(value);
99    }
100    bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) {
101      return dict.Contains(item);
102    }
[2573]103
[2572]104    public bool TryGetValue(TKey key, out TValue value) {
105      return dict.TryGetValue(key, out value);
106    }
107    #endregion
108
109    #region Manipulation
110    public void Add(TKey key, TValue value) {
111      dict.Add(key, value);
[2620]112      OnPropertyChanged("Item[]");
113      OnPropertyChanged("Keys");
114      OnPropertyChanged("Values");
115      OnPropertyChanged("Count");
[2572]116      OnItemsAdded(new KeyValuePair<TKey, TValue>[] { new KeyValuePair<TKey, TValue>(key, value) });
117    }
118    void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) {
119      ((ICollection<KeyValuePair<TKey, TValue>>)dict).Add(item);
[2620]120      OnPropertyChanged("Item[]");
121      OnPropertyChanged("Keys");
122      OnPropertyChanged("Values");
123      OnPropertyChanged("Count");
[2572]124      OnItemsAdded(new KeyValuePair<TKey, TValue>[] { item });
125    }
126
127    public bool Remove(TKey key) {
128      TValue value;
129      if (dict.TryGetValue(key, out value)) {
130        dict.Remove(key);
[2620]131        OnPropertyChanged("Item[]");
132        OnPropertyChanged("Keys");
133        OnPropertyChanged("Values");
134        OnPropertyChanged("Count");
[2572]135        OnItemsRemoved(new KeyValuePair<TKey, TValue>[] { new KeyValuePair<TKey, TValue>(key, value) });
136        return true;
137      }
138      return false;
139    }
140    bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) {
141      if (((ICollection<KeyValuePair<TKey, TValue>>)dict).Remove(item)) {
[2620]142        OnPropertyChanged("Item[]");
143        OnPropertyChanged("Keys");
144        OnPropertyChanged("Values");
145        OnPropertyChanged("Count");
[2572]146        OnItemsRemoved(new KeyValuePair<TKey, TValue>[] { item });
147        return true;
148      }
149      return false;
150    }
151
152    public void Clear() {
[2620]153      if (dict.Count > 0) {
154        KeyValuePair<TKey, TValue>[] items = dict.ToArray();
155        dict.Clear();
156        OnPropertyChanged("Item[]");
157        OnPropertyChanged("Keys");
158        OnPropertyChanged("Values");
159        OnPropertyChanged("Count");
160        OnCollectionReset(new KeyValuePair<TKey, TValue>[0], items);
161      }
[2572]162    }
163    #endregion
164
165    #region Conversion
[2618]166    public ReadOnlyObservableDictionary<TKey, TValue> AsReadOnly() {
167      return new ReadOnlyObservableDictionary<TKey, TValue>(this);
168    }
[2572]169    void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
170      ((ICollection<KeyValuePair<TKey, TValue>>)dict).CopyTo(array, arrayIndex);
171    }
172    #endregion
173
174    #region Enumeration
[2623]175    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
[2572]176      return dict.GetEnumerator();
177    }
178    IEnumerator IEnumerable.GetEnumerator() {
[2623]179      return dict.GetEnumerator();
[2572]180    }
181    #endregion
[2574]182
183    #region Events
184    [field: NonSerialized]
185    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsAdded;
186    protected virtual void OnItemsAdded(IEnumerable<KeyValuePair<TKey, TValue>> items) {
[3317]187      CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = ItemsAdded;
188      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items));
[2574]189    }
190
191    [field: NonSerialized]
192    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsRemoved;
193    protected virtual void OnItemsRemoved(IEnumerable<KeyValuePair<TKey, TValue>> items) {
[3317]194      CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = ItemsRemoved;
195      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items));
[2574]196    }
197
198    [field: NonSerialized]
199    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsReplaced;
200    protected virtual void OnItemsReplaced(IEnumerable<KeyValuePair<TKey, TValue>> items, IEnumerable<KeyValuePair<TKey, TValue>> oldItems) {
[3317]201      CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = ItemsReplaced;
202      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items, oldItems));
[2574]203    }
204
205    [field: NonSerialized]
206    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> CollectionReset;
207    protected virtual void OnCollectionReset(IEnumerable<KeyValuePair<TKey, TValue>> items, IEnumerable<KeyValuePair<TKey, TValue>> oldItems) {
[3317]208      CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = CollectionReset;
209      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items, oldItems));
[2574]210    }
[2620]211
212    [field: NonSerialized]
213    public event PropertyChangedEventHandler PropertyChanged;
214    protected virtual void OnPropertyChanged(string propertyName) {
[3317]215      PropertyChangedEventHandler handler = PropertyChanged;
216      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
[2620]217    }
[2574]218    #endregion
[2572]219  }
220}
Note: See TracBrowser for help on using the repository browser.