groovyx.gpars.dataflow
Class DataflowVariable<T>

java.lang.Object
  extended by groovyx.gpars.serial.WithSerialId
      extended by groovyx.gpars.dataflow.expression.DataflowExpression<T>
          extended by groovyx.gpars.dataflow.DataflowVariable<T>
Type Parameters:
T - Type of values to bind with the DataflowVariable
All Implemented Interfaces:
groovy.lang.GroovyObject, DataflowChannel<T>, DataflowReadChannel<T>, DataflowWriteChannel<T>, Promise<T>, java.io.Serializable
Direct Known Subclasses:
DataflowVariable.RemoteDataflowVariable, SyncDataflowVariable

public class DataflowVariable<T>
extends DataflowExpression<T>
implements DataflowChannel<T>, Promise<T>

Represents a thread-safe single-assignment, multi-read variable. Each instance of DataflowVariable can be read repeatedly any time using the 'val' property and assigned once in its lifetime using the '<<' operator. Reads preceding assignment will be blocked until the value is assigned. For actors and Dataflow Operators the asynchronous non-blocking variants of the getValAsync() methods can be used. They register the request to read a value and will send a message to the actor or operator once the value is available.

Author:
Vaclav Pech, Alex Tkachman Date: Jun 4, 2009
See Also:
Serialized Form

Nested Class Summary
static class DataflowVariable.RemoteDataflowVariable<T>
           
 
Nested classes/interfaces inherited from class groovyx.gpars.dataflow.expression.DataflowExpression
DataflowExpression.BindDataflow<T>
 
Field Summary
private static long serialVersionUID
           
 
Fields inherited from class groovyx.gpars.dataflow.expression.DataflowExpression
error, S_INITIALIZED, S_INITIALIZING, S_NOT_INITIALIZED, state, value
 
Fields inherited from class groovyx.gpars.serial.WithSerialId
serialHandle
 
Constructor Summary
DataflowVariable()
          Creates a new unbound Dataflow Variable
 
Method Summary
private  DataflowWriteChannel<T> bindDFV(DataflowReadChannel<T> ref)
           
 T get()
          Retrieves the value of the variable, blocking until a value is available
 T get(long timeout, java.util.concurrent.TimeUnit units)
          Retrieves the value of the variable, blocking up to given timeout, if the value has not been assigned yet.
 java.lang.Throwable getError()
          Returns the error bound to the promise
 java.lang.Class<DataflowVariable.RemoteDataflowVariable> getRemoteClass()
          Class of remote object to be created
 boolean isError()
          Checks if the promise is bound to an error
 DataflowWriteChannel<T> leftShift(DataflowReadChannel<T> ref)
          Assigns a value from one DataflowVariable instance to this variable.
 DataflowWriteChannel<T> leftShift(T value)
          Assigns a value to the variable.
(package private)  boolean shouldThrowTimeout()
           
<V> Promise<V>
then(groovy.lang.Closure<V> closure, groovy.lang.Closure<V> errorHandler)
          Schedule closure to be executed after data became available.
<V> Promise<V>
then(PGroup group, groovy.lang.Closure<V> closure, groovy.lang.Closure<V> errorHandler)
          Schedule closure to be executed after data becomes available.
<V> Promise<V>
then(Pool pool, groovy.lang.Closure<V> closure, groovy.lang.Closure<V> errorHandler)
          Schedule closure to be executed after data becomes available.
 
Methods inherited from class groovyx.gpars.dataflow.expression.DataflowExpression
binaryChoice, binaryChoice, binaryChoice, binaryChoice, binaryChoice, binaryChoice, bind, bindError, bindSafely, bindUnique, doBindImpl, doBindRemote, evaluate, filter, filter, filter, filter, filter, filter, getEventManager, getMetaClass, getProperty, getVal, getVal, getValAsync, getValAsync, chainWith, chainWith, chainWith, chainWith, chainWith, chainWith, choice, choice, choice, choice, choice, choice, into, into, into, into, into, into, invokeMethod, isBound, join, join, length, merge, merge, merge, merge, merge, merge, merge, merge, merge, merge, merge, merge, or, or, poll, rightShift, separate, separate, separate, separate, separate, separate, setMetaClass, setProperty, scheduleCallback, split, split, split, split, split, split, split, split, split, split, split, split, subscribe, subscribe, tap, tap, tap, tap, tap, tap, then, then, then, toString, transform, whenBound, whenBound, whenBound, whenBound, wheneverBound, wheneverBound
 
Methods inherited from class groovyx.gpars.serial.WithSerialId
createRemoteHandle, getOrCreateSerialHandle, writeReplace
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface groovyx.gpars.dataflow.DataflowReadChannel
binaryChoice, binaryChoice, binaryChoice, binaryChoice, binaryChoice, binaryChoice, filter, filter, filter, filter, filter, filter, getEventManager, getVal, getVal, getValAsync, getValAsync, chainWith, chainWith, chainWith, chainWith, chainWith, chainWith, choice, choice, choice, choice, choice, choice, into, into, into, into, into, into, isBound, length, merge, merge, merge, merge, merge, merge, merge, merge, merge, merge, merge, merge, or, or, poll, rightShift, separate, separate, separate, separate, separate, separate, split, split, split, split, split, split, split, split, split, split, split, split, tap, tap, tap, tap, tap, tap, then, then, then, whenBound, whenBound, whenBound, whenBound, wheneverBound, wheneverBound
 
Methods inherited from interface groovyx.gpars.dataflow.DataflowWriteChannel
bind
 
Methods inherited from interface groovyx.gpars.dataflow.Promise
isBound, join, join, rightShift, then, then, then, whenBound, whenBound, whenBound, whenBound
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values
Constructor Detail

DataflowVariable

public DataflowVariable()
Creates a new unbound Dataflow Variable

Method Detail

leftShift

public DataflowWriteChannel<T> leftShift(T value)
Assigns a value to the variable. Can only be invoked once on each instance of DataflowVariable

Specified by:
leftShift in interface DataflowWriteChannel<T>
Parameters:
value - The value to assign
Returns:
The current channel instance

leftShift

public DataflowWriteChannel<T> leftShift(DataflowReadChannel<T> ref)
Assigns a value from one DataflowVariable instance to this variable. Can only be invoked once on each instance of DataflowVariable

Specified by:
leftShift in interface DataflowWriteChannel<T>
Parameters:
ref - The DataflowVariable instance the value of which to bind
Returns:
The current channel instance

bindDFV

private DataflowWriteChannel<T> bindDFV(DataflowReadChannel<T> ref)

get

public T get()
      throws java.lang.Throwable
Retrieves the value of the variable, blocking until a value is available

Specified by:
get in interface Promise<T>
Returns:
The value stored in the variable
Throws:
java.lang.Throwable - If the stored value is an exception instance it gets re-thrown

get

public final T get(long timeout,
                   java.util.concurrent.TimeUnit units)
            throws java.lang.Throwable
Retrieves the value of the variable, blocking up to given timeout, if the value has not been assigned yet.

Specified by:
get in interface Promise<T>
Parameters:
timeout - The timeout value
units - Units for the timeout
Returns:
The value stored in the variable
Throws:
java.lang.Throwable - If the stored value is an exception instance it gets re-thrown

shouldThrowTimeout

boolean shouldThrowTimeout()

isError

public final boolean isError()
Checks if the promise is bound to an error

Specified by:
isError in interface Promise<T>
Returns:
True, if an error has been bound

getError

public final java.lang.Throwable getError()
Returns the error bound to the promise

Specified by:
getError in interface Promise<T>
Returns:
The error
Throws:
java.lang.IllegalStateException - If not bound or not bound to an error

then

public final <V> Promise<V> then(groovy.lang.Closure<V> closure,
                                 groovy.lang.Closure<V> errorHandler)
Schedule closure to be executed after data became available. It is important to notice that even if the expression is already bound the execution of closure will not happen immediately but will be scheduled

Specified by:
then in interface Promise<T>
Parameters:
closure - closure to execute when data becomes available. The closure should take at most one argument.
errorHandler - closure to execute when an error (instance of Throwable) gets bound. The closure should take at most one argument.
Returns:
A promise for the results of the supplied closure. This allows for chaining of then() method calls.

then

public final <V> Promise<V> then(Pool pool,
                                 groovy.lang.Closure<V> closure,
                                 groovy.lang.Closure<V> errorHandler)
Schedule closure to be executed after data becomes available. It is important to notice that even if the expression is already bound the execution of closure will not happen immediately but will be scheduled.

Specified by:
then in interface Promise<T>
Parameters:
pool - The thread pool to use for task scheduling for asynchronous message delivery
closure - closure to execute when data becomes available. The closure should take at most one argument.
errorHandler - closure to execute when an error (instance of Throwable) gets bound. The closure should take at most one argument.
Returns:
A promise for the results of the supplied closure. This allows for chaining of then() method calls.

then

public final <V> Promise<V> then(PGroup group,
                                 groovy.lang.Closure<V> closure,
                                 groovy.lang.Closure<V> errorHandler)
Schedule closure to be executed after data becomes available. It is important to notice that even if the expression is already bound the execution of closure will not happen immediately but will be scheduled.

Specified by:
then in interface Promise<T>
Parameters:
group - The PGroup to use for task scheduling for asynchronous message delivery
closure - closure to execute when data becomes available. The closure should take at most one argument.
errorHandler - closure to execute when an error (instance of Throwable) gets bound. The closure should take at most one argument.
Returns:
A promise for the results of the supplied closure. This allows for chaining of then() method calls.

getRemoteClass

public java.lang.Class<DataflowVariable.RemoteDataflowVariable> getRemoteClass()
Description copied from class: WithSerialId
Class of remote object to be created

Overrides:
getRemoteClass in class WithSerialId
Returns:
Throws UnsupportedOperationException

Copyright © 2008–2012 Václav Pech. All Rights Reserved.