OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.utils;

import org.openconcerto.utils.cc.BiConsumerExn;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Objects;

/**
 * Allow to maintain the dispatching of events in order when a listener itself fires an event.
 * 
 * @author sylvain
 *
 * @param <L> listener type.
 * @param <E> event type.
 * @param <X> exception type.
 */
public final class ReentrantEventDispatcher<L, E, X extends Exception> {

    private final class DispatchingState extends Tuple3<Iterator<L>, BiConsumerExn<L, E, X>, E> {
        public DispatchingState(final Iterator<L> iter, BiConsumerExn<L, E, X> callback, final E evt) {
            super(Objects.requireNonNull(iter, "Missing iterator"), Objects.requireNonNull(callback, "Missing callback"), evt);
        }
    }

    private final ThreadLocal<LinkedList<DispatchingState>> events = new ThreadLocal<LinkedList<DispatchingState>>() {
        @Override
        protected LinkedList<DispatchingState> initialValue() {
            return new LinkedList<>();
        }
    };

    private final BiConsumerExn<L, E, X> callback;

    public ReentrantEventDispatcher() {
        this(null);
    }

    public ReentrantEventDispatcher(final BiConsumerExn<L, E, X> callback) {
        super();
        this.callback = callback;
    }

    public final void fire(final Iterator<L> iter, final E evt) throws X {
        this.fire(iter, this.callback, evt);
    }

    public final void fire(final Iterator<L> iter, final BiConsumerExn<L, E, X> callback, final E evt) throws X {
        this.fire(new DispatchingState(iter, callback, evt));
    }

    private final void fire(final DispatchingState newTuple) throws X {
        final LinkedList<DispatchingState> linkedList = this.events.get();
        // add new event
        linkedList.addLast(newTuple);
        // process all pending events
        DispatchingState currentTuple;
        while ((currentTuple = linkedList.peekFirst()) != null) {
            final Iterator<L> currentIter = currentTuple.get0();
            final BiConsumerExn<L, E, X> currentCallback = currentTuple.get1();
            final E currentEvt = currentTuple.get2();
            while (currentIter.hasNext()) {
                final L l = currentIter.next();
                currentCallback.accept(l, currentEvt);
            }
            // not removeFirst() since the item might have been already removed
            linkedList.pollFirst();
        }
    }

}