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

org.metafacture.framework.helpers.DefaultTee Maven / Gradle / Ivy

There is a newer version: 6.1.2
Show newest version
/*
 * Copyright 2016 Christoph Böhme
 *
 * Licensed under the Apache License, Version 2.0 the "License";
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.metafacture.framework.helpers;

import org.metafacture.framework.Receiver;
import org.metafacture.framework.Tee;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Default implementation for tee modules.
 *
 * @param  receiver type
 *
 * @author Christoph Böhme
 */
public class DefaultTee implements Tee {

    private final List receivers = new ArrayList();

    /**
     * Creates an instance of {@link DefaultTee}.
     */
    public DefaultTee() {
    }

    @Override
    public final  R setReceiver(final R receiver) {
        receivers.clear();
        receivers.add(receiver);
        onChangeReceivers();
        return receiver;
    }

    @Override
    public final  R setReceivers(final R receiver, final T lateralReceiver) {
        receivers.clear();
        receivers.add(receiver);
        receivers.add(lateralReceiver);
        onChangeReceivers();
        return receiver;
    }

    @Override
    public final Tee addReceiver(final T receiver) {
        receivers.add(receiver);
        onChangeReceivers();
        return this;
    }

    @Override
    public final Tee removeReceiver(final T receiver) {
        receivers.remove(receiver);
        onChangeReceivers();
        return this;
    }

    @Override
    public final Tee clearReceivers() {
        receivers.clear();
        onChangeReceivers();
        return this;
    }

    @Override
    public final void resetStream() {
        onResetStream();
        for (final T receiver : receivers) {
            receiver.resetStream();
        }
    }

    @Override
    public final void closeStream() {
        onCloseStream();
        for (final T receiver : receivers) {
            receiver.closeStream();
        }
    }

    /**
     * Invoked when the list of receivers connected to this tee is changed.
     * This method is called after the receiver has been updated. Hence,
     * {@code getReceivers} will return a reference to the new list of
     * receivers.
     */
    protected void onChangeReceivers() {
        // Default implementation does nothing
    }

    /**
     * Invoked when the {@code resetStream()} method is called.
     * Override this method to perform a reset of the module.
     *
     * Do not call the {@code resetStream()} method of the next modules downstream.
     * This is handled by the implementation of {@code resetStream()} in
     * {@code DefaultTee}.
     *
     * {@code onResetStream()} is called before {@code DefaultTee} calls the
     * {@code resetStream()} method of the downstream modules.
     */
    protected void onResetStream() {
        // Default implementation does nothing
    }

    /**
     * Invoked when the {@code closeStream()} method is called. Override
     * this method to close any resources used by the module.
     *
     * Do not call the {@code closeStream()} method of the next modules
     * downstream. This is handled by the implementation of
     * {@code closeStream()} in {@code DefaultTee}.
     *
     * {@code onCloseStream()} is called before {@code DefaultTee} calls
     * the {@code closeStream()} method of the downstream modules.
     */
    protected void onCloseStream() {
        // Default implementation does nothing
    }

    /**
     * Returns a reference to the downstream module.
     *
     * @return reference to the downstream module
     */
    protected final List getReceivers() {
        return Collections.unmodifiableList(receivers);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy