Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3727 was 3560, checked in by swagner, 15 years ago

Fixed persistence exceptions by restoring the reference on HeuristicLab.Persistence in HeuristicLab.Collections (#977)

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