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