Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on ReadOnlyView property (#969).

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