jconch.pipeline
Class PipeLink<T>

java.lang.Object
  extended by jconch.pipeline.PipeLink<T>
Direct Known Subclasses:
BoundedPipeLink, UnboundedPipeLink

public class PipeLink<T>
extends Object

A link in the stages of the pipeline. This class takes responsibility for all of the concurrency handling during hand-offs between pipelines. Furthermore, it abstracts the nature of the preceeding and succeeding elements -- as long as something feeds into this class or reads out of this class, it can be merged into a standard pipeline.

This class has a concept of a "fetch timeout" (getFetchTimeout()/setFetchTimeout(long)). Although adding new elements will always be accepted, it is possible that a stall might cause a delay in processing. The fetch timeout is the amount of time to wait before the stall is detected. The default value is 0, which means to not wait at all, but act in pass-through mode.

The link requires at least one source and one sink to be registered before add(Object) and get() are usable. A source will place elements into the link, and a sink will draw elements from it. These registries are used for tracking status.

Author:
Robert Fischer

Constructor Summary
protected PipeLink(BlockingQueue<T> queue)
          Constructor.
 
Method Summary
 boolean add(T in)
          Adds an element into the link, if at all possible.
 void breakLink()
          Breaks the link completely and permanently.
 void clearQueue()
          Drops all elements from this link.
 T get()
          Removes an element from the link, if any is available.
 long getAddTimeout()
          Gets the timeout on add operations of the instance.
 long getFetchTimeout()
          Gets the timeout on fetch operations of the instance.
 int getQueueLength()
          Provides the number of elements currently in the queue.
 int getRemainingCapacity()
          Provides an estimate of the number of elements the queue could additionally hold.
 void registerSource(Producer<T> source)
          Registers a source for this pipe link.
 void setAddTimeout(long timeout)
          Sets the add timeout of the instance.
 void setFetchTimeout(long newFetchTimeout)
          Sets the fetch timeout of the instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PipeLink

protected PipeLink(BlockingQueue<T> queue)
Constructor.

Parameters:
queue - The queue to use.
Throws:
org.apache.commons.lang.NullArgumentException - If the argument is null
Method Detail

registerSource

public void registerSource(Producer<T> source)
Registers a source for this pipe link.

Parameters:
source -
Throws:
org.apache.commons.lang.NullArgumentException - If the argument is null

add

public boolean add(T in)
Adds an element into the link, if at all possible.

Parameters:
in - The element to add.
Returns:
true if the add succeeded
Throws:
org.apache.commons.lang.NullArgumentException - If the argument is null

getFetchTimeout

public long getFetchTimeout()
Gets the timeout on fetch operations of the instance.

Returns:
the fetch timeout.

setFetchTimeout

public void setFetchTimeout(long newFetchTimeout)
Sets the fetch timeout of the instance. Positive values represent milliseconds to wait, and 0 means to not wait at all.

Parameters:
fetchTimeout - the fetch timeout to set
Throws:
IllegalArgumentException - If the argument is < 0.

get

public T get()
Removes an element from the link, if any is available. This method may block for up to getFetchTimeout() milliseconds for a value to become available.

Returns:
The removed element, or null if the link is empty.

getAddTimeout

public long getAddTimeout()
Gets the timeout on add operations of the instance.

Returns:
the add timeout.

setAddTimeout

public void setAddTimeout(long timeout)
Sets the add timeout of the instance. Positive values represent milliseconds to wait, and 0 means to not wait at all.

Parameters:
fetchTimeout - the add timeout to set
Throws:
IllegalArgumentException - If the argument is < 0.

getQueueLength

public int getQueueLength()
Provides the number of elements currently in the queue.

Returns:
The minimum of the number of queued elements and Integer.MAX_VALUE

getRemainingCapacity

public int getRemainingCapacity()
Provides an estimate of the number of elements the queue could additionally hold.

Returns:
An estimate of the capcity of the queue, or Integer.MAX_VALUE if it is unbounded.

clearQueue

public void clearQueue()
Drops all elements from this link.


breakLink

public void breakLink()
Breaks the link completely and permanently. Any new add(Object) or get() calls will fail after calling this method.