180 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
182 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
180 |
ilm |
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.
|
182 |
ilm |
24 |
* <p>
|
|
|
25 |
* If each notification simply goes through a list of listeners, the events are delivered out of
|
|
|
26 |
* order : <img src="doc-files/reentrantEventsNaive.png" />
|
|
|
27 |
* <p>
|
|
|
28 |
* This class adds new notifications at the end of a list, but resume notifying from the start of
|
|
|
29 |
* the list. <img src="doc-files/reentrantEventsIn-order.png" />
|
180 |
ilm |
30 |
*
|
|
|
31 |
* @author sylvain
|
|
|
32 |
*
|
|
|
33 |
* @param <L> listener type.
|
|
|
34 |
* @param <E> event type.
|
|
|
35 |
* @param <X> exception type.
|
|
|
36 |
*/
|
|
|
37 |
public final class ReentrantEventDispatcher<L, E, X extends Exception> {
|
|
|
38 |
|
182 |
ilm |
39 |
public final class DispatchingState {
|
|
|
40 |
private final Iterator<L> iter;
|
|
|
41 |
private final BiConsumerExn<L, E, X> callback;
|
|
|
42 |
private final E evt;
|
|
|
43 |
|
|
|
44 |
private DispatchingState(final Iterator<L> iter, BiConsumerExn<L, E, X> callback, final E evt) {
|
|
|
45 |
this.iter = Objects.requireNonNull(iter, "Missing iterator");
|
|
|
46 |
this.callback = Objects.requireNonNull(callback, "Missing callback");
|
|
|
47 |
this.evt = evt;
|
180 |
ilm |
48 |
}
|
182 |
ilm |
49 |
|
|
|
50 |
private final Iterator<L> getIterator() {
|
|
|
51 |
return this.iter;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
private final BiConsumerExn<L, E, X> getCallback() {
|
|
|
55 |
return this.callback;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
public final E getEvt() {
|
|
|
59 |
return this.evt;
|
|
|
60 |
}
|
180 |
ilm |
61 |
}
|
|
|
62 |
|
|
|
63 |
private final ThreadLocal<LinkedList<DispatchingState>> events = new ThreadLocal<LinkedList<DispatchingState>>() {
|
|
|
64 |
@Override
|
|
|
65 |
protected LinkedList<DispatchingState> initialValue() {
|
|
|
66 |
return new LinkedList<>();
|
|
|
67 |
}
|
|
|
68 |
};
|
|
|
69 |
|
|
|
70 |
private final BiConsumerExn<L, E, X> callback;
|
|
|
71 |
|
|
|
72 |
public ReentrantEventDispatcher() {
|
|
|
73 |
this(null);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
public ReentrantEventDispatcher(final BiConsumerExn<L, E, X> callback) {
|
|
|
77 |
super();
|
|
|
78 |
this.callback = callback;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
public final void fire(final Iterator<L> iter, final E evt) throws X {
|
182 |
ilm |
82 |
this.fire(this.createDispatchingState(iter, evt));
|
180 |
ilm |
83 |
}
|
|
|
84 |
|
|
|
85 |
public final void fire(final Iterator<L> iter, final BiConsumerExn<L, E, X> callback, final E evt) throws X {
|
182 |
ilm |
86 |
this.fire(this.createDispatchingState(iter, callback, evt));
|
180 |
ilm |
87 |
}
|
|
|
88 |
|
182 |
ilm |
89 |
public final DispatchingState createDispatchingState(final Iterator<L> iter, final E evt) {
|
|
|
90 |
return this.createDispatchingState(iter, this.callback, evt);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
public final DispatchingState createDispatchingState(final Iterator<L> iter, final BiConsumerExn<L, E, X> callback, final E evt) {
|
|
|
94 |
return new DispatchingState(iter, callback, evt);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
public final void fire(final DispatchingState newTuple) throws X {
|
180 |
ilm |
98 |
final LinkedList<DispatchingState> linkedList = this.events.get();
|
|
|
99 |
// add new event
|
|
|
100 |
linkedList.addLast(newTuple);
|
|
|
101 |
// process all pending events
|
|
|
102 |
DispatchingState currentTuple;
|
|
|
103 |
while ((currentTuple = linkedList.peekFirst()) != null) {
|
182 |
ilm |
104 |
final Iterator<L> currentIter = currentTuple.getIterator();
|
|
|
105 |
final BiConsumerExn<L, E, X> currentCallback = currentTuple.getCallback();
|
|
|
106 |
final E currentEvt = currentTuple.getEvt();
|
180 |
ilm |
107 |
while (currentIter.hasNext()) {
|
|
|
108 |
final L l = currentIter.next();
|
|
|
109 |
currentCallback.accept(l, currentEvt);
|
|
|
110 |
}
|
182 |
ilm |
111 |
/*
|
|
|
112 |
* It isn't because one callback failed that the event itself didn't happen or should be
|
|
|
113 |
* reverted : we should still notify the other callbacks. So don't use a finally block
|
|
|
114 |
* to remove currentTuple. But if the event should indeed be reverted (e.g. the callback
|
|
|
115 |
* was a check), #remove(DispatchingState) should be called.
|
|
|
116 |
*/
|
180 |
ilm |
117 |
// not removeFirst() since the item might have been already removed
|
|
|
118 |
linkedList.pollFirst();
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
|
182 |
ilm |
122 |
/**
|
|
|
123 |
* Remove a dispatching state. Useful because if there's an exception while notifying the
|
|
|
124 |
* listeners, then the next {@link #fire(DispatchingState)} in the same thread will by default
|
|
|
125 |
* resume notifying the listeners. That behaviour is not valid for a reverted DB transaction.
|
|
|
126 |
*
|
|
|
127 |
* @param dispatchingState what to remove.
|
|
|
128 |
* @return {@code true} if the item was actually removed.
|
|
|
129 |
* @see #createDispatchingState(Iterator, Object)
|
|
|
130 |
*/
|
|
|
131 |
public final boolean remove(DispatchingState dispatchingState) {
|
|
|
132 |
return this.events.get().remove(dispatchingState);
|
|
|
133 |
}
|
180 |
ilm |
134 |
}
|