biz.hammurapi.rules.backwardreasoning
Class Accumulator

java.lang.Object
  extended by biz.hammurapi.rules.backwardreasoning.Accumulator

public class Accumulator
extends java.lang.Object

Accumulates objects. Allows multiple producers concurrently add objects and multiple consumers iterate over objects. Iterators block until next item is available or all producers are done with producing objects. Addition of nulls is not allowed. In some situations last value returned by iterator can be null. Consumers shall check value returned by iterator for null and discard it.

Author:
Pavel

Nested Class Summary
static interface Accumulator.AccumulatorIterator
           
protected static interface Accumulator.Link
           
protected  class Accumulator.LinkImpl
           
 
Field Summary
protected  Accumulator.Link current
           
protected  Accumulator.Link END
           
protected  Accumulator.LinkImpl first
          This link
 
Constructor Summary
Accumulator(java.lang.Class type)
          Only elements of specified type can be added to accumulator.
 
Method Summary
 void add(java.lang.Object obj)
          Adds object, unblocks waiting iterators.
 void addProducer(java.lang.Object producer)
          Indicates that one more produces was added to the accumulator.
 java.util.Iterator blockingIterator()
          Returned iterator iterates over available elements and blocks waiting for more elements from producers or for all producers to finish.
 void close(java.lang.Object producer)
          Invocation of this method indicates that no more elements will be added to accumulator.
 Accumulator.AccumulatorIterator iterator()
          Returned iterator iterates over available elements.
protected  void onBlock()
          This method is invoked before iterator enters blocked state.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

END

protected final Accumulator.Link END

first

protected final Accumulator.LinkImpl first
This link


current

protected Accumulator.Link current
Constructor Detail

Accumulator

public Accumulator(java.lang.Class type)
Only elements of specified type can be added to accumulator.

Parameters:
type -
Method Detail

addProducer

public void addProducer(java.lang.Object producer)
Indicates that one more produces was added to the accumulator.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

blockingIterator

public java.util.Iterator blockingIterator()
Returned iterator iterates over available elements and blocks waiting for more elements from producers or for all producers to finish. remove() is not supported by returned iterators.

Returns:
Iterator.

iterator

public Accumulator.AccumulatorIterator iterator()
Returned iterator iterates over available elements. It does not block waiting for more elements from producers or for all producers to finish. When this iterator reaches the end of elements chain it returns false from hasNext(). If later elements are added, it will return true from hasNext() and can be used again to navigate further. remove() is not supported by returned iterators.

Returns:
Iterator.

add

public void add(java.lang.Object obj)
Adds object, unblocks waiting iterators.

Parameters:
obj -

close

public void close(java.lang.Object producer)
Invocation of this method indicates that no more elements will be added to accumulator.


onBlock

protected void onBlock()
This method is invoked before iterator enters blocked state. Subclasses can override this to provide unblocking logic.

Parameters:
it -


Copyright © 2005 Hammurapi Group. All Rights Reserved.