Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2599 was 2575, checked in by swagner, 15 years ago

Implemented IDisposable and unified extension capabilities for all observable collections (#819)

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