Mercurial > projects > dwt2
comparison org.eclipse.core.databinding.observable/src/org/eclipse/core/databinding/observable/set/ObservableSet.d @ 95:6208d4f6a277
Added trees for databinding.beans and observable
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 21 Apr 2009 10:55:51 +0200 |
parents | |
children | b74ac5dfcc06 |
comparison
equal
deleted
inserted
replaced
94:1d37a7813832 | 95:6208d4f6a277 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2006, 2008 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Matthew Hall - bugs 208332, 245183 | |
11 *******************************************************************************/ | |
12 | |
13 package org.eclipse.core.databinding.observable.set; | |
14 | |
15 import java.util.Collection; | |
16 import java.util.Iterator; | |
17 import java.util.Set; | |
18 | |
19 import org.eclipse.core.databinding.observable.AbstractObservable; | |
20 import org.eclipse.core.databinding.observable.ObservableTracker; | |
21 import org.eclipse.core.databinding.observable.Realm; | |
22 | |
23 /** | |
24 * | |
25 * Abstract implementation of {@link IObservableSet}. | |
26 * | |
27 * <p> | |
28 * This class is thread safe. All state accessing methods must be invoked from | |
29 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing | |
30 * listeners may be invoked from any thread. | |
31 * </p> | |
32 * | |
33 * @since 1.0 | |
34 * | |
35 */ | |
36 public abstract class ObservableSet : AbstractObservable , | |
37 IObservableSet { | |
38 | |
39 protected Set wrappedSet; | |
40 | |
41 private bool stale = false; | |
42 | |
43 protected Object elementType; | |
44 | |
45 protected this(Set wrappedSet, Object elementType) { | |
46 this(Realm.getDefault(), wrappedSet, elementType); | |
47 } | |
48 | |
49 protected this(Realm realm, Set wrappedSet, Object elementType) { | |
50 super(realm); | |
51 this.wrappedSet = wrappedSet; | |
52 this.elementType = elementType; | |
53 } | |
54 | |
55 public synchronized void addSetChangeListener(ISetChangeListener listener) { | |
56 addListener(SetChangeEvent.TYPE, listener); | |
57 } | |
58 | |
59 public synchronized void removeSetChangeListener(ISetChangeListener listener) { | |
60 removeListener(SetChangeEvent.TYPE, listener); | |
61 } | |
62 | |
63 protected void fireSetChange(SetDiff diff) { | |
64 // fire general change event first | |
65 super.fireChange(); | |
66 | |
67 fireEvent(new SetChangeEvent(this, diff)); | |
68 } | |
69 | |
70 public bool contains(Object o) { | |
71 getterCalled(); | |
72 return wrappedSet.contains(o); | |
73 } | |
74 | |
75 public bool containsAll(Collection c) { | |
76 getterCalled(); | |
77 return wrappedSet.containsAll(c); | |
78 } | |
79 | |
80 public override equals_t opEquals(Object o) { | |
81 getterCalled(); | |
82 | |
83 if (o is this) | |
84 return true; | |
85 if (o is null) | |
86 return false; | |
87 if (getClass() is o.getClass()) { | |
88 return wrappedSet.equals((cast(ObservableSet) o).wrappedSet); | |
89 } | |
90 | |
91 return wrappedSet.equals(o); | |
92 } | |
93 | |
94 public override hash_t toHash() { | |
95 getterCalled(); | |
96 return wrappedSet.hashCode(); | |
97 } | |
98 | |
99 public bool isEmpty() { | |
100 getterCalled(); | |
101 return wrappedSet.isEmpty(); | |
102 } | |
103 | |
104 public Iterator iterator() { | |
105 getterCalled(); | |
106 final Iterator wrappedIterator = wrappedSet.iterator(); | |
107 return new class() Iterator { | |
108 | |
109 public void remove() { | |
110 throw new UnsupportedOperationException(); | |
111 } | |
112 | |
113 public bool hasNext() { | |
114 ObservableTracker.getterCalled(this.outer); | |
115 return wrappedIterator.hasNext(); | |
116 } | |
117 | |
118 public Object next() { | |
119 ObservableTracker.getterCalled(this.outer); | |
120 return wrappedIterator.next(); | |
121 } | |
122 }; | |
123 } | |
124 | |
125 public int size() { | |
126 getterCalled(); | |
127 return wrappedSet.size(); | |
128 } | |
129 | |
130 public Object[] toArray() { | |
131 getterCalled(); | |
132 return wrappedSet.toArray(); | |
133 } | |
134 | |
135 public Object[] toArray(Object[] a) { | |
136 getterCalled(); | |
137 return wrappedSet.toArray(a); | |
138 } | |
139 | |
140 public String toString() { | |
141 getterCalled(); | |
142 return wrappedSet.toString(); | |
143 } | |
144 | |
145 protected void getterCalled() { | |
146 ObservableTracker.getterCalled(this); | |
147 } | |
148 | |
149 public bool add(Object o) { | |
150 throw new UnsupportedOperationException(); | |
151 } | |
152 | |
153 public bool addAll(Collection c) { | |
154 throw new UnsupportedOperationException(); | |
155 } | |
156 | |
157 public bool remove(Object o) { | |
158 throw new UnsupportedOperationException(); | |
159 } | |
160 | |
161 public bool removeAll(Collection c) { | |
162 throw new UnsupportedOperationException(); | |
163 } | |
164 | |
165 public bool retainAll(Collection c) { | |
166 throw new UnsupportedOperationException(); | |
167 } | |
168 | |
169 public void clear() { | |
170 throw new UnsupportedOperationException(); | |
171 } | |
172 | |
173 /** | |
174 * @return Returns the stale state. | |
175 */ | |
176 public bool isStale() { | |
177 getterCalled(); | |
178 return stale; | |
179 } | |
180 | |
181 /** | |
182 * @param stale | |
183 * The stale state to set. This will fire a stale event if the | |
184 * given bool is true and this observable set was not already | |
185 * stale. | |
186 */ | |
187 public void setStale(bool stale) { | |
188 checkRealm(); | |
189 bool wasStale = this.stale; | |
190 this.stale = stale; | |
191 if (!wasStale && stale) { | |
192 fireStale(); | |
193 } | |
194 } | |
195 | |
196 /** | |
197 * @param wrappedSet The wrappedSet to set. | |
198 */ | |
199 protected void setWrappedSet(Set wrappedSet) { | |
200 this.wrappedSet = wrappedSet; | |
201 } | |
202 | |
203 protected void fireChange() { | |
204 throw new RuntimeException("fireChange should not be called, use fireSetChange() instead"); //$NON-NLS-1$ | |
205 } | |
206 | |
207 /* (non-Javadoc) | |
208 * @see org.eclipse.jface.provisional.databinding.observable.AbstractObservable#dispose() | |
209 */ | |
210 public synchronized void dispose() { | |
211 super.dispose(); | |
212 } | |
213 | |
214 public Object getElementType() { | |
215 return elementType; | |
216 } | |
217 } |