All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.erasmusmc.data_mining.ontology.impl.EventMulticaster Maven / Gradle / Ivy

The newest version!
/**
 * Peregrine is an indexing engine that can recognize concepts in human readable
 * text, based on a database (thesaurus) of known terms.
 *
 * Copyright 2005-2011 Erasmus University Medical Centre (EMC)
 * Copyright 2009-2011 Netherlands Bioinformatics Centre (NBIC)
 *
 * Contact us at: [email protected]
 *
 * This file is part of Peregrine. Peregrine is free software: you can
 * redistribute it and/or modify it under the terms of the GNU Affero General
 * Public License as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * This program 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 Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see .
 */
package org.erasmusmc.data_mining.ontology.impl;

import java.util.EventListener;

/**
 * Originally it is implemented in java.awt.AWTEventMulticaster class but uses
 * no generics.
 *
 * @author Dmitry Katsubo
 * @param  The class of EventListeners supported by this multicaster.
 */
public abstract class EventMulticaster
        implements EventListener {

    /**
     * The left branch of the listeners of this multicaster.
     */
    protected final T left;
    /**
     * the right branch of the listeners of this multicaster.
     */
    protected final T right;

    /**
     * Constructor.
     *
     * @param a the listener
     * @param b the listener
     *
     * @see java.awt.AWTEventMulticaster
     */
    protected EventMulticaster(final T a, final T b) {
        this.left = a;
        this.right = b;

        assert a != null && b != null;
    }

    /**
     * Add a listener to this multicaster.
     *
     * @param a the listener
     * @param b the listener
     * @return the listener
     */
    protected abstract T addInternal(T a, T b);

    /**
     * Remove a listener from this multicaster.
     *
     * @param oldl the listener to remove.
     * @return The remaining listener.
     */
    @SuppressWarnings("unchecked")
    private T removeInternal(final T oldl) {
        // Return the other listener if there is match:
        if (oldl == left) {
            return right;
        }
        if (oldl == right) {
            return left;
        }

        // Otherwise we process the left and right branch:
        final T a2 = removeInternal(left, oldl);
        final T b2 = removeInternal(right, oldl);

        if (a2 == left && b2 == right) {
            // it's not here
            return (T) this;
        }

        return addInternal(a2, b2);
    }

    /**
     * Remove the listener.
     *
     * @param  the type of the listener.
     * @param l the parent of the listener to remove.
     * @param oldl the listener to remove.
     * @return null if the either params was null or the parent listener
     * otherwise.
     */
    @SuppressWarnings({ "unchecked", "unchecked" })
    protected static  T removeInternal(final T l,
            final T oldl) {

        if (l == oldl || l == null) {
            return null;
        } else if (l instanceof EventMulticaster) {
            return ((EventMulticaster) l).removeInternal(oldl);
        } else {
            return l; // it's not here
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy