Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Collections/3.3/ReadOnlyObservableSet.cs @ 3390

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

Refactored HeuristicLab.Collections (#977)

File size: 5.8 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 ReadOnlyObservableSet<T> : IObservableSet<T> {
30    protected IObservableSet<T> set;
31
32    #region Properties
33    public int Count {
34      get { return set.Count; }
35    }
36    bool ICollection<T>.IsReadOnly {
37      get { return true; }
38    }
39    #endregion
40
41    #region Constructors
42    protected ReadOnlyObservableSet() { }
43    public ReadOnlyObservableSet(IObservableSet<T> set) {
44      if (set == null) throw new ArgumentNullException();
45      this.set = set;
46      RegisterEvents();
47    }
48    #endregion
49
50    #region Access
51    public bool Contains(T item) {
52      return set.Contains(item);
53    }
54
55    public bool IsProperSubsetOf(IEnumerable<T> other) {
56      return set.IsProperSubsetOf(other);
57    }
58    public bool IsProperSupersetOf(IEnumerable<T> other) {
59      return set.IsProperSupersetOf(other);
60    }
61
62    public bool IsSubsetOf(IEnumerable<T> other) {
63      return set.IsSubsetOf(other);
64    }
65    public bool IsSupersetOf(IEnumerable<T> other) {
66      return set.IsSupersetOf(other);
67    }
68
69    public bool Overlaps(IEnumerable<T> other) {
70      return set.Overlaps(other);
71    }
72
73    public bool SetEquals(IEnumerable<T> other) {
74      return set.SetEquals(other);
75    }
76    #endregion
77
78    #region Manipulation
79    bool IObservableSet<T>.Add(T item) {
80      throw new NotSupportedException();
81    }
82    void ICollection<T>.Add(T item) {
83      throw new NotSupportedException();
84    }
85
86    void IObservableSet<T>.ExceptWith(IEnumerable<T> other) {
87      throw new NotSupportedException();
88    }
89
90    void IObservableSet<T>.IntersectWith(IEnumerable<T> other) {
91      throw new NotSupportedException();
92    }
93
94    bool ICollection<T>.Remove(T item) {
95      throw new NotSupportedException();
96    }
97
98    void IObservableSet<T>.SymmetricExceptWith(IEnumerable<T> other) {
99      throw new NotSupportedException();
100    }
101
102    void IObservableSet<T>.UnionWith(IEnumerable<T> other) {
103      throw new NotSupportedException();
104    }
105
106    void ICollection<T>.Clear() {
107      throw new NotSupportedException();
108    }
109    #endregion
110
111    #region Conversion
112    public void CopyTo(T[] array, int arrayIndex) {
113      set.CopyTo(array, arrayIndex);
114    }
115    #endregion
116
117    #region Enumeration
118    public IEnumerator<T> GetEnumerator() {
119      return set.GetEnumerator();
120    }
121    IEnumerator IEnumerable.GetEnumerator() {
122      return set.GetEnumerator();
123    }
124    #endregion
125
126    #region Events
127    protected void RegisterEvents() {
128      set.ItemsAdded += new CollectionItemsChangedEventHandler<T>(set_ItemsAdded);
129      set.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(set_ItemsRemoved);
130      set.CollectionReset += new CollectionItemsChangedEventHandler<T>(set_CollectionReset);
131      set.PropertyChanged += new PropertyChangedEventHandler(set_PropertyChanged);
132    }
133
134    [field: NonSerialized]
135    public event CollectionItemsChangedEventHandler<T> ItemsAdded;
136    protected virtual void OnItemsAdded(IEnumerable<T> items) {
137      CollectionItemsChangedEventHandler<T> handler = ItemsAdded;
138      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
139    }
140
141    [field: NonSerialized]
142    public event CollectionItemsChangedEventHandler<T> ItemsRemoved;
143    protected virtual void OnItemsRemoved(IEnumerable<T> items) {
144      CollectionItemsChangedEventHandler<T> handler = ItemsRemoved;
145      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
146    }
147
148    [field: NonSerialized]
149    public event CollectionItemsChangedEventHandler<T> CollectionReset;
150    protected virtual void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
151      CollectionItemsChangedEventHandler<T> handler = CollectionReset;
152      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
153    }
154
155    [field: NonSerialized]
156    public event PropertyChangedEventHandler PropertyChanged;
157    protected virtual void OnPropertyChanged(string propertyName) {
158      PropertyChangedEventHandler handler = PropertyChanged;
159      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
160    }
161
162    private void set_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
163      OnItemsAdded(e.Items);
164    }
165    private void set_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
166      OnItemsRemoved(e.Items);
167    }
168    private void set_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
169      OnCollectionReset(e.Items, e.OldItems);
170    }
171    private void set_PropertyChanged(object sender, PropertyChangedEventArgs e) {
172      if (e.PropertyName.Equals("Count"))
173        OnPropertyChanged(e.PropertyName);
174    }
175    #endregion
176  }
177}
Note: See TracBrowser for help on using the repository browser.