Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2995 was 2995, checked in by abeham, 14 years ago

Added StorableClass attribute to several more classes #548

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