groovyx.gpars.dataflow
Class LazyDataflowVariable<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>
              extended by groovyx.gpars.dataflow.LazyDataflowVariable<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>, SelectableChannel<T>, java.io.Serializable

public final class LazyDataflowVariable<T>
extends DataflowVariable<T>

Represents a thread-safe single-assignment, multi-read variable with delayed initialization. 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 Date: April 4, 2013
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class groovyx.gpars.dataflow.DataflowVariable
DataflowVariable.RemoteDataflowVariable<T>
 
Nested classes/interfaces inherited from class groovyx.gpars.dataflow.expression.DataflowExpression
DataflowExpression.BindDataflow<T>
 
Field Summary
private  PGroup group
           
private  java.util.concurrent.atomic.AtomicBoolean initializationStarted
           
private  groovy.lang.Closure<T> initializer
           
 
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
LazyDataflowVariable(groovy.lang.Closure<T> initializer)
          Creates a new unbound Lazy Dataflow Variable
LazyDataflowVariable(PGroup group, groovy.lang.Closure<T> initializer)
          Creates a new unbound Lazy Dataflow Variable with specific a PGroup to use for running the initializer
 
Method Summary
 java.lang.Throwable getError()
          Returns the error bound to the promise
 T getVal()
          Reads the value of the variable.
 T getVal(long timeout, java.util.concurrent.TimeUnit units)
          Reads the value of the variable.
 void getValAsync(MessageStream callback)
          Asynchronously retrieves the value of the variable.
 void getValAsync(java.lang.Object attachment, MessageStream callback)
          Used by Dataflow operators.
private  void initialize()
           
 boolean isError()
          Checks if the promise is bound to an error
<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.
 void touch()
          May be used by lazy implementations to warm up
 
Methods inherited from class groovyx.gpars.dataflow.DataflowVariable
get, get, getRemoteClass, leftShift, leftShift, shouldThrowTimeout
 
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, getBindErrorManager, getEventManager, getMetaClass, getProperty, 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, 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, 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
 
Methods inherited from interface groovyx.gpars.dataflow.SelectableChannel
poll, 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

initializer

private final groovy.lang.Closure<T> initializer

initializationStarted

private final java.util.concurrent.atomic.AtomicBoolean initializationStarted

group

private final PGroup group
Constructor Detail

LazyDataflowVariable

public LazyDataflowVariable(groovy.lang.Closure<T> initializer)
Creates a new unbound Lazy Dataflow Variable


LazyDataflowVariable

public LazyDataflowVariable(PGroup group,
                            groovy.lang.Closure<T> initializer)
Creates a new unbound Lazy Dataflow Variable with specific a PGroup to use for running the initializer

Method Detail

initialize

private void initialize()

touch

public void touch()
Description copied from interface: Promise
May be used by lazy implementations to warm up

Specified by:
touch in interface Promise<T>
Overrides:
touch in class DataflowVariable<T>

isError

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

Specified by:
isError in interface Promise<T>
Overrides:
isError in class DataflowVariable<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>
Overrides:
getError in class DataflowVariable<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>
Overrides:
then in class DataflowVariable<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>
Overrides:
then in class DataflowVariable<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>
Overrides:
then in class DataflowVariable<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.

getVal

public T getVal()
         throws java.lang.InterruptedException
Description copied from class: DataflowExpression
Reads the value of the variable. Blocks, if the value has not been assigned yet.

Specified by:
getVal in interface DataflowReadChannel<T>
Overrides:
getVal in class DataflowExpression<T>
Returns:
The actual value
Throws:
java.lang.InterruptedException - If the current thread gets interrupted while waiting for the variable to be bound

getVal

public T getVal(long timeout,
                java.util.concurrent.TimeUnit units)
         throws java.lang.InterruptedException
Description copied from class: DataflowExpression
Reads the value of the variable. Blocks up to given timeout, if the value has not been assigned yet.

Specified by:
getVal in interface DataflowReadChannel<T>
Overrides:
getVal in class DataflowExpression<T>
Parameters:
timeout - The timeout value
units - Units for the timeout
Returns:
The actual value
Throws:
java.lang.InterruptedException - If the current thread gets interrupted while waiting for the variable to be bound

getValAsync

public void getValAsync(MessageStream callback)
Description copied from class: DataflowExpression
Asynchronously retrieves the value of the variable. Sends the actual value of the variable as a message back the the supplied actor once the value has been bound. The actor can perform other activities or release a thread back to the pool by calling react() waiting for the message with the value of the Dataflow Variable.

Specified by:
getValAsync in interface DataflowReadChannel<T>
Overrides:
getValAsync in class DataflowExpression<T>
Parameters:
callback - An actor to send the bound value to.

getValAsync

public void getValAsync(java.lang.Object attachment,
                        MessageStream callback)
Description copied from class: DataflowExpression
Used by Dataflow operators. Asynchronously retrieves the value of the variable. Sends a message back the the supplied MessageStream with a map holding the supplied attachment under the 'attachment' key and the actual value of the variable under the 'result' key once the value has been bound. Attachment is an arbitrary value helping the actor.operator match its request with the reply. The actor/operator can perform other activities or release a thread back to the pool by calling react() waiting for the message with the value of the Dataflow Variable.

Specified by:
getValAsync in interface DataflowReadChannel<T>
Overrides:
getValAsync in class DataflowExpression<T>
Parameters:
attachment - arbitrary non-null attachment if reader needs better identification of result
callback - An actor to send the bound value plus the supplied index to.

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