Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3016 was 3002, checked in by epitzer, 15 years ago

Add missing constructors and hooks (#548)

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