Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Collections/3.3/ObservableCollection.cs @ 3491

Last change on this file since 3491 was 3390, checked in by swagner, 14 years ago

Refactored HeuristicLab.Collections (#977)

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