180 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.utils;
|
|
|
15 |
|
|
|
16 |
import org.openconcerto.utils.cc.BiConsumerExn;
|
|
|
17 |
|
|
|
18 |
import java.util.Iterator;
|
|
|
19 |
import java.util.LinkedList;
|
|
|
20 |
import java.util.Objects;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Allow to maintain the dispatching of events in order when a listener itself fires an event.
|
|
|
24 |
*
|
|
|
25 |
* @author sylvain
|
|
|
26 |
*
|
|
|
27 |
* @param <L> listener type.
|
|
|
28 |
* @param <E> event type.
|
|
|
29 |
* @param <X> exception type.
|
|
|
30 |
*/
|
|
|
31 |
public final class ReentrantEventDispatcher<L, E, X extends Exception> {
|
|
|
32 |
|
|
|
33 |
private final class DispatchingState extends Tuple3<Iterator<L>, BiConsumerExn<L, E, X>, E> {
|
|
|
34 |
public DispatchingState(final Iterator<L> iter, BiConsumerExn<L, E, X> callback, final E evt) {
|
|
|
35 |
super(Objects.requireNonNull(iter, "Missing iterator"), Objects.requireNonNull(callback, "Missing callback"), evt);
|
|
|
36 |
}
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
private final ThreadLocal<LinkedList<DispatchingState>> events = new ThreadLocal<LinkedList<DispatchingState>>() {
|
|
|
40 |
@Override
|
|
|
41 |
protected LinkedList<DispatchingState> initialValue() {
|
|
|
42 |
return new LinkedList<>();
|
|
|
43 |
}
|
|
|
44 |
};
|
|
|
45 |
|
|
|
46 |
private final BiConsumerExn<L, E, X> callback;
|
|
|
47 |
|
|
|
48 |
public ReentrantEventDispatcher() {
|
|
|
49 |
this(null);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
public ReentrantEventDispatcher(final BiConsumerExn<L, E, X> callback) {
|
|
|
53 |
super();
|
|
|
54 |
this.callback = callback;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
public final void fire(final Iterator<L> iter, final E evt) throws X {
|
|
|
58 |
this.fire(iter, this.callback, evt);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
public final void fire(final Iterator<L> iter, final BiConsumerExn<L, E, X> callback, final E evt) throws X {
|
|
|
62 |
this.fire(new DispatchingState(iter, callback, evt));
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
private final void fire(final DispatchingState newTuple) throws X {
|
|
|
66 |
final LinkedList<DispatchingState> linkedList = this.events.get();
|
|
|
67 |
// add new event
|
|
|
68 |
linkedList.addLast(newTuple);
|
|
|
69 |
// process all pending events
|
|
|
70 |
DispatchingState currentTuple;
|
|
|
71 |
while ((currentTuple = linkedList.peekFirst()) != null) {
|
|
|
72 |
final Iterator<L> currentIter = currentTuple.get0();
|
|
|
73 |
final BiConsumerExn<L, E, X> currentCallback = currentTuple.get1();
|
|
|
74 |
final E currentEvt = currentTuple.get2();
|
|
|
75 |
while (currentIter.hasNext()) {
|
|
|
76 |
final L l = currentIter.next();
|
|
|
77 |
currentCallback.accept(l, currentEvt);
|
|
|
78 |
}
|
|
|
79 |
// not removeFirst() since the item might have been already removed
|
|
|
80 |
linkedList.pollFirst();
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
}
|