Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

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