78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2005, 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 * Brad Reynolds - bug 116920
|
|
11 * Brad Reynolds - bug 147515
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.core.databinding.observable.value.ComputedValue;
|
81
|
14 import org.eclipse.core.databinding.observable.value.ValueDiff;
|
|
15 import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
|
|
16 import org.eclipse.core.databinding.observable.value.IValueChangeListener;
|
78
|
17
|
|
18 import java.lang.all;
|
|
19
|
|
20 import org.eclipse.core.databinding.observable.ChangeEvent;
|
|
21 import org.eclipse.core.databinding.observable.IChangeListener;
|
|
22 import org.eclipse.core.databinding.observable.IObservable;
|
|
23 import org.eclipse.core.databinding.observable.IStaleListener;
|
|
24 import org.eclipse.core.databinding.observable.ObservableTracker;
|
|
25 import org.eclipse.core.databinding.observable.Realm;
|
|
26 import org.eclipse.core.databinding.observable.StaleEvent;
|
|
27
|
|
28 /**
|
|
29 * A Lazily calculated value that automatically computes and registers listeners
|
|
30 * on its dependencies as long as all of its dependencies are IObservable
|
|
31 * objects
|
|
32 * <p>
|
|
33 * This class is thread safe. All state accessing methods must be invoked from
|
|
34 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
|
|
35 * listeners may be invoked from any thread.
|
|
36 * </p>
|
|
37 *
|
|
38 * @since 1.0
|
|
39 */
|
|
40 public abstract class ComputedValue : AbstractObservableValue {
|
|
41
|
|
42 private bool dirty = true;
|
|
43
|
|
44 private bool stale = false;
|
|
45
|
|
46 private Object cachedValue = null;
|
|
47
|
|
48 /**
|
|
49 * Array of observables this computed value depends on. This field has a
|
|
50 * value of <code>null</code> if we are not currently listening.
|
|
51 */
|
|
52 private IObservable[] dependencies = null;
|
|
53
|
|
54 /**
|
|
55 *
|
|
56 */
|
|
57 public this() {
|
|
58 this(Realm.getDefault(), null);
|
|
59 }
|
|
60
|
|
61 /**
|
|
62 * @param valueType
|
|
63 * can be <code>null</code>
|
|
64 */
|
|
65 public this(Object valueType) {
|
|
66 this(Realm.getDefault(), valueType);
|
|
67 }
|
|
68
|
|
69 /**
|
|
70 * @param realm
|
|
71 *
|
|
72 */
|
|
73 public this(Realm realm) {
|
|
74 this(realm, null);
|
|
75 }
|
|
76
|
|
77 /**
|
|
78 * @param realm
|
|
79 * @param valueType
|
|
80 */
|
|
81 public this(Realm realm, Object valueType) {
|
85
|
82 privateInterface = new PrivateInterface();
|
78
|
83 super(realm);
|
|
84 this.valueType = valueType;
|
|
85 }
|
|
86
|
|
87 /**
|
|
88 * Inner class that implements interfaces that we don't want to expose as
|
|
89 * public API. Each interface could have been implemented using a separate
|
|
90 * anonymous class, but we combine them here to reduce the memory overhead
|
|
91 * and number of classes.
|
|
92 *
|
|
93 * <p>
|
|
94 * The Runnable calls computeValue and stores the result in cachedValue.
|
|
95 * </p>
|
|
96 *
|
|
97 * <p>
|
|
98 * The IChangeListener stores each observable in the dependencies list. This
|
|
99 * is registered as the listener when calling ObservableTracker, to detect
|
|
100 * every observable that is used by computeValue.
|
|
101 * </p>
|
|
102 *
|
|
103 * <p>
|
|
104 * The IChangeListener is attached to every dependency.
|
|
105 * </p>
|
|
106 *
|
|
107 */
|
|
108 private class PrivateInterface : Runnable, IChangeListener,
|
|
109 IStaleListener {
|
|
110 public void run() {
|
|
111 cachedValue = calculate();
|
|
112 }
|
|
113
|
|
114 public void handleStale(StaleEvent event) {
|
|
115 if (!dirty && !stale) {
|
|
116 stale = true;
|
|
117 fireStale();
|
|
118 }
|
|
119 }
|
|
120
|
|
121 public void handleChange(ChangeEvent event) {
|
|
122 makeDirty();
|
|
123 }
|
|
124 }
|
|
125
|
85
|
126 private PrivateInterface privateInterface;
|
78
|
127
|
|
128 private Object valueType;
|
|
129
|
|
130 protected final Object doGetValue() {
|
|
131 if (dirty) {
|
|
132 // This line will do the following:
|
|
133 // - Run the calculate method
|
|
134 // - While doing so, add any observable that is touched to the
|
|
135 // dependencies list
|
|
136 IObservable[] newDependencies = ObservableTracker.runAndMonitor(
|
|
137 privateInterface, privateInterface, null);
|
|
138
|
|
139 stale = false;
|
|
140 for (int i = 0; i < newDependencies.length; i++) {
|
|
141 IObservable observable = newDependencies[i];
|
|
142 // Add a change listener to the new dependency.
|
|
143 if (observable.isStale()) {
|
|
144 stale = true;
|
|
145 } else {
|
|
146 observable.addStaleListener(privateInterface);
|
|
147 }
|
|
148 }
|
|
149
|
|
150 dependencies = newDependencies;
|
|
151
|
|
152 dirty = false;
|
|
153 }
|
|
154
|
|
155 return cachedValue;
|
|
156 }
|
|
157
|
|
158 /**
|
|
159 * Subclasses must override this method to provide the object's value.
|
|
160 *
|
|
161 * @return the object's value
|
|
162 */
|
|
163 protected abstract Object calculate();
|
|
164
|
|
165 protected final void makeDirty() {
|
|
166 if (!dirty) {
|
|
167 dirty = true;
|
|
168
|
|
169 stopListening();
|
|
170
|
|
171 // copy the old value
|
|
172 final Object oldValue = cachedValue;
|
|
173 // Fire the "dirty" event. This implementation recomputes the new
|
|
174 // value lazily.
|
|
175 fireValueChange(new class() ValueDiff {
|
|
176
|
|
177 public Object getOldValue() {
|
|
178 return oldValue;
|
|
179 }
|
|
180
|
|
181 public Object getNewValue() {
|
|
182 return getValue();
|
|
183 }
|
|
184 });
|
|
185 }
|
|
186 }
|
|
187
|
|
188 /**
|
|
189 *
|
|
190 */
|
|
191 private void stopListening() {
|
|
192 // Stop listening for dependency changes.
|
|
193 if (dependencies !is null) {
|
|
194 for (int i = 0; i < dependencies.length; i++) {
|
|
195 IObservable observable = dependencies[i];
|
|
196
|
|
197 observable.removeChangeListener(privateInterface);
|
|
198 observable.removeStaleListener(privateInterface);
|
|
199 }
|
|
200 dependencies = null;
|
|
201 }
|
|
202 }
|
|
203
|
|
204 public bool isStale() {
|
|
205 // we need to recompute, otherwise staleness wouldn't mean anything
|
|
206 getValue();
|
|
207 return stale;
|
|
208 }
|
|
209
|
|
210 public Object getValueType() {
|
|
211 return valueType;
|
|
212 }
|
|
213
|
|
214 // this method exists here so that we can call it from the runnable below.
|
|
215 /**
|
|
216 * @since 1.1
|
|
217 */
|
|
218 protected bool hasListeners() {
|
|
219 return super.hasListeners();
|
|
220 }
|
|
221
|
|
222 public synchronized void addChangeListener(IChangeListener listener) {
|
|
223 super.addChangeListener(listener);
|
|
224 // If somebody is listening, we need to make sure we attach our own
|
|
225 // listeners
|
|
226 computeValueForListeners();
|
|
227 }
|
|
228
|
|
229 /**
|
|
230 * Some clients just add a listener and expect to get notified even if they
|
|
231 * never called getValue(), so we have to call getValue() ourselves here to
|
|
232 * be sure. Need to be careful about realms though, this method can be
|
|
233 * called outside of our realm. See also bug 198211. If a client calls this
|
|
234 * outside of our realm, they may receive change notifications before the
|
|
235 * runnable below has been executed. It is their job to figure out what to
|
|
236 * do with those notifications.
|
|
237 */
|
|
238 private void computeValueForListeners() {
|
|
239 getRealm().exec(new class() Runnable {
|
|
240 public void run() {
|
|
241 if (dependencies is null) {
|
|
242 // We are not currently listening.
|
|
243 if (hasListeners()) {
|
|
244 // But someone is listening for changes. Call getValue()
|
|
245 // to make sure we start listening to the observables we
|
|
246 // depend on.
|
|
247 getValue();
|
|
248 }
|
|
249 }
|
|
250 }
|
|
251 });
|
|
252 }
|
|
253
|
|
254 public synchronized void addValueChangeListener(
|
|
255 IValueChangeListener listener) {
|
|
256 super.addValueChangeListener(listener);
|
|
257 // If somebody is listening, we need to make sure we attach our own
|
|
258 // listeners
|
|
259 computeValueForListeners();
|
|
260 }
|
|
261
|
|
262 public synchronized void dispose() {
|
|
263 super.dispose();
|
|
264 stopListening();
|
|
265 }
|
|
266
|
|
267 }
|