[2573] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2573] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections;
|
---|
| 24 | using System.Collections.Generic;
|
---|
[2620] | 25 | using System.ComponentModel;
|
---|
[2573] | 26 | using System.Linq;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Collections {
|
---|
| 30 | [Serializable]
|
---|
[3017] | 31 | [StorableClass]
|
---|
[2664] | 32 | public abstract class ObservableKeyedCollection<TKey, TItem> : IObservableKeyedCollection<TKey, TItem> {
|
---|
[2573] | 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 | }
|
---|
[2618] | 43 | bool ICollection<TItem>.IsReadOnly {
|
---|
[2573] | 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
|
---|
[2664] | 55 | protected ObservableKeyedCollection() {
|
---|
[2573] | 56 | dict = new Dictionary<TKey, TItem>();
|
---|
| 57 | }
|
---|
[2664] | 58 | protected ObservableKeyedCollection(int capacity) {
|
---|
[2573] | 59 | dict = new Dictionary<TKey, TItem>(capacity);
|
---|
| 60 | }
|
---|
[2664] | 61 | protected ObservableKeyedCollection(IEqualityComparer<TKey> comparer) {
|
---|
[2573] | 62 | dict = new Dictionary<TKey, TItem>(comparer);
|
---|
| 63 | }
|
---|
[2664] | 64 | protected ObservableKeyedCollection(IEnumerable<TItem> collection) {
|
---|
[2573] | 65 | if (collection == null) throw new ArgumentNullException();
|
---|
| 66 | dict = new Dictionary<TKey, TItem>();
|
---|
| 67 | foreach (TItem item in collection)
|
---|
[2575] | 68 | dict.Add(GetKeyForItem(item), item);
|
---|
[2573] | 69 | }
|
---|
[2664] | 70 | protected ObservableKeyedCollection(int capacity, IEqualityComparer<TKey> comparer) {
|
---|
[2573] | 71 | dict = new Dictionary<TKey, TItem>(capacity, comparer);
|
---|
| 72 | }
|
---|
[2664] | 73 | protected ObservableKeyedCollection(IEnumerable<TItem> collection, IEqualityComparer<TKey> comparer) {
|
---|
[2573] | 74 | if (collection == null) throw new ArgumentNullException();
|
---|
| 75 | dict = new Dictionary<TKey, TItem>(comparer);
|
---|
| 76 | foreach (TItem item in collection)
|
---|
[2575] | 77 | dict.Add(GetKeyForItem(item), item);
|
---|
[2573] | 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 | }
|
---|
[2620] | 93 | if (!oldKeyFound) throw new ArgumentException("Item not found");
|
---|
[2573] | 94 | dict.Remove(oldKey);
|
---|
| 95 | dict.Add(GetKeyForItem(item), item);
|
---|
[2620] | 96 | OnPropertyChanged("Item[]");
|
---|
[2573] | 97 | OnItemsReplaced(new TItem[] { item }, new TItem[] { item });
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | #region Access
|
---|
[2618] | 101 | public bool ContainsKey(TKey key) {
|
---|
[2573] | 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
|
---|
[2575] | 138 | public void Add(TItem item) {
|
---|
[2573] | 139 | dict.Add(GetKeyForItem(item), item);
|
---|
[2620] | 140 | OnPropertyChanged("Item[]");
|
---|
| 141 | OnPropertyChanged("Count");
|
---|
[2573] | 142 | OnItemsAdded(new TItem[] { item });
|
---|
| 143 | }
|
---|
| 144 | public void AddRange(IEnumerable<TItem> collection) {
|
---|
| 145 | if (collection == null) throw new ArgumentNullException();
|
---|
[2620] | 146 | bool empty = true;
|
---|
| 147 | foreach (TItem item in collection) {
|
---|
[2575] | 148 | dict.Add(GetKeyForItem(item), item);
|
---|
[2620] | 149 | empty = false;
|
---|
| 150 | }
|
---|
| 151 | if (!empty) {
|
---|
| 152 | OnPropertyChanged("Item[]");
|
---|
| 153 | OnPropertyChanged("Count");
|
---|
| 154 | OnItemsAdded(collection);
|
---|
| 155 | }
|
---|
[2573] | 156 | }
|
---|
| 157 |
|
---|
| 158 | public bool Remove(TKey key) {
|
---|
| 159 | TItem item;
|
---|
| 160 | if (TryGetValue(key, out item)) {
|
---|
[2575] | 161 | dict.Remove(key);
|
---|
[2620] | 162 | OnPropertyChanged("Item[]");
|
---|
| 163 | OnPropertyChanged("Count");
|
---|
[2573] | 164 | OnItemsRemoved(new TItem[] { item });
|
---|
| 165 | return true;
|
---|
| 166 | }
|
---|
| 167 | return false;
|
---|
| 168 | }
|
---|
| 169 | public bool Remove(TItem item) {
|
---|
[2575] | 170 | if (dict.Remove(GetKeyForItem(item))) {
|
---|
[2620] | 171 | OnPropertyChanged("Item[]");
|
---|
| 172 | OnPropertyChanged("Count");
|
---|
[2573] | 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) {
|
---|
[2575] | 182 | if (dict.Remove(GetKeyForItem(item)))
|
---|
[2573] | 183 | items.Add(item);
|
---|
| 184 | }
|
---|
[2620] | 185 | if (items.Count > 0) {
|
---|
| 186 | OnPropertyChanged("Item[]");
|
---|
| 187 | OnPropertyChanged("Count");
|
---|
[2573] | 188 | OnItemsRemoved(items);
|
---|
[2620] | 189 | }
|
---|
[2573] | 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() {
|
---|
[2620] | 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 | }
|
---|
[2573] | 205 | }
|
---|
| 206 | #endregion
|
---|
| 207 |
|
---|
| 208 | #region Conversion
|
---|
[2618] | 209 | public ReadOnlyObservableKeyedCollection<TKey, TItem> AsReadOnly() {
|
---|
| 210 | return new ReadOnlyObservableKeyedCollection<TKey, TItem>(this);
|
---|
| 211 | }
|
---|
[2573] | 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
|
---|
[2574] | 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 | }
|
---|
[2620] | 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 | }
|
---|
[2574] | 285 | #endregion
|
---|
[2573] | 286 | }
|
---|
| 287 | }
|
---|