[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]
|
---|
[3286] | 34 | protected Dictionary<TKey, TItem> dict;
|
---|
[2573] | 35 |
|
---|
| 36 | #region Properties
|
---|
[3317] | 37 | [Storable]
|
---|
| 38 | private bool readOnlyView;
|
---|
[3370] | 39 | public virtual bool ReadOnlyView {
|
---|
[3317] | 40 | get { return readOnlyView; }
|
---|
| 41 | set {
|
---|
[3370] | 42 | if ((readOnlyView != value) && !((ICollection<KeyValuePair<TKey, TItem>>)dict).IsReadOnly) {
|
---|
[3317] | 43 | readOnlyView = value;
|
---|
| 44 | OnReadOnlyViewChanged();
|
---|
| 45 | OnPropertyChanged("ReadOnlyView");
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[2573] | 50 | public int Count {
|
---|
| 51 | get { return dict.Count; }
|
---|
| 52 | }
|
---|
| 53 | public IEqualityComparer<TKey> Comparer {
|
---|
| 54 | get { return dict.Comparer; }
|
---|
| 55 | }
|
---|
[2618] | 56 | bool ICollection<TItem>.IsReadOnly {
|
---|
[2573] | 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
|
---|
[2664] | 68 | protected ObservableKeyedCollection() {
|
---|
[2573] | 69 | dict = new Dictionary<TKey, TItem>();
|
---|
[3317] | 70 | readOnlyView = ((ICollection<KeyValuePair<TKey, TItem>>)dict).IsReadOnly;
|
---|
[2573] | 71 | }
|
---|
[2664] | 72 | protected ObservableKeyedCollection(int capacity) {
|
---|
[2573] | 73 | dict = new Dictionary<TKey, TItem>(capacity);
|
---|
[3317] | 74 | readOnlyView = ((ICollection<KeyValuePair<TKey, TItem>>)dict).IsReadOnly;
|
---|
[2573] | 75 | }
|
---|
[2664] | 76 | protected ObservableKeyedCollection(IEqualityComparer<TKey> comparer) {
|
---|
[2573] | 77 | dict = new Dictionary<TKey, TItem>(comparer);
|
---|
[3317] | 78 | readOnlyView = ((ICollection<KeyValuePair<TKey, TItem>>)dict).IsReadOnly;
|
---|
[2573] | 79 | }
|
---|
[2664] | 80 | protected ObservableKeyedCollection(IEnumerable<TItem> collection) {
|
---|
[2573] | 81 | if (collection == null) throw new ArgumentNullException();
|
---|
| 82 | dict = new Dictionary<TKey, TItem>();
|
---|
| 83 | foreach (TItem item in collection)
|
---|
[2575] | 84 | dict.Add(GetKeyForItem(item), item);
|
---|
[3317] | 85 | readOnlyView = ((ICollection<KeyValuePair<TKey, TItem>>)dict).IsReadOnly;
|
---|
[2573] | 86 | }
|
---|
[2664] | 87 | protected ObservableKeyedCollection(int capacity, IEqualityComparer<TKey> comparer) {
|
---|
[2573] | 88 | dict = new Dictionary<TKey, TItem>(capacity, comparer);
|
---|
[3317] | 89 | readOnlyView = ((ICollection<KeyValuePair<TKey, TItem>>)dict).IsReadOnly;
|
---|
[2573] | 90 | }
|
---|
[2664] | 91 | protected ObservableKeyedCollection(IEnumerable<TItem> collection, IEqualityComparer<TKey> comparer) {
|
---|
[2573] | 92 | if (collection == null) throw new ArgumentNullException();
|
---|
| 93 | dict = new Dictionary<TKey, TItem>(comparer);
|
---|
| 94 | foreach (TItem item in collection)
|
---|
[2575] | 95 | dict.Add(GetKeyForItem(item), item);
|
---|
[3317] | 96 | readOnlyView = ((ICollection<KeyValuePair<TKey, TItem>>)dict).IsReadOnly;
|
---|
[2573] | 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 | }
|
---|
[2620] | 112 | if (!oldKeyFound) throw new ArgumentException("Item not found");
|
---|
[2573] | 113 | dict.Remove(oldKey);
|
---|
| 114 | dict.Add(GetKeyForItem(item), item);
|
---|
[2620] | 115 | OnPropertyChanged("Item[]");
|
---|
[2573] | 116 | OnItemsReplaced(new TItem[] { item }, new TItem[] { item });
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | #region Access
|
---|
[2618] | 120 | public bool ContainsKey(TKey key) {
|
---|
[2573] | 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
|
---|
[2575] | 157 | public void Add(TItem item) {
|
---|
[2573] | 158 | dict.Add(GetKeyForItem(item), item);
|
---|
[2620] | 159 | OnPropertyChanged("Item[]");
|
---|
| 160 | OnPropertyChanged("Count");
|
---|
[2573] | 161 | OnItemsAdded(new TItem[] { item });
|
---|
| 162 | }
|
---|
| 163 | public void AddRange(IEnumerable<TItem> collection) {
|
---|
| 164 | if (collection == null) throw new ArgumentNullException();
|
---|
[2620] | 165 | bool empty = true;
|
---|
| 166 | foreach (TItem item in collection) {
|
---|
[2575] | 167 | dict.Add(GetKeyForItem(item), item);
|
---|
[2620] | 168 | empty = false;
|
---|
| 169 | }
|
---|
| 170 | if (!empty) {
|
---|
| 171 | OnPropertyChanged("Item[]");
|
---|
| 172 | OnPropertyChanged("Count");
|
---|
| 173 | OnItemsAdded(collection);
|
---|
| 174 | }
|
---|
[2573] | 175 | }
|
---|
| 176 |
|
---|
| 177 | public bool Remove(TKey key) {
|
---|
| 178 | TItem item;
|
---|
| 179 | if (TryGetValue(key, out item)) {
|
---|
[2575] | 180 | dict.Remove(key);
|
---|
[2620] | 181 | OnPropertyChanged("Item[]");
|
---|
| 182 | OnPropertyChanged("Count");
|
---|
[2573] | 183 | OnItemsRemoved(new TItem[] { item });
|
---|
| 184 | return true;
|
---|
| 185 | }
|
---|
| 186 | return false;
|
---|
| 187 | }
|
---|
| 188 | public bool Remove(TItem item) {
|
---|
[2575] | 189 | if (dict.Remove(GetKeyForItem(item))) {
|
---|
[2620] | 190 | OnPropertyChanged("Item[]");
|
---|
| 191 | OnPropertyChanged("Count");
|
---|
[2573] | 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) {
|
---|
[2575] | 201 | if (dict.Remove(GetKeyForItem(item)))
|
---|
[2573] | 202 | items.Add(item);
|
---|
| 203 | }
|
---|
[2620] | 204 | if (items.Count > 0) {
|
---|
| 205 | OnPropertyChanged("Item[]");
|
---|
| 206 | OnPropertyChanged("Count");
|
---|
[2573] | 207 | OnItemsRemoved(items);
|
---|
[2620] | 208 | }
|
---|
[2573] | 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() {
|
---|
[2620] | 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 | }
|
---|
[2573] | 224 | }
|
---|
| 225 | #endregion
|
---|
| 226 |
|
---|
| 227 | #region Conversion
|
---|
[2618] | 228 | public ReadOnlyObservableKeyedCollection<TKey, TItem> AsReadOnly() {
|
---|
| 229 | return new ReadOnlyObservableKeyedCollection<TKey, TItem>(this);
|
---|
| 230 | }
|
---|
[2573] | 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
|
---|
[2574] | 268 |
|
---|
| 269 | #region Events
|
---|
| 270 | [field: NonSerialized]
|
---|
[3317] | 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]
|
---|
[2574] | 278 | public event CollectionItemsChangedEventHandler<TItem> ItemsAdded;
|
---|
| 279 | protected virtual void OnItemsAdded(IEnumerable<TItem> items) {
|
---|
[3317] | 280 | CollectionItemsChangedEventHandler<TItem> handler = ItemsAdded;
|
---|
| 281 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items));
|
---|
[2574] | 282 | }
|
---|
| 283 |
|
---|
| 284 | [field: NonSerialized]
|
---|
| 285 | public event CollectionItemsChangedEventHandler<TItem> ItemsRemoved;
|
---|
| 286 | protected virtual void OnItemsRemoved(IEnumerable<TItem> items) {
|
---|
[3317] | 287 | CollectionItemsChangedEventHandler<TItem> handler = ItemsRemoved;
|
---|
| 288 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items));
|
---|
[2574] | 289 | }
|
---|
| 290 |
|
---|
| 291 | [field: NonSerialized]
|
---|
| 292 | public event CollectionItemsChangedEventHandler<TItem> ItemsReplaced;
|
---|
| 293 | protected virtual void OnItemsReplaced(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
|
---|
[3317] | 294 | CollectionItemsChangedEventHandler<TItem> handler = ItemsReplaced;
|
---|
| 295 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
|
---|
[2574] | 296 | }
|
---|
| 297 |
|
---|
| 298 | [field: NonSerialized]
|
---|
| 299 | public event CollectionItemsChangedEventHandler<TItem> CollectionReset;
|
---|
| 300 | protected virtual void OnCollectionReset(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
|
---|
[3317] | 301 | CollectionItemsChangedEventHandler<TItem> handler = CollectionReset;
|
---|
| 302 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
|
---|
[2574] | 303 | }
|
---|
[2620] | 304 |
|
---|
| 305 | [field: NonSerialized]
|
---|
| 306 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
| 307 | protected virtual void OnPropertyChanged(string propertyName) {
|
---|
[3317] | 308 | PropertyChangedEventHandler handler = PropertyChanged;
|
---|
| 309 | if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
|
---|
[2620] | 310 | }
|
---|
[2574] | 311 | #endregion
|
---|
[2573] | 312 | }
|
---|
| 313 | }
|
---|