comparison org.eclipse.core.databinding/src/org/eclipse/core/databinding/observable/set/AbstractObservableSet.d @ 78:0a55d2d5a946

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