Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3287 was 3019, checked in by swagner, 15 years ago

Restructured persistence code of read-only observable collections (#548)

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