[2573] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 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;
|
---|
[3560] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2573] | 28 |
|
---|
| 29 | namespace HeuristicLab.Collections {
|
---|
[3560] | 30 | [StorableClass]
|
---|
[2573] | 31 | [Serializable]
|
---|
[2664] | 32 | public abstract class ObservableKeyedCollection<TKey, TItem> : IObservableKeyedCollection<TKey, TItem> {
|
---|
[3560] | 33 | [Storable]
|
---|
[3286] | 34 | protected Dictionary<TKey, TItem> dict;
|
---|
[2573] | 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 | }
|
---|
[3560] | 79 | [StorableConstructor]
|
---|
| 80 | protected ObservableKeyedCollection(bool deserializing) { }
|
---|
[2573] | 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 | }
|
---|
[2620] | 95 | if (!oldKeyFound) throw new ArgumentException("Item not found");
|
---|
[2573] | 96 | dict.Remove(oldKey);
|
---|
| 97 | dict.Add(GetKeyForItem(item), item);
|
---|
[2620] | 98 | OnPropertyChanged("Item[]");
|
---|
[2573] | 99 | OnItemsReplaced(new TItem[] { item }, new TItem[] { item });
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | #region Access
|
---|
[2618] | 103 | public bool ContainsKey(TKey key) {
|
---|
[2573] | 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
|
---|
[2575] | 140 | public void Add(TItem item) {
|
---|
[2573] | 141 | dict.Add(GetKeyForItem(item), item);
|
---|
[2620] | 142 | OnPropertyChanged("Item[]");
|
---|
| 143 | OnPropertyChanged("Count");
|
---|
[2573] | 144 | OnItemsAdded(new TItem[] { item });
|
---|
| 145 | }
|
---|
| 146 | public void AddRange(IEnumerable<TItem> collection) {
|
---|
| 147 | if (collection == null) throw new ArgumentNullException();
|
---|
[2620] | 148 | bool empty = true;
|
---|
| 149 | foreach (TItem item in collection) {
|
---|
[2575] | 150 | dict.Add(GetKeyForItem(item), item);
|
---|
[2620] | 151 | empty = false;
|
---|
| 152 | }
|
---|
| 153 | if (!empty) {
|
---|
| 154 | OnPropertyChanged("Item[]");
|
---|
| 155 | OnPropertyChanged("Count");
|
---|
| 156 | OnItemsAdded(collection);
|
---|
| 157 | }
|
---|
[2573] | 158 | }
|
---|
| 159 |
|
---|
| 160 | public bool Remove(TKey key) {
|
---|
| 161 | TItem item;
|
---|
| 162 | if (TryGetValue(key, out item)) {
|
---|
[2575] | 163 | dict.Remove(key);
|
---|
[2620] | 164 | OnPropertyChanged("Item[]");
|
---|
| 165 | OnPropertyChanged("Count");
|
---|
[2573] | 166 | OnItemsRemoved(new TItem[] { item });
|
---|
| 167 | return true;
|
---|
| 168 | }
|
---|
| 169 | return false;
|
---|
| 170 | }
|
---|
| 171 | public bool Remove(TItem item) {
|
---|
[2575] | 172 | if (dict.Remove(GetKeyForItem(item))) {
|
---|
[2620] | 173 | OnPropertyChanged("Item[]");
|
---|
| 174 | OnPropertyChanged("Count");
|
---|
[2573] | 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) {
|
---|
[2575] | 184 | if (dict.Remove(GetKeyForItem(item)))
|
---|
[2573] | 185 | items.Add(item);
|
---|
| 186 | }
|
---|
[2620] | 187 | if (items.Count > 0) {
|
---|
| 188 | OnPropertyChanged("Item[]");
|
---|
| 189 | OnPropertyChanged("Count");
|
---|
[2573] | 190 | OnItemsRemoved(items);
|
---|
[2620] | 191 | }
|
---|
[2573] | 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() {
|
---|
[2620] | 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 | }
|
---|
[2573] | 207 | }
|
---|
| 208 | #endregion
|
---|
| 209 |
|
---|
| 210 | #region Conversion
|
---|
[2618] | 211 | public ReadOnlyObservableKeyedCollection<TKey, TItem> AsReadOnly() {
|
---|
| 212 | return new ReadOnlyObservableKeyedCollection<TKey, TItem>(this);
|
---|
| 213 | }
|
---|
[2573] | 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
|
---|
[2574] | 251 |
|
---|
| 252 | #region Events
|
---|
| 253 | [field: NonSerialized]
|
---|
| 254 | public event CollectionItemsChangedEventHandler<TItem> ItemsAdded;
|
---|
| 255 | protected virtual void OnItemsAdded(IEnumerable<TItem> items) {
|
---|
[3317] | 256 | CollectionItemsChangedEventHandler<TItem> handler = ItemsAdded;
|
---|
| 257 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items));
|
---|
[2574] | 258 | }
|
---|
| 259 |
|
---|
| 260 | [field: NonSerialized]
|
---|
| 261 | public event CollectionItemsChangedEventHandler<TItem> ItemsRemoved;
|
---|
| 262 | protected virtual void OnItemsRemoved(IEnumerable<TItem> items) {
|
---|
[3317] | 263 | CollectionItemsChangedEventHandler<TItem> handler = ItemsRemoved;
|
---|
| 264 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items));
|
---|
[2574] | 265 | }
|
---|
| 266 |
|
---|
| 267 | [field: NonSerialized]
|
---|
| 268 | public event CollectionItemsChangedEventHandler<TItem> ItemsReplaced;
|
---|
| 269 | protected virtual void OnItemsReplaced(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
|
---|
[3317] | 270 | CollectionItemsChangedEventHandler<TItem> handler = ItemsReplaced;
|
---|
| 271 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
|
---|
[2574] | 272 | }
|
---|
| 273 |
|
---|
| 274 | [field: NonSerialized]
|
---|
| 275 | public event CollectionItemsChangedEventHandler<TItem> CollectionReset;
|
---|
| 276 | protected virtual void OnCollectionReset(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
|
---|
[3317] | 277 | CollectionItemsChangedEventHandler<TItem> handler = CollectionReset;
|
---|
| 278 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
|
---|
[2574] | 279 | }
|
---|
[2620] | 280 |
|
---|
| 281 | [field: NonSerialized]
|
---|
| 282 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
| 283 | protected virtual void OnPropertyChanged(string propertyName) {
|
---|
[3317] | 284 | PropertyChangedEventHandler handler = PropertyChanged;
|
---|
| 285 | if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
|
---|
[2620] | 286 | }
|
---|
[2574] | 287 | #endregion
|
---|
[2573] | 288 | }
|
---|
| 289 | }
|
---|