Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2950 was 2830, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

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