[2572] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2572] | 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;
|
---|
[3560] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2572] | 27 |
|
---|
| 28 | namespace HeuristicLab.Collections {
|
---|
[3560] | 29 | [StorableClass]
|
---|
[2572] | 30 | [Serializable]
|
---|
[2574] | 31 | public class ObservableCollection<T> : IObservableCollection<T> {
|
---|
[3560] | 32 | [Storable]
|
---|
[3286] | 33 | protected List<T> list;
|
---|
[2572] | 34 |
|
---|
| 35 | #region Properties
|
---|
| 36 | public int Capacity {
|
---|
| 37 | get { return list.Capacity; }
|
---|
[2620] | 38 | set {
|
---|
| 39 | if (list.Capacity != value) {
|
---|
| 40 | list.Capacity = value;
|
---|
| 41 | OnPropertyChanged("Capacity");
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
[2572] | 44 | }
|
---|
| 45 | public int Count {
|
---|
| 46 | get { return list.Count; }
|
---|
| 47 | }
|
---|
[2618] | 48 | bool ICollection<T>.IsReadOnly {
|
---|
[2572] | 49 | get { return ((ICollection<T>)list).IsReadOnly; }
|
---|
| 50 | }
|
---|
| 51 | #endregion
|
---|
| 52 |
|
---|
| 53 | #region Constructors
|
---|
| 54 | public ObservableCollection() {
|
---|
| 55 | list = new List<T>();
|
---|
| 56 | }
|
---|
| 57 | public ObservableCollection(int capacity) {
|
---|
| 58 | list = new List<T>(capacity);
|
---|
| 59 | }
|
---|
| 60 | public ObservableCollection(IEnumerable<T> collection) {
|
---|
| 61 | list = new List<T>(collection);
|
---|
| 62 | }
|
---|
[3560] | 63 | [StorableConstructor]
|
---|
| 64 | protected ObservableCollection(bool deserializing) { }
|
---|
[2572] | 65 | #endregion
|
---|
| 66 |
|
---|
| 67 | #region Access
|
---|
| 68 | public bool Contains(T item) {
|
---|
| 69 | return list.Contains(item);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | public bool Exists(Predicate<T> match) {
|
---|
| 73 | return list.Exists(match);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | public T Find(Predicate<T> match) {
|
---|
| 77 | return list.Find(match);
|
---|
| 78 | }
|
---|
| 79 | public ICollection<T> FindAll(Predicate<T> match) {
|
---|
| 80 | return list.FindAll(match);
|
---|
| 81 | }
|
---|
| 82 | public T FindLast(Predicate<T> match) {
|
---|
| 83 | return list.FindLast(match);
|
---|
| 84 | }
|
---|
| 85 | #endregion
|
---|
| 86 |
|
---|
| 87 | #region Manipulation
|
---|
| 88 | public void Add(T item) {
|
---|
[2620] | 89 | int capacity = list.Capacity;
|
---|
[2572] | 90 | list.Add(item);
|
---|
[2620] | 91 | if (list.Capacity != capacity)
|
---|
| 92 | OnPropertyChanged("Capacity");
|
---|
| 93 | OnPropertyChanged("Count");
|
---|
[2572] | 94 | OnItemsAdded(new T[] { item });
|
---|
| 95 | }
|
---|
| 96 | public void AddRange(IEnumerable<T> collection) {
|
---|
[2620] | 97 | int capacity = list.Capacity;
|
---|
| 98 | int count = list.Count;
|
---|
[2572] | 99 | list.AddRange(collection);
|
---|
[2620] | 100 | if (list.Count != count) {
|
---|
| 101 | if (list.Capacity != capacity)
|
---|
| 102 | OnPropertyChanged("Capacity");
|
---|
| 103 | OnPropertyChanged("Count");
|
---|
| 104 | OnItemsAdded(collection);
|
---|
| 105 | }
|
---|
[2572] | 106 | }
|
---|
| 107 |
|
---|
| 108 | public bool Remove(T item) {
|
---|
| 109 | if (list.Remove(item)) {
|
---|
[2620] | 110 | OnPropertyChanged("Count");
|
---|
[2572] | 111 | OnItemsRemoved(new T[] { item });
|
---|
| 112 | return true;
|
---|
| 113 | }
|
---|
| 114 | return false;
|
---|
| 115 | }
|
---|
| 116 | public void RemoveRange(IEnumerable<T> collection) {
|
---|
[2573] | 117 | if (collection == null) throw new ArgumentNullException();
|
---|
[2572] | 118 | List<T> items = new List<T>();
|
---|
| 119 | foreach (T item in collection) {
|
---|
| 120 | if (list.Remove(item))
|
---|
| 121 | items.Add(item);
|
---|
| 122 | }
|
---|
[2620] | 123 | if (items.Count > 0) {
|
---|
| 124 | OnPropertyChanged("Count");
|
---|
[2572] | 125 | OnItemsRemoved(items);
|
---|
[2620] | 126 | }
|
---|
[2572] | 127 | }
|
---|
| 128 | public int RemoveAll(Predicate<T> match) {
|
---|
| 129 | List<T> items = list.FindAll(match);
|
---|
[2620] | 130 | int result = 0;
|
---|
| 131 | if (items.Count > 0) {
|
---|
| 132 | result = list.RemoveAll(match);
|
---|
| 133 | OnPropertyChanged("Count");
|
---|
| 134 | OnItemsRemoved(items);
|
---|
| 135 | }
|
---|
[2572] | 136 | return result;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | public void Clear() {
|
---|
[2620] | 140 | if (list.Count > 0) {
|
---|
| 141 | T[] items = list.ToArray();
|
---|
| 142 | list.Clear();
|
---|
| 143 | OnPropertyChanged("Count");
|
---|
| 144 | OnCollectionReset(new T[0], items);
|
---|
| 145 | }
|
---|
[2572] | 146 | }
|
---|
| 147 | #endregion
|
---|
| 148 |
|
---|
| 149 | #region Conversion
|
---|
[2618] | 150 | public ReadOnlyObservableCollection<T> AsReadOnly() {
|
---|
| 151 | return new ReadOnlyObservableCollection<T>(this);
|
---|
[2572] | 152 | }
|
---|
| 153 | public T[] ToArray() {
|
---|
| 154 | return list.ToArray();
|
---|
| 155 | }
|
---|
| 156 | public void CopyTo(T[] array, int arrayIndex) {
|
---|
| 157 | list.CopyTo(array, arrayIndex);
|
---|
| 158 | }
|
---|
| 159 | public ICollection<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter) {
|
---|
| 160 | return list.ConvertAll<TOutput>(converter);
|
---|
| 161 | }
|
---|
| 162 | #endregion
|
---|
| 163 |
|
---|
| 164 | #region Processing
|
---|
| 165 | public void ForEach(Action<T> action) {
|
---|
| 166 | list.ForEach(action);
|
---|
| 167 | }
|
---|
| 168 | public bool TrueForAll(Predicate<T> match) {
|
---|
| 169 | return list.TrueForAll(match);
|
---|
| 170 | }
|
---|
| 171 | #endregion
|
---|
| 172 |
|
---|
| 173 | #region Enumeration
|
---|
| 174 | public IEnumerator<T> GetEnumerator() {
|
---|
| 175 | return ((IEnumerable<T>)list).GetEnumerator();
|
---|
| 176 | }
|
---|
| 177 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
| 178 | return ((IEnumerable)list).GetEnumerator();
|
---|
| 179 | }
|
---|
| 180 | #endregion
|
---|
| 181 |
|
---|
| 182 | #region Helpers
|
---|
| 183 | public void TrimExcess() {
|
---|
[2620] | 184 | int capacity = list.Capacity;
|
---|
[2572] | 185 | list.TrimExcess();
|
---|
[2620] | 186 | if (list.Capacity != capacity)
|
---|
| 187 | OnPropertyChanged("Capacity");
|
---|
[2572] | 188 | }
|
---|
| 189 | #endregion
|
---|
[2574] | 190 |
|
---|
| 191 | #region Events
|
---|
| 192 | [field: NonSerialized]
|
---|
| 193 | public event CollectionItemsChangedEventHandler<T> ItemsAdded;
|
---|
| 194 | protected virtual void OnItemsAdded(IEnumerable<T> items) {
|
---|
[3317] | 195 | CollectionItemsChangedEventHandler<T> handler = ItemsAdded;
|
---|
| 196 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
|
---|
[2574] | 197 | }
|
---|
| 198 |
|
---|
| 199 | [field: NonSerialized]
|
---|
| 200 | public event CollectionItemsChangedEventHandler<T> ItemsRemoved;
|
---|
| 201 | protected virtual void OnItemsRemoved(IEnumerable<T> items) {
|
---|
[3317] | 202 | CollectionItemsChangedEventHandler<T> handler = ItemsRemoved;
|
---|
| 203 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
|
---|
[2574] | 204 | }
|
---|
| 205 |
|
---|
| 206 | [field: NonSerialized]
|
---|
| 207 | public event CollectionItemsChangedEventHandler<T> CollectionReset;
|
---|
| 208 | protected virtual void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
|
---|
[3317] | 209 | CollectionItemsChangedEventHandler<T> handler = CollectionReset;
|
---|
| 210 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
|
---|
[2574] | 211 | }
|
---|
[2620] | 212 |
|
---|
| 213 | [field: NonSerialized]
|
---|
| 214 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
| 215 | protected virtual void OnPropertyChanged(string propertyName) {
|
---|
[3317] | 216 | PropertyChangedEventHandler handler = PropertyChanged;
|
---|
| 217 | if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
|
---|
[2620] | 218 | }
|
---|
[2574] | 219 | #endregion
|
---|
[2572] | 220 | }
|
---|
| 221 | }
|
---|