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

net.fortytwo.flow.Switch Maven / Gradle / Ivy

There is a newer version: 1.5
Show newest version
package net.fortytwo.flow;

import net.fortytwo.ripple.RippleException;

/**
 * A modal pipeline which passes data alternately to one of two downstream sinks
 * @param  the type of data being passed
 *
 * @author Joshua Shinavier (http://fortytwo.net)
 */
public class Switch implements Sink
{
	private final Sink left, right;
	private boolean state;

    /**
     * Constructs a new switch over the given sinks
     * @param left one of two downstream sinks
     * @param right the other of two downstream sinks
     */
	public Switch( final Sink left, final Sink right )
	{
		this.left = left;
		this.right = right;
		state = true;
	}

    /**
     * Receives the next data item and passes it to the appropriate downstream sink.
     * @param t the data item being passed
     * @throws RippleException if a data handling error occurs
     */
	public void put( final T t ) throws RippleException
	{
		( ( state ) ? left : right ).put( t );
	}

    /**
     * Toggles between the two downstream sinks.
     * When this method is called, one sink becomes inactive (idle) while the other becomes active (ready to receive data).
     */
	public void flip()
	{
		state = !state;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy