Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Collections/3.3/ObservableKeyedCollectionBase.cs @ 2573

Last change on this file since 2573 was 2573, checked in by swagner, 15 years ago

Worked on HeuristicLab.Collections plugin (#819)

  • added ObservableKeyedCollectionBase
  • minor fixes
File size: 7.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.ObjectModel;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Collections {
31  [Serializable]
32  public abstract class ObservableKeyedCollectionBase<TKey, TItem> : CollectionChangedEventsBase<TItem>, ICollection<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    public bool 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 ObservableKeyedCollectionBase() {
56      dict = new Dictionary<TKey, TItem>();
57    }
58    protected ObservableKeyedCollectionBase(int capacity) {
59      dict = new Dictionary<TKey, TItem>(capacity);
60    }
61    protected ObservableKeyedCollectionBase(IEqualityComparer<TKey> comparer) {
62      dict = new Dictionary<TKey, TItem>(comparer);
63    }
64    protected ObservableKeyedCollectionBase(IEnumerable<TItem> collection) {
65      if (collection == null) throw new ArgumentNullException();
66      dict = new Dictionary<TKey, TItem>();
67      foreach (TItem item in collection)
68        AddItem(item);
69    }
70    protected ObservableKeyedCollectionBase(int capacity, IEqualityComparer<TKey> comparer) {
71      dict = new Dictionary<TKey, TItem>(capacity, comparer);
72    }
73    protected ObservableKeyedCollectionBase(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        AddItem(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      OnItemsReplaced(new TItem[] { item }, new TItem[] { item });
97    }
98
99    #region Access
100    public bool Contains(TKey key) {
101      return dict.ContainsKey(key);
102    }
103    public bool Contains(TItem item) {
104      return dict.ContainsValue(item);
105    }
106
107    public bool TryGetValue(TKey key, out TItem item) {
108      return dict.TryGetValue(key, out item);
109    }
110
111    public bool Exists(Predicate<TItem> match) {
112      if (match == null) throw new ArgumentNullException();
113      foreach (TItem item in dict.Values) {
114        if (match(item)) return true;
115      }
116      return false;
117    }
118
119    public TItem Find(Predicate<TItem> match) {
120      if (match == null) throw new ArgumentNullException();
121      foreach (TItem item in dict.Values) {
122        if (match(item)) return item;
123      }
124      return default(TItem);
125    }
126    public ICollection<TItem> FindAll(Predicate<TItem> match) {
127      if (match == null) throw new ArgumentNullException();
128      List<TItem> result = new List<TItem>();
129      foreach (TItem item in dict.Values) {
130        if (match(item)) result.Add(item);
131      }
132      return result;
133    }
134    #endregion
135
136    #region Manipulation
137    protected virtual void AddItem(TItem item) {
138      dict.Add(GetKeyForItem(item), item);
139    }
140    public void Add(TItem item) {
141      AddItem(item);
142      OnItemsAdded(new TItem[] { item });
143    }
144    public void AddRange(IEnumerable<TItem> collection) {
145      if (collection == null) throw new ArgumentNullException();
146      foreach (TItem item in collection)
147        AddItem(item);
148      OnItemsAdded(collection);
149    }
150
151    protected virtual bool RemoveItem(TItem item) {
152      return dict.Remove(GetKeyForItem(item));
153    }
154    public bool Remove(TKey key) {
155      TItem item;
156      if (TryGetValue(key, out item)) {
157        RemoveItem(item);
158        OnItemsRemoved(new TItem[] { item });
159        return true;
160      }
161      return false;
162    }
163    public bool Remove(TItem item) {
164      if (RemoveItem(item)) {
165        OnItemsRemoved(new TItem[] { item });
166        return true;
167      }
168      return false;
169    }
170    public void RemoveRange(IEnumerable<TItem> collection) {
171      if (collection == null) throw new ArgumentNullException();
172      List<TItem> items = new List<TItem>();
173      foreach (TItem item in collection) {
174        if (RemoveItem(item))
175          items.Add(item);
176      }
177      if (items.Count > 0)
178        OnItemsRemoved(items);
179    }
180    public int RemoveAll(Predicate<TItem> match) {
181      ICollection<TItem> items = FindAll(match);
182      RemoveRange(items);
183      return items.Count;
184    }
185
186    protected virtual void ClearItems() {
187      dict.Clear();
188    }
189    public void Clear() {
190      TItem[] items = dict.Values.ToArray();
191      ClearItems();
192      OnCollectionReset(new TItem[0], items);
193    }
194    #endregion
195
196    #region Conversion
197    public TItem[] ToArray() {
198      return dict.Values.ToArray();
199    }
200    public void CopyTo(TItem[] array, int arrayIndex) {
201      dict.Values.CopyTo(array, arrayIndex);
202    }
203    public ICollection<TOutput> ConvertAll<TOutput>(Converter<TItem, TOutput> converter) {
204      if (converter == null) throw new ArgumentNullException();
205      List<TOutput> result = new List<TOutput>();
206      foreach (TItem item in dict.Values)
207        result.Add(converter(item));
208      return result;
209    }
210    #endregion
211
212    #region Processing
213    public void ForEach(Action<TItem> action) {
214      if (action == null) throw new ArgumentNullException();
215      foreach (TItem item in dict.Values)
216        action(item);
217    }
218    public bool TrueForAll(Predicate<TItem> match) {
219      if (match == null) throw new ArgumentNullException();
220      foreach (TItem item in dict.Values)
221        if (! match(item)) return false;
222      return true;
223    }
224    #endregion
225
226    #region Enumeration
227    public IEnumerator<TItem> GetEnumerator() {
228      return dict.Values.GetEnumerator();
229    }
230    IEnumerator IEnumerable.GetEnumerator() {
231      return dict.Values.GetEnumerator();
232    }
233    #endregion
234  }
235}
Note: See TracBrowser for help on using the repository browser.