Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Common/3.2/ObservableCollection.cs @ 2345

Last change on this file since 2345 was 2323, checked in by mkommend, 15 years ago

added ObservableCollection and EnumerableEventArgs to HeuristicLab.Common (ticket #732)

File size: 3.5 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
21using System;
22using System.Collections.Generic;
23using System.Linq;
24using System.Text;
25
26namespace HeuristicLab.Common {
27  public class ObservableCollection<T> : ICollection<T> {
28    private List<T> list;
29
30    public ObservableCollection() {
31      list = new List<T>();
32    }
33
34    #region Events
35    public event EventHandler CollectionCleared;
36    protected void FireCollectionCleared() {
37      OnCollectionCleared();
38    }
39    protected virtual void OnCollectionCleared() {
40      if (CollectionCleared != null)
41        CollectionCleared(this, new EventArgs());
42    }
43
44    public event EventHandler<EnumerableEventArgs<T>> ItemsAdded;
45    protected void FireItemsAdded(IEnumerable<T> addedItems) {
46      OnItemsAdded(addedItems);
47    }
48    protected virtual void OnItemsAdded(IEnumerable<T> addedItems) {
49      if (ItemsAdded != null)
50        ItemsAdded(this, new EnumerableEventArgs<T>(addedItems));
51    }
52
53    public event EventHandler<EnumerableEventArgs<T>> ItemsRemoved;
54    protected void FireItemsRemoved(IEnumerable<T> removedItems) {
55      OnItemsRemoved(removedItems);
56    }
57    protected virtual void OnItemsRemoved(IEnumerable<T> removedItems) {
58      if (ItemsRemoved != null)
59        ItemsRemoved(this, new EnumerableEventArgs<T>(removedItems));
60    }
61    #endregion
62
63    #region ICollection<T> Members
64    public void Add(T item) {
65      this.list.Add(item);
66      FireItemsAdded(new List<T> { item });
67    }
68
69    public void AddRange(IEnumerable<T> items) {
70      this.list.AddRange(items);
71      FireItemsAdded(items);
72    }
73
74    public void Clear() {
75      this.list.Clear();
76      FireCollectionCleared();
77    }
78
79    public bool Contains(T item) {
80      return this.list.Contains(item);
81    }
82
83    public void CopyTo(T[] array, int arrayIndex) {
84      this.list.CopyTo(array, arrayIndex);
85    }
86
87    public int Count {
88      get { return this.list.Count; }
89    }
90
91    public bool IsReadOnly {
92      get { return false; }
93    }
94
95    public bool Remove(T item) {
96      bool ret = this.list.Remove(item);
97      if (ret)
98        FireItemsRemoved(new List<T> { item });
99      return ret;
100    }
101
102    public void RemoveRange(IEnumerable<T> items) {
103      bool ret = false;
104      foreach (T item in items)
105        ret |= list.Remove(item);
106      if (ret)
107        FireItemsRemoved(items);
108    }
109
110    #endregion
111
112    #region IEnumerable<T> Members
113
114    public IEnumerator<T> GetEnumerator() {
115      return list.GetEnumerator();
116    }
117
118    #endregion
119
120    #region IEnumerable Members
121
122    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
123      return list.GetEnumerator();
124    }
125
126    #endregion
127  }
128}
Note: See TracBrowser for help on using the repository browser.