org.codehaus.gpars

groovyx.gpars.dataflow.stream
[Java] Class DataflowStream

java.lang.Object
  groovyx.gpars.dataflow.stream.DataflowStream
All Implemented Interfaces:
FList

@SuppressWarnings({"rawtypes", "TailRecursion", "unchecked", "StaticMethodNamingConvention", "ClassWithTooManyMethods"})
public final class DataflowStream
extends java.lang.Object

Represents a deterministic dataflow channel. Unlike a DataflowQueue, DataflowStream allows multiple readers each to read all the messages. Essentially, you may think of DataflowStream as a 1 to many communication channel, since when a reader consumes a messages, other readers will still be able to read the message. Also, all messages arrive to all readers in the same order. DataflowStream is implemented as a functional queue, which impacts the API in that users have to traverse the values in the stream themselves. On the other hand in offers handy methods for value filtering or transformation together with interesting performance characteristics. For convenience and for the ability to use DataflowStream with other dataflow constructs, like e.g. operators, you can wrap DataflowStreams with DataflowReadAdapter for read access or DataflowWriteAdapter for write access.

The DataflowStream class is designed for single-threaded producers and consumers. If multiple threads are supposed to read or write values to the stream, their access to the stream must be serialized externally or the adapters should be used.

Parameters:
- Type for values to pass through the stream
Authors:
Johannes Link, Vaclav Pech


Field Summary
private DataflowVariable first

private java.util.concurrent.atomic.AtomicReference rest

private java.util.Collection wheneverBoundListeners

A collection of listeners who need to be informed each time the stream is bound to a value

 
Constructor Summary
DataflowStream()

Creates an empty stream

DataflowStream(groovy.lang.Closure toBeApplied)

Creates a stream while applying the supplied initialization closure to it

private DataflowStream(java.util.Collection wheneverBoundListeners)

 
Method Summary
java.lang.String appendingString()

DataflowStream apply(groovy.lang.Closure closure)

Calls the supplied closure with the stream as a parameter

private void bind(T value)

static T eos()

boolean equals(java.lang.Object obj)

private static T eval(java.lang.Object valueOrDataflowVariable)

FList filter(groovy.lang.Closure filterClosure)

Builds a filtered stream using the supplied filter closure

private void filter(DataflowStream rest, groovy.lang.Closure filterClosure, DataflowStream result)

DataflowStream generate(T seed, groovy.lang.Closure generator, groovy.lang.Closure condition)

Populates the stream with generated values

private void generateNext(T value, DataflowStream stream, groovy.lang.Closure generator, groovy.lang.Closure condition)

T getFirst()

Retrieved the first element in the stream, blocking until a value is available

DataflowVariable getFirstDFV()

FList getRest()

Retrieves a DataflowStream representing the rest of this Stream after removing the first element

int hashCode()

private DataflowExpression hookWheneverBoundListeners(DataflowExpression expr)

Hooks the registered when bound handlers to the supplied dataflow expression

boolean isEmpty()

Indicates, whether the first element in the stream is an eos

java.util.Iterator iterator()

Builds an iterator to iterate over the stream

DataflowStream leftShift(DataflowReadChannel ref)

Adds a dataflow variable value to the stream, once the value is available

DataflowStream leftShift(T value)

Adds a value to the stream

FList map(groovy.lang.Closure mapClosure)

Builds a modified stream using the supplied map closure

private void map(FList rest, groovy.lang.Closure mapClosure, DataflowStream result)

T reduce(groovy.lang.Closure reduceClosure)

Reduces all elements in the stream using the supplied closure

T reduce(T seed, groovy.lang.Closure reduceClosure)

Reduces all elements in the stream using the supplied closure

private T reduce(T current, FList rest, groovy.lang.Closure reduceClosure)

java.lang.String toString()

void wheneverBound(groovy.lang.Closure closure)

void wheneverBound(MessageStream stream)

 
Methods inherited from class java.lang.Object
java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#wait(long, int), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()
 

Field Detail

first

private final DataflowVariable first


rest

private final java.util.concurrent.atomic.AtomicReference rest


wheneverBoundListeners

private final java.util.Collection wheneverBoundListeners
A collection of listeners who need to be informed each time the stream is bound to a value


 
Constructor Detail

DataflowStream

public DataflowStream()
Creates an empty stream


DataflowStream

public DataflowStream(groovy.lang.Closure toBeApplied)
Creates a stream while applying the supplied initialization closure to it
Parameters:
toBeApplied - The closure to use for initialization


DataflowStream

@SuppressWarnings({"AssignmentToCollectionOrArrayFieldFromParameter"})
private DataflowStream(java.util.Collection wheneverBoundListeners)


 
Method Detail

appendingString

@Override
public java.lang.String appendingString()


apply

public final DataflowStream apply(groovy.lang.Closure closure)
Calls the supplied closure with the stream as a parameter
Parameters:
closure - The closure to call
Returns:
This instance of DataflowStream


bind

private void bind(T value)


eos

public static T eos()


equals

@Override
public boolean equals(java.lang.Object obj)


eval

private static T eval(java.lang.Object valueOrDataflowVariable)


filter

@Override
public FList filter(groovy.lang.Closure filterClosure)
Builds a filtered stream using the supplied filter closure
Parameters:
filterClosure - The closure to decide on inclusion of elements
Returns:
The first item of the filtered stream


filter

private void filter(DataflowStream rest, groovy.lang.Closure filterClosure, DataflowStream result)


generate

public DataflowStream generate(T seed, groovy.lang.Closure generator, groovy.lang.Closure condition)
Populates the stream with generated values
Parameters:
seed - The initial element to evaluate and add as the first value of the stream
generator - A closure generating stream elements from the previous values
condition - A closure indicating whether the generation should continue based on the last generated value
Returns:
This stream


generateNext

private void generateNext(T value, DataflowStream stream, groovy.lang.Closure generator, groovy.lang.Closure condition)


getFirst

@Override
public T getFirst()
Retrieved the first element in the stream, blocking until a value is available
Returns:
The first item in the stream


getFirstDFV

DataflowVariable getFirstDFV()


getRest

@Override
public FList getRest()
Retrieves a DataflowStream representing the rest of this Stream after removing the first element
Returns:
The remaining stream elements


hashCode

@Override
public int hashCode()


hookWheneverBoundListeners

private DataflowExpression hookWheneverBoundListeners(DataflowExpression expr)
Hooks the registered when bound handlers to the supplied dataflow expression
Parameters:
expr - The expression to hook all the when bound listeners to
Returns:
The supplied expression handler to allow method chaining


isEmpty

@Override
public boolean isEmpty()
Indicates, whether the first element in the stream is an eos


iterator

@Override
public java.util.Iterator iterator()
Builds an iterator to iterate over the stream
Returns:
A new FListIterator instance


leftShift

public DataflowStream leftShift(DataflowReadChannel ref)
Adds a dataflow variable value to the stream, once the value is available
Parameters:
ref - The DataflowVariable to check for value
Returns:
The rest of the stream


leftShift

public DataflowStream leftShift(T value)
Adds a value to the stream
Parameters:
value - The value to add
Returns:
The rest of the stream


map

@Override
public FList map(groovy.lang.Closure mapClosure)
Builds a modified stream using the supplied map closure
Parameters:
mapClosure - The closure to transform elements
Returns:
The first item of the transformed stream


map

private void map(FList rest, groovy.lang.Closure mapClosure, DataflowStream result)


reduce

@Override
public T reduce(groovy.lang.Closure reduceClosure)
Reduces all elements in the stream using the supplied closure
Parameters:
reduceClosure - The closure to reduce elements of the stream gradually into an accumulator. The accumulator is seeded with the first stream element.
Returns:
The result of reduction of the whole stream


reduce

@Override
public T reduce(T seed, groovy.lang.Closure reduceClosure)
Reduces all elements in the stream using the supplied closure
Parameters:
reduceClosure - The closure to reduce elements of the stream gradually into an accumulator.
seed - The value to initialize the accumulator with.
Returns:
The result of reduction of the whole stream


reduce

private T reduce(T current, FList rest, groovy.lang.Closure reduceClosure)


toString

@Override
public java.lang.String toString()


wheneverBound

public void wheneverBound(groovy.lang.Closure closure)


wheneverBound

public void wheneverBound(MessageStream stream)


 

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