1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Intersect Software Corporation
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.control;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import net.wotonomy.foundation.NSRunLoop;
import net.wotonomy.foundation.NSSelector;
/**
* EODelayedObserverQueue allows EODelayedObservers to receive only one
* subjectChanged() message after numerous willChange() messages have been sent.
* Observers are then notified in order of their priority property, so that
* certain observers can be notified before others for whatever
* application-specific purpose. This class is not thread-safe and should be
* used only for single-threaded GUI clients (AWT and Swing). <br>
* <br>
*
* Important note: because AWT's event queue does not allow for priority-based
* scheduling, this class installs a custom event queue, replacing the existing
* queue on the AWT dispatch thread. We know of no way around this problem. <br>
* <br>
*
* Implementation note: this queue relies on the result of equals() for
* maintaining a set of objects on the queue. If two EODelayedObservers evaluate
* to the same value using equals(), only one of them will exist on the queue.
* If this, starts to suck, we can change it.
*
* @author michael@mpowers.net
* @author $Author: cgruber $
* @version $Revision: 894 $
*/
public class EODelayedObserverQueue {
/**
* The default run loop ordering flushes the delayed observers up to
* ObserverPrioritySixth before dispatching the AWT event queue.
* ObserverPriorityLater is run last.
*/
public static int FlushDelayedObserversRunLoopOrdering = 400000;
private static EODelayedObserverQueue defaultObserverQueue = null;
private static NSSelector runLaterSelector = new NSSelector("flushObserverQueue", new Class[] { Object.class });
private boolean willRunLater;
private LinkedList priorityQueue;
/**
* Default constructor.
*/
public EODelayedObserverQueue() {
willRunLater = false;
priorityQueue = new LinkedList();
}
/**
* Returns the system default observer queue.
*/
public static EODelayedObserverQueue defaultObserverQueue() {
if (defaultObserverQueue == null) {
defaultObserverQueue = new EODelayedObserverQueue();
}
return defaultObserverQueue;
}
/**
* Removes the specified observer from the queue.
*/
public void dequeueObserver(EODelayedObserver anObserver) {
//System.out.println( "dequeueObserver: " + anObserver );
// synchronized ( priorityQueue )
// {
priorityQueue.remove(anObserver);
// }
}
/**
* Adds the specified observer to the queue. An already enqueued observer will
* not be added again. If the observer's priority is ObserverPriorityImmediate,
* it will be notified immediately and not added to the queue. Otherwise, the
* queue sets itself up to call notifyObserversUpToPriority during the run loop
* as specified by FlushDelayedObserversRunLoopOrdering.
*/
public void enqueueObserver(EODelayedObserver anObserver) {
// syntactic glue for Runnables
final EODelayedObserver observer = anObserver;
if (observer.priority() == EODelayedObserver.ObserverPriorityImmediate) {
// invoke immediately
observer.subjectChanged();
} else {
// place in the delayed observer queue
// synchronized ( priorityQueue )
// {
int i = 0;
int priority = observer.priority();
Object o;
Iterator iterator = priorityQueue.iterator();
// scan entire list to ensure we're not already queued
while (iterator.hasNext()) {
o = iterator.next();
if (o == observer) {
// already queued
return;
}
if (((EODelayedObserver) o).priority() > priority) {
// insert at this index: break now
break;
}
i++;
}
// if we broke early, we found a threshhold:
// continue scanning to ensure we're not already queued
while (iterator.hasNext()) {
if (iterator.next() == observer) {
// already queued
return;
}
}
// insert before items of lower priority,
// otherwise insert at end of list.
priorityQueue.add(i, observer);
// }
runLater();
}
//System.out.println( "enqueueObserver: " + anObserver + " : " + priorityQueue );
}
/**
* Notifies all observers with priority equal to or greater than the specified
* priority.
*/
public void notifyObserversUpToPriority(int priority) {
//System.out.println( "notifyObserversUpToPriority: priorityQueue size = " + priorityQueue.size() );
EODelayedObserver o;
while (!priorityQueue.isEmpty()) {
o = (EODelayedObserver) priorityQueue.getFirst();
if (o.priority() > priority)
break;
priorityQueue.removeFirst();
try {
o.subjectChanged();
} catch (Exception exc) {
System.out.println("Error notifying observer: " + o);
exc.printStackTrace();
}
}
}
/**
* Called to ensure that notifyObserversUpToPriority will be called on the next
* event loop.
*/
private void runLater() {
if (!willRunLater) {
willRunLater = true;
NSRunLoop.currentRunLoop().performSelectorWithOrder(runLaterSelector, this, null,
FlushDelayedObserversRunLoopOrdering, null);
}
}
/**
* This method is called by the event queue run loop and calls
* notifyObserversUpToPriority with ObserverPriorityLater. NOTE: This method is
* not part of the specification.
*/
public void flushObserverQueue(Object anObject) {
//System.out.println( "EODelayedObserverQueue: running" );
notifyObserversUpToPriority(EODelayedObserver.ObserverPrioritySixth);
if (!priorityQueue.isEmpty()) {
// assumes all remaining on queue are ObserverPriorityLater
NSRunLoop.invokeLater(new PriorityLaterRunnable(new LinkedList(priorityQueue)));
priorityQueue.clear();
}
willRunLater = false;
}
/**
* A runnable for dispatching remaining observers running at
* ObserverPriorityLater.
*/
class PriorityLaterRunnable implements Runnable {
List observers;
public PriorityLaterRunnable(List anObserverList) {
observers = anObserverList;
}
public void run() {
EODelayedObserver o = null;
Iterator i = observers.iterator();
while (i.hasNext()) {
try {
o = (EODelayedObserver) i.next();
o.subjectChanged();
} catch (Exception exc) {
System.out.println("Error notifying observer: " + o);
exc.printStackTrace();
}
}
}
}
}
/*
* $Log$ Revision 1.2 2006/02/16 16:47:14 cgruber Move some classes in to
* "internal" packages and re-work imports, etc.
*
* Also use UnsupportedOperationExceptions where appropriate, instead of
* WotonomyExceptions.
*
* Revision 1.1 2006/02/16 13:19:57 cgruber Check in all sources in
* eclipse-friendly maven-enabled packages.
*
* Revision 1.8 2003/08/19 01:53:12 chochos EOObjectStore had some incompatible
* return types (Object instead of EOEnterpriseObject, in fault methods mostly).
* It's internally consistent but I hope it doesn't break anything based on
* this, even though fault methods mostly throw exceptions for now.
*
* Revision 1.7 2002/05/20 15:08:35 mpowers Optimization for enqueueObserver: we
* were scanning the entire list anyway; now we compare priorities and ensure
* we're not double-queued on same pass.
*
* Revision 1.6 2002/05/15 13:45:57 mpowers RunLater now appropriately runs
* later: at the end of the current awt queue.
*
* Revision 1.5 2002/03/11 03:18:39 mpowers Now properly handling
* ObserverChangesLater.
*
* Revision 1.4 2001/10/26 18:37:15 mpowers Now using NSRunLoop instead of AWT
* EventQueue.
*
* Revision 1.3 2001/10/22 21:54:16 mpowers Removed swing dependency in favor of
* jdk1.3 event queue. Optimized priority queue population.
*
* Revision 1.2 2001/10/12 18:01:59 mpowers Now catching exceptions before they
* disrupt the awt event queue.
*
* Revision 1.1.1.1 2000/12/21 15:46:42 mpowers Contributing wotonomy.
*
* Revision 1.5 2000/12/20 16:25:35 michael Added log to all files.
*
*
*/
|