OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 180 Rev 182
Line 1... Line 1...
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
Line 19... Line 19...
19
import java.util.LinkedList;
19
import java.util.LinkedList;
20
import java.util.Objects;
20
import java.util.Objects;
21
 
21
 
22
/**
22
/**
23
 * Allow to maintain the dispatching of events in order when a listener itself fires an event.
23
 * Allow to maintain the dispatching of events in order when a listener itself fires an event.
-
 
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" />
24
 * 
30
 * 
25
 * @author sylvain
31
 * @author sylvain
26
 *
32
 *
27
 * @param <L> listener type.
33
 * @param <L> listener type.
28
 * @param <E> event type.
34
 * @param <E> event type.
29
 * @param <X> exception type.
35
 * @param <X> exception type.
30
 */
36
 */
31
public final class ReentrantEventDispatcher<L, E, X extends Exception> {
37
public final class ReentrantEventDispatcher<L, E, X extends Exception> {
32
 
38
 
-
 
39
    public final class DispatchingState {
-
 
40
        private final Iterator<L> iter;
33
    private final class DispatchingState extends Tuple3<Iterator<L>, BiConsumerExn<L, E, X>, E> {
41
        private final BiConsumerExn<L, E, X> callback;
-
 
42
        private final E evt;
-
 
43
 
34
        public DispatchingState(final Iterator<L> iter, BiConsumerExn<L, E, X> callback, final E evt) {
44
        private DispatchingState(final Iterator<L> iter, BiConsumerExn<L, E, X> callback, final E evt) {
-
 
45
            this.iter = Objects.requireNonNull(iter, "Missing iterator");
35
            super(Objects.requireNonNull(iter, "Missing iterator"), Objects.requireNonNull(callback, "Missing callback"), evt);
46
            this.callback = Objects.requireNonNull(callback, "Missing callback");
-
 
47
            this.evt = evt;
-
 
48
        }
-
 
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;
36
        }
60
        }
37
    }
61
    }
38
 
62
 
39
    private final ThreadLocal<LinkedList<DispatchingState>> events = new ThreadLocal<LinkedList<DispatchingState>>() {
63
    private final ThreadLocal<LinkedList<DispatchingState>> events = new ThreadLocal<LinkedList<DispatchingState>>() {
40
        @Override
64
        @Override
Line 53... Line 77...
53
        super();
77
        super();
54
        this.callback = callback;
78
        this.callback = callback;
55
    }
79
    }
56
 
80
 
57
    public final void fire(final Iterator<L> iter, final E evt) throws X {
81
    public final void fire(final Iterator<L> iter, final E evt) throws X {
58
        this.fire(iter, this.callback, evt);
82
        this.fire(this.createDispatchingState(iter, evt));
59
    }
83
    }
60
 
84
 
61
    public final void fire(final Iterator<L> iter, final BiConsumerExn<L, E, X> callback, final E evt) throws X {
85
    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));
86
        this.fire(this.createDispatchingState(iter, callback, evt));
-
 
87
    }
-
 
88
 
-
 
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);
63
    }
95
    }
64
 
96
 
65
    private final void fire(final DispatchingState newTuple) throws X {
97
    public final void fire(final DispatchingState newTuple) throws X {
66
        final LinkedList<DispatchingState> linkedList = this.events.get();
98
        final LinkedList<DispatchingState> linkedList = this.events.get();
67
        // add new event
99
        // add new event
68
        linkedList.addLast(newTuple);
100
        linkedList.addLast(newTuple);
69
        // process all pending events
101
        // process all pending events
70
        DispatchingState currentTuple;
102
        DispatchingState currentTuple;
71
        while ((currentTuple = linkedList.peekFirst()) != null) {
103
        while ((currentTuple = linkedList.peekFirst()) != null) {
72
            final Iterator<L> currentIter = currentTuple.get0();
104
            final Iterator<L> currentIter = currentTuple.getIterator();
73
            final BiConsumerExn<L, E, X> currentCallback = currentTuple.get1();
105
            final BiConsumerExn<L, E, X> currentCallback = currentTuple.getCallback();
74
            final E currentEvt = currentTuple.get2();
106
            final E currentEvt = currentTuple.getEvt();
75
            while (currentIter.hasNext()) {
107
            while (currentIter.hasNext()) {
76
                final L l = currentIter.next();
108
                final L l = currentIter.next();
77
                currentCallback.accept(l, currentEvt);
109
                currentCallback.accept(l, currentEvt);
78
            }
110
            }
-
 
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
             */
79
            // not removeFirst() since the item might have been already removed
117
            // not removeFirst() since the item might have been already removed
80
            linkedList.pollFirst();
118
            linkedList.pollFirst();
81
        }
119
        }
82
    }
120
    }
83
 
121
 
-
 
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
    }
84
}
134
}