Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added new plugin HeuristicLab.Collections which contains generic observable collections (#819)

File size: 4.3 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> : CollectionChangedEventsBase<T>, ICollection<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    }
59    #endregion
60
61    #region Access
62    public bool Contains(T item) {
63      return list.Contains(item);
64    }
65
66    public bool Exists(Predicate<T> match) {
67      return list.Exists(match);
68    }
69
70    public T Find(Predicate<T> match) {
71      return list.Find(match);
72    }
73    public ICollection<T> FindAll(Predicate<T> match) {
74      return list.FindAll(match);
75    }
76    public T FindLast(Predicate<T> match) {
77      return list.FindLast(match);
78    }
79    #endregion
80
81    #region Manipulation
82    public void Add(T item) {
83      list.Add(item);
84      OnItemsAdded(new T[] { item });
85    }
86    public void AddRange(IEnumerable<T> collection) {
87      list.AddRange(collection);
88      OnItemsAdded(collection);
89    }
90
91    public bool Remove(T item) {
92      if (list.Remove(item)) {
93        OnItemsRemoved(new T[] { item });
94        return true;
95      }
96      return false;
97    }
98    public void RemoveRange(IEnumerable<T> collection) {
99      List<T> items = new List<T>();
100      foreach (T item in collection) {
101        if (list.Remove(item))
102          items.Add(item);
103      }
104      if (items.Count > 0)
105        OnItemsRemoved(items);
106    }
107    public int RemoveAll(Predicate<T> match) {
108      List<T> items = list.FindAll(match);
109      int result = list.RemoveAll(match);
110      OnItemsRemoved(items);
111      return result;
112    }
113
114    public void Clear() {
115      T[] items = list.ToArray();
116      list.Clear();
117      OnCollectionReset(new T[0], items);
118    }
119    #endregion
120
121    #region Conversion
122    public ReadOnlyCollection<T> AsReadOnly() {
123      return list.AsReadOnly();
124    }
125    public T[] ToArray() {
126      return list.ToArray();
127    }
128    public void CopyTo(T[] array, int arrayIndex) {
129      list.CopyTo(array, arrayIndex);
130    }
131    public ICollection<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter) {
132      return list.ConvertAll<TOutput>(converter);
133    }
134    #endregion
135
136    #region Processing
137    public void ForEach(Action<T> action) {
138      list.ForEach(action);
139    }
140    public bool TrueForAll(Predicate<T> match) {
141      return list.TrueForAll(match);
142    }
143    #endregion
144
145    #region Enumeration
146    public IEnumerator<T> GetEnumerator() {
147      return ((IEnumerable<T>)list).GetEnumerator();
148    }
149    IEnumerator IEnumerable.GetEnumerator() {
150      return ((IEnumerable)list).GetEnumerator();
151    }
152    #endregion
153
154    #region Helpers
155    public void TrimExcess() {
156      list.TrimExcess();
157    }
158    #endregion
159  }
160}
Note: See TracBrowser for help on using the repository browser.