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