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