Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gteufl/HeuristicLab.Collections/3.3/ObservableCollection.cs @ 11641

Last change on this file since 11641 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.ComponentModel;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Collections {
29  [StorableClass]
30  [Serializable]
31  public class ObservableCollection<T> : IObservableCollection<T> {
32    [Storable]
33    protected List<T> list;
34
35    #region Properties
36    public int Capacity {
37      get { return list.Capacity; }
38      set {
39        if (list.Capacity != value) {
40          list.Capacity = value;
41          OnPropertyChanged("Capacity");
42        }
43      }
44    }
45    public int Count {
46      get { return list.Count; }
47    }
48    bool ICollection<T>.IsReadOnly {
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    }
63    [StorableConstructor]
64    protected ObservableCollection(bool deserializing) { }
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) {
89      int capacity = list.Capacity;
90      list.Add(item);
91      if (list.Capacity != capacity)
92        OnPropertyChanged("Capacity");
93      OnPropertyChanged("Count");
94      OnItemsAdded(new T[] { item });
95    }
96    public void AddRange(IEnumerable<T> collection) {
97      int capacity = list.Capacity;
98      int count = list.Count;
99      list.AddRange(collection);
100      if (list.Count != count) {
101        if (list.Capacity != capacity)
102          OnPropertyChanged("Capacity");
103        OnPropertyChanged("Count");
104        OnItemsAdded(collection);
105      }
106    }
107
108    public bool Remove(T item) {
109      if (list.Remove(item)) {
110        OnPropertyChanged("Count");
111        OnItemsRemoved(new T[] { item });
112        return true;
113      }
114      return false;
115    }
116    public void RemoveRange(IEnumerable<T> collection) {
117      if (collection == null) throw new ArgumentNullException();
118      List<T> items = new List<T>();
119      foreach (T item in collection) {
120        if (list.Remove(item))
121          items.Add(item);
122      }
123      if (items.Count > 0) {
124        OnPropertyChanged("Count");
125        OnItemsRemoved(items);
126      }
127    }
128    public int RemoveAll(Predicate<T> match) {
129      List<T> items = list.FindAll(match);
130      int result = 0;
131      if (items.Count > 0) {
132        result = list.RemoveAll(match);
133        OnPropertyChanged("Count");
134        OnItemsRemoved(items);
135      }
136      return result;
137    }
138
139    public void Clear() {
140      if (list.Count > 0) {
141        T[] items = list.ToArray();
142        list.Clear();
143        OnPropertyChanged("Count");
144        OnCollectionReset(new T[0], items);
145      }
146    }
147    #endregion
148
149    #region Conversion
150    public ReadOnlyObservableCollection<T> AsReadOnly() {
151      return new ReadOnlyObservableCollection<T>(this);
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() {
184      int capacity = list.Capacity;
185      list.TrimExcess();
186      if (list.Capacity != capacity)
187        OnPropertyChanged("Capacity");
188    }
189    #endregion
190
191    #region Events
192    [field: NonSerialized]
193    public event CollectionItemsChangedEventHandler<T> ItemsAdded;
194    protected virtual void OnItemsAdded(IEnumerable<T> items) {
195      CollectionItemsChangedEventHandler<T> handler = ItemsAdded;
196      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
197    }
198
199    [field: NonSerialized]
200    public event CollectionItemsChangedEventHandler<T> ItemsRemoved;
201    protected virtual void OnItemsRemoved(IEnumerable<T> items) {
202      CollectionItemsChangedEventHandler<T> handler = ItemsRemoved;
203      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
204    }
205
206    [field: NonSerialized]
207    public event CollectionItemsChangedEventHandler<T> CollectionReset;
208    protected virtual void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
209      CollectionItemsChangedEventHandler<T> handler = CollectionReset;
210      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
211    }
212
213    [field: NonSerialized]
214    public event PropertyChangedEventHandler PropertyChanged;
215    protected virtual void OnPropertyChanged(string propertyName) {
216      PropertyChangedEventHandler handler = PropertyChanged;
217      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
218    }
219    #endregion
220  }
221}
Note: See TracBrowser for help on using the repository browser.