Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored HeuristicLab.Collections (#977)

File size: 9.7 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 abstract class ObservableKeyedCollection<TKey, TItem> : IObservableKeyedCollection<TKey, TItem> {
31    protected Dictionary<TKey, TItem> dict;
32
33    #region Properties
34    public int Count {
35      get { return dict.Count; }
36    }
37    public IEqualityComparer<TKey> Comparer {
38      get { return dict.Comparer; }
39    }
40    bool ICollection<TItem>.IsReadOnly {
41      get { return ((ICollection<KeyValuePair<TKey, TItem>>)dict).IsReadOnly; }
42    }
43
44    public TItem this[TKey key] {
45      get {
46        return dict[key];
47      }
48    }
49    #endregion
50
51    #region Constructors
52    protected ObservableKeyedCollection() {
53      dict = new Dictionary<TKey, TItem>();
54    }
55    protected ObservableKeyedCollection(int capacity) {
56      dict = new Dictionary<TKey, TItem>(capacity);
57    }
58    protected ObservableKeyedCollection(IEqualityComparer<TKey> comparer) {
59      dict = new Dictionary<TKey, TItem>(comparer);
60    }
61    protected ObservableKeyedCollection(IEnumerable<TItem> collection) {
62      if (collection == null) throw new ArgumentNullException();
63      dict = new Dictionary<TKey, TItem>();
64      foreach (TItem item in collection)
65        dict.Add(GetKeyForItem(item), item);
66    }
67    protected ObservableKeyedCollection(int capacity, IEqualityComparer<TKey> comparer) {
68      dict = new Dictionary<TKey, TItem>(capacity, comparer);
69    }
70    protected ObservableKeyedCollection(IEnumerable<TItem> collection, IEqualityComparer<TKey> comparer) {
71      if (collection == null) throw new ArgumentNullException();
72      dict = new Dictionary<TKey, TItem>(comparer);
73      foreach (TItem item in collection)
74        dict.Add(GetKeyForItem(item), item);
75    }
76    #endregion
77
78    protected abstract TKey GetKeyForItem(TItem item);
79    protected void UpdateItemKey(TItem item) {
80      if (item == null) throw new ArgumentNullException();
81      TKey oldKey = default(TKey);
82      bool oldKeyFound = false;
83      foreach (KeyValuePair<TKey, TItem> entry in dict) {
84        if (entry.Value.Equals(item)) {
85          oldKey = entry.Key;
86          oldKeyFound = true;
87          break;
88        }
89      }
90      if (!oldKeyFound) throw new ArgumentException("Item not found");
91      dict.Remove(oldKey);
92      dict.Add(GetKeyForItem(item), item);
93      OnPropertyChanged("Item[]");
94      OnItemsReplaced(new TItem[] { item }, new TItem[] { item });
95    }
96
97    #region Access
98    public bool ContainsKey(TKey key) {
99      return dict.ContainsKey(key);
100    }
101    public bool Contains(TItem item) {
102      return dict.ContainsValue(item);
103    }
104
105    public bool TryGetValue(TKey key, out TItem item) {
106      return dict.TryGetValue(key, out item);
107    }
108
109    public bool Exists(Predicate<TItem> match) {
110      if (match == null) throw new ArgumentNullException();
111      foreach (TItem item in dict.Values) {
112        if (match(item)) return true;
113      }
114      return false;
115    }
116
117    public TItem Find(Predicate<TItem> match) {
118      if (match == null) throw new ArgumentNullException();
119      foreach (TItem item in dict.Values) {
120        if (match(item)) return item;
121      }
122      return default(TItem);
123    }
124    public ICollection<TItem> FindAll(Predicate<TItem> match) {
125      if (match == null) throw new ArgumentNullException();
126      List<TItem> result = new List<TItem>();
127      foreach (TItem item in dict.Values) {
128        if (match(item)) result.Add(item);
129      }
130      return result;
131    }
132    #endregion
133
134    #region Manipulation
135    public void Add(TItem item) {
136      dict.Add(GetKeyForItem(item), item);
137      OnPropertyChanged("Item[]");
138      OnPropertyChanged("Count");
139      OnItemsAdded(new TItem[] { item });
140    }
141    public void AddRange(IEnumerable<TItem> collection) {
142      if (collection == null) throw new ArgumentNullException();
143      bool empty = true;
144      foreach (TItem item in collection) {
145        dict.Add(GetKeyForItem(item), item);
146        empty = false;
147      }
148      if (!empty) {
149        OnPropertyChanged("Item[]");
150        OnPropertyChanged("Count");
151        OnItemsAdded(collection);
152      }
153    }
154
155    public bool Remove(TKey key) {
156      TItem item;
157      if (TryGetValue(key, out item)) {
158        dict.Remove(key);
159        OnPropertyChanged("Item[]");
160        OnPropertyChanged("Count");
161        OnItemsRemoved(new TItem[] { item });
162        return true;
163      }
164      return false;
165    }
166    public bool Remove(TItem item) {
167      if (dict.Remove(GetKeyForItem(item))) {
168        OnPropertyChanged("Item[]");
169        OnPropertyChanged("Count");
170        OnItemsRemoved(new TItem[] { item });
171        return true;
172      }
173      return false;
174    }
175    public void RemoveRange(IEnumerable<TItem> collection) {
176      if (collection == null) throw new ArgumentNullException();
177      List<TItem> items = new List<TItem>();
178      foreach (TItem item in collection) {
179        if (dict.Remove(GetKeyForItem(item)))
180          items.Add(item);
181      }
182      if (items.Count > 0) {
183        OnPropertyChanged("Item[]");
184        OnPropertyChanged("Count");
185        OnItemsRemoved(items);
186      }
187    }
188    public int RemoveAll(Predicate<TItem> match) {
189      ICollection<TItem> items = FindAll(match);
190      RemoveRange(items);
191      return items.Count;
192    }
193
194    public void Clear() {
195      if (dict.Count > 0) {
196        TItem[] items = dict.Values.ToArray();
197        dict.Clear();
198        OnPropertyChanged("Item[]");
199        OnPropertyChanged("Count");
200        OnCollectionReset(new TItem[0], items);
201      }
202    }
203    #endregion
204
205    #region Conversion
206    public ReadOnlyObservableKeyedCollection<TKey, TItem> AsReadOnly() {
207      return new ReadOnlyObservableKeyedCollection<TKey, TItem>(this);
208    }
209    public TItem[] ToArray() {
210      return dict.Values.ToArray();
211    }
212    public void CopyTo(TItem[] array, int arrayIndex) {
213      dict.Values.CopyTo(array, arrayIndex);
214    }
215    public ICollection<TOutput> ConvertAll<TOutput>(Converter<TItem, TOutput> converter) {
216      if (converter == null) throw new ArgumentNullException();
217      List<TOutput> result = new List<TOutput>();
218      foreach (TItem item in dict.Values)
219        result.Add(converter(item));
220      return result;
221    }
222    #endregion
223
224    #region Processing
225    public void ForEach(Action<TItem> action) {
226      if (action == null) throw new ArgumentNullException();
227      foreach (TItem item in dict.Values)
228        action(item);
229    }
230    public bool TrueForAll(Predicate<TItem> match) {
231      if (match == null) throw new ArgumentNullException();
232      foreach (TItem item in dict.Values)
233        if (! match(item)) return false;
234      return true;
235    }
236    #endregion
237
238    #region Enumeration
239    public IEnumerator<TItem> GetEnumerator() {
240      return dict.Values.GetEnumerator();
241    }
242    IEnumerator IEnumerable.GetEnumerator() {
243      return dict.Values.GetEnumerator();
244    }
245    #endregion
246
247    #region Events
248    [field: NonSerialized]
249    public event CollectionItemsChangedEventHandler<TItem> ItemsAdded;
250    protected virtual void OnItemsAdded(IEnumerable<TItem> items) {
251      CollectionItemsChangedEventHandler<TItem> handler = ItemsAdded;
252      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items));
253    }
254
255    [field: NonSerialized]
256    public event CollectionItemsChangedEventHandler<TItem> ItemsRemoved;
257    protected virtual void OnItemsRemoved(IEnumerable<TItem> items) {
258      CollectionItemsChangedEventHandler<TItem> handler = ItemsRemoved;
259      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items));
260    }
261
262    [field: NonSerialized]
263    public event CollectionItemsChangedEventHandler<TItem> ItemsReplaced;
264    protected virtual void OnItemsReplaced(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
265      CollectionItemsChangedEventHandler<TItem> handler = ItemsReplaced;
266      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
267    }
268
269    [field: NonSerialized]
270    public event CollectionItemsChangedEventHandler<TItem> CollectionReset;
271    protected virtual void OnCollectionReset(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
272      CollectionItemsChangedEventHandler<TItem> handler = CollectionReset;
273      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
274    }
275
276    [field: NonSerialized]
277    public event PropertyChangedEventHandler PropertyChanged;
278    protected virtual void OnPropertyChanged(string propertyName) {
279      PropertyChangedEventHandler handler = PropertyChanged;
280      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
281    }
282    #endregion
283  }
284}
Note: See TracBrowser for help on using the repository browser.