Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2877_HiveImprovements/HeuristicLab.Collections/3.3/ObservableKeyedCollection.cs @ 15859

Last change on this file since 15859 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

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