net.fortytwo.flow.SynchronizedSink Maven / Gradle / Ivy
package net.fortytwo.flow;
import net.fortytwo.ripple.RippleException;
/**
* A pipeline which enforces one data item at a time, regardless of the number of threads writing into the sink.
* It is thread-safe but protects downstream components which may not be thread-safe.
* @param the type of data being passed
*
* @author Joshua Shinavier (http://fortytwo.net)
*/
public class SynchronizedSink implements Sink
{
private final Object mutex;
private final Sink sink;
/**
* Constructs a new synchronized sink using a specified mutex
* @param other the downstream sink which is to be protected
* @param mutex a mutex to use for synchronization
*/
public SynchronizedSink( final Sink other, final Object mutex )
{
sink = other;
this.mutex = mutex;
}
/**
* Constructs a new synchronized sink using an internal mutex
* @param other the downstream sink which is to be protected
*/
public SynchronizedSink( final Sink other )
{
this( other, other );
}
public void put( final T t ) throws RippleException
{
synchronized (mutex)
{
sink.put( t );
}
}
/**
* @return the mutex which this pipeline uses for synchronization
*/
public Object getMutex()
{
return mutex;
}
}