groovyx.gpars.dataflow
Class Dataflows

java.lang.Object
  extended by groovy.lang.GroovyObjectSupport
      extended by groovyx.gpars.dataflow.Dataflows
All Implemented Interfaces:
groovy.lang.GroovyObject

public final class Dataflows
extends groovy.lang.GroovyObjectSupport

Convenience class that makes working with DataflowVariables more comfortable.

See the implementation of groovyx.gpars.samples.dataflow.DemoDataflows for a full example.

A Dataflows instance is a bean with properties of type DataflowVariable. Property access is relayed to the access methods of DataflowVariable. Each property is initialized lazily the first time it is accessed. Non-String named properties can be also accessed using array-like indexing syntax This allows a rather compact usage of DataflowVariables like

 final df = new Dataflows()
 start { df[0] = df.x + df.y }
 start { df.x = 10 }
 start { df.y = 5 }
 assert 15 == df[0]
 

Author:
Vaclav Pech, Dierk Koenig, Alex Tkachman Date: Sep 3, 2009

Field Summary
(package private) static int DEFAULT_CONCURRENCY_LEVEL
           
(package private) static int DEFAULT_INITIAL_CAPACITY
           
(package private) static float DEFAULT_LOAD_FACTOR
           
private static DataflowVariable<java.lang.Object> DUMMY
           
private  java.lang.Object lock
           
private  java.util.concurrent.ConcurrentMap<java.lang.Object,DataflowVariable<java.lang.Object>> variables
           
 
Constructor Summary
Dataflows()
          Constructor with default values for building the underlying ConcurrentHashMap
Dataflows(int initialCapacity, float loadFactor, int concurrencyLevel)
          Constructor that supports the various constructors of the underlying ConcurrentHashMap (unless the one with Map parameter).
 
Method Summary
 boolean contains(java.lang.Object name)
          Checks whether a certain key is contained in the map.
private  DataflowVariable<java.lang.Object> ensureToContainVariable(java.lang.Object name)
          The idea is following: - we try to putIfAbsent dummy DFV in to map - if something real already there we are done - if not we obtain lock and put new DFV with double check

Unfortunately we have to sync on this as there is no better option (God forbid to sync on name)

(package private)  java.lang.Object getAt(int index)
          Retrieves the DFV associated with the given index
 java.lang.Object getProperty(java.lang.String property)
           
 java.lang.Object invokeMethod(java.lang.String name, java.lang.Object args)
          Invokes the given method.
 java.util.Iterator<java.util.Map.Entry<java.lang.Object,DataflowVariable<java.lang.Object>>> iterator()
          Convenience method to play nicely with Groovy object iteration methods.
(package private)  void putAt(java.lang.Object index, java.lang.Object value)
          Binds the value to the DataflowVariable that is associated with the property "index".
private  DataflowVariable<java.lang.Object> putNewUnderLock(java.lang.Object name)
          Utility method extracted just to help JIT
 DataflowVariable<java.lang.Object> remove(java.lang.Object name)
          Removes a DFV from the map and binds it to null, if it has not been bound yet
 void setProperty(java.lang.String property, java.lang.Object newValue)
          Binds the value to the DataflowVariable that is associated with the property "name".
 
Methods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, setMetaClass
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DUMMY

private static final DataflowVariable<java.lang.Object> DUMMY

lock

private final java.lang.Object lock

variables

private java.util.concurrent.ConcurrentMap<java.lang.Object,DataflowVariable<java.lang.Object>> variables

DEFAULT_INITIAL_CAPACITY

static final int DEFAULT_INITIAL_CAPACITY
See Also:
Constant Field Values

DEFAULT_LOAD_FACTOR

static final float DEFAULT_LOAD_FACTOR
See Also:
Constant Field Values

DEFAULT_CONCURRENCY_LEVEL

static final int DEFAULT_CONCURRENCY_LEVEL
See Also:
Constant Field Values
Constructor Detail

Dataflows

public Dataflows(int initialCapacity,
                 float loadFactor,
                 int concurrencyLevel)
Constructor that supports the various constructors of the underlying ConcurrentHashMap (unless the one with Map parameter).

Parameters:
initialCapacity - the initial capacity. The implementation performs internal sizing to accommodate this many elements.
loadFactor - the load factor threshold, used to control resizing. Resizing may be performed when the average number of elements per bin exceeds this threshold.
concurrencyLevel - the estimated number of concurrently updating threads. The implementation performs internal sizing to try to accommodate this many threads.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is negative or the load factor or concurrencyLevel are non-positive.
See Also:
ConcurrentHashMap

Dataflows

public Dataflows()
Constructor with default values for building the underlying ConcurrentHashMap

See Also:
ConcurrentHashMap
Method Detail

setProperty

public void setProperty(java.lang.String property,
                        java.lang.Object newValue)
Binds the value to the DataflowVariable that is associated with the property "name".

Specified by:
setProperty in interface groovy.lang.GroovyObject
Overrides:
setProperty in class groovy.lang.GroovyObjectSupport
Parameters:
newValue - a scalar or a DataflowVariable that may block on value access
See Also:
DataflowExpression.bind(T)

getProperty

public java.lang.Object getProperty(java.lang.String property)
Specified by:
getProperty in interface groovy.lang.GroovyObject
Overrides:
getProperty in class groovy.lang.GroovyObjectSupport
Returns:
the value of the DataflowVariable associated with the property "name". May block if the value is not scalar.
See Also:
DataflowExpression.getVal()

invokeMethod

public java.lang.Object invokeMethod(java.lang.String name,
                                     java.lang.Object args)
Invokes the given method. Allows for invoking whenBound() on the dataflow variables.
 def df = new Dataflows()
 df.var {*     println "Variable bound to $it"
 }* 

Specified by:
invokeMethod in interface groovy.lang.GroovyObject
Overrides:
invokeMethod in class groovy.lang.GroovyObjectSupport
Parameters:
name - the name of the method to call (the variable name)
args - the arguments to use for the method call (a closure to invoke when a value is bound)
Returns:
the result of invoking the method (void)

getAt

java.lang.Object getAt(int index)
                 throws java.lang.InterruptedException
Retrieves the DFV associated with the given index

Parameters:
index - The index to find a match for
Returns:
the value of the DataflowVariable associated with the property "index". May block if the value is not scalar.
Throws:
java.lang.InterruptedException - If the thread gets interrupted
See Also:
DataflowExpression.getVal()

putAt

void putAt(java.lang.Object index,
           java.lang.Object value)
Binds the value to the DataflowVariable that is associated with the property "index".

Parameters:
index - The index to associate the value with
value - a scalar or a DataflowVariable that may block on value access
See Also:
DataflowExpression.bind(T)

ensureToContainVariable

private DataflowVariable<java.lang.Object> ensureToContainVariable(java.lang.Object name)
The idea is following: - we try to putIfAbsent dummy DFV in to map - if something real already there we are done - if not we obtain lock and put new DFV with double check

Unfortunately we have to sync on this as there is no better option (God forbid to sync on name)

Parameters:
name - The key to ensure has a DFV bound to it
Returns:
DataflowVariable corresponding to name

putNewUnderLock

private DataflowVariable<java.lang.Object> putNewUnderLock(java.lang.Object name)
Utility method extracted just to help JIT

Parameters:
name - The key to ensure has a DFV bound to it
Returns:
a DFV associated with the key

remove

public DataflowVariable<java.lang.Object> remove(java.lang.Object name)
Removes a DFV from the map and binds it to null, if it has not been bound yet

Parameters:
name - The name of the DFV to remove.
Returns:
A DFV is exists, or null

contains

public boolean contains(java.lang.Object name)
Checks whether a certain key is contained in the map. Doesn't check, whether the variable has already been bound.

Parameters:
name - The name of the DFV to check.
Returns:
A DFV is exists, or null

iterator

public java.util.Iterator<java.util.Map.Entry<java.lang.Object,DataflowVariable<java.lang.Object>>> iterator()
Convenience method to play nicely with Groovy object iteration methods. The iteration restrictions of ConcurrentHashMap concerning parallel access and ConcurrentModificationException apply.

Returns:
iterator over the stored key:DataflowVariable value pairs

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