org.shapelogic.streams
Class BaseListCommonStream<E>

java.lang.Object
  extended by org.shapelogic.streams.BaseListCommonStream<E>
All Implemented Interfaces:
java.lang.Iterable<E>, java.util.Iterator<E>, CalcValue<E>, ContextGettable, LazyCalc<E>, RecursiveContext, ListStream<E>, NumberedStream<E>, Stream<E>, StreamProperties
Direct Known Subclasses:
BaseListIndexedStream1, BaseListStream0, BaseListStream1, BaseListStream2, BaseListStreamList, IteratorStream, SingleListStream, WrappedListStream

public abstract class BaseListCommonStream<E>
extends java.lang.Object
implements ListStream<E>, StreamProperties, ContextGettable

Implementation of ListStream.
Close to
1: a lazy stream.
2: a UNIX pipe.

This is close to org.apache.commons.collections.list.LazyList, but that takes a factory to calculate next element, and that works badly here. How should a step work?
Normally call the hasNext, in order for this to work it would have to do the calculation.

So _current should be the last element that has been returned.

So when you call hasNext it will add that element to the list. When you call next it will bump the counter up one element if goes too high then do the calculation. At the end it will get to a point where the calculation fails. At that point last will be set to the current element. I will ignore synchronization to begin with.

Author:
Sami Badawi

Field Summary
protected  java.util.Map _context
           
protected  int _current
          The last value that was calculated and looked at So if you want to see that again use this for a lookup
protected  boolean _dirty
           
protected  int _last
          Last value that can be calculated.
protected  java.util.List<E> _list
           
protected  int _maxLast
          Highest value that last can take.
protected  java.lang.String _name
          Does not always exist.
protected  boolean _nullLegalValue
           
protected  RecursiveContext _parentContext
           
protected  IQueryCalc _query
           
protected  E _value
           
 
Constructor Summary
BaseListCommonStream()
           
 
Method Summary
protected  boolean calcAddNext()
          Try to calculate one more, independent of the _current.
 E get(int inputIndex)
          Get next element without advancing _current.
 java.util.Map getContext()
          The context if needed.
 int getCurrentSize()
           
 java.lang.Object getInContext(java.lang.Object key)
           
 int getIndex()
          Index of last successfully processed element.
 int getLast()
          Last possible element.
 java.util.List<E> getList()
          Get underlying list.
 int getMaxLast()
          Manually set max value for last possible element.
 java.lang.String getName()
          If a stream has a name.
 RecursiveContext getParentContext()
           
 E getValue()
          Starts a lazy calculation.
 boolean hasNext()
           
 boolean hasNextBase()
           
abstract  E invokeIndex(int index)
          Calculate the value at an index.
 boolean isCached()
          If there is a list that contains all the results.
 boolean isDeterministic()
          Is the result of a calculation deterministic.
 boolean isDirty()
          When dirty is false that means that the calculated value can be used
 boolean isNullLegalValue()
          If null is a legal value.
 boolean isRandomAccess()
          If you can calculate 1 element independent of other elements.
 java.util.Iterator<E> iterator()
           
 E next()
          Get next element and advance _current.
 void remove()
           
protected  void setLastFromInput()
           
 void setList(java.util.List<E> list)
          Get underlying list.
 void setMaxLast(int maxLast)
          Set a max value for last possible element.
 void setNullLegalValue(boolean nullLegalValue)
           
 void setup()
          Currently not super well define used for 2 purposes: Reset: called from the outside if you want to reuse it init: Called from the inside when calculation start to have everything setup maybe the semantic could work for both? TODO: This should be separated better.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_list

protected java.util.List<E> _list

_current

protected int _current
The last value that was calculated and looked at So if you want to see that again use this for a lookup


_last

protected int _last
Last value that can be calculated. When _maxLast is set then _last should first be set to the same. If lower limit is found move down.


_maxLast

protected int _maxLast
Highest value that last can take.


_name

protected java.lang.String _name
Does not always exist.


_dirty

protected boolean _dirty

_value

protected E _value

_context

protected java.util.Map _context

_query

protected IQueryCalc _query

_parentContext

protected RecursiveContext _parentContext

_nullLegalValue

protected boolean _nullLegalValue
Constructor Detail

BaseListCommonStream

public BaseListCommonStream()
Method Detail

invokeIndex

public abstract E invokeIndex(int index)
Calculate the value at an index.
So it gets the needed input value and call the appropriate invoke function.
Can this be used for a filter call?
The index does not make sense for a filter since you do not know where the input is coming from. So maybe just ignore it.
This is a little messy but less messy that what is there now.


hasNext

public boolean hasNext()
Specified by:
hasNext in interface java.util.Iterator<E>

calcAddNext

protected boolean calcAddNext()
Try to calculate one more, independent of the _current.


hasNextBase

public boolean hasNextBase()

setLastFromInput

protected void setLastFromInput()

next

public E next()
Get next element and advance _current.

Specified by:
next in interface java.util.Iterator<E>

get

public E get(int inputIndex)
Get next element without advancing _current.

Specified by:
get in interface NumberedStream<E>
Returns:

getLast

public int getLast()
Description copied from interface: NumberedStream
Last possible element.
If last is not known then this is set to LAST_UNKNOWN.
When you add a new element this will not grow.
When maxLast is set so it this, but this can can get lower.
Set when you find the end.

If you use this for iteration you need to call the function at each iteration.

Specified by:
getLast in interface NumberedStream<E>

getMaxLast

public int getMaxLast()
Description copied from interface: NumberedStream
Manually set max value for last possible element.

Specified by:
getMaxLast in interface NumberedStream<E>

setMaxLast

public void setMaxLast(int maxLast)
Set a max value for last possible element. This also set last.

Specified by:
setMaxLast in interface NumberedStream<E>

isRandomAccess

public boolean isRandomAccess()
Description copied from interface: StreamProperties
If you can calculate 1 element independent of other elements.

Specified by:
isRandomAccess in interface StreamProperties

isCached

public boolean isCached()
If there is a list that contains all the results. I would think that this should always happen for a ListStream, but it could happen for other streams as well.

Specified by:
isCached in interface StreamProperties

isNullLegalValue

public boolean isNullLegalValue()
Description copied from interface: StreamProperties
If null is a legal value. Otherwise when a null if encountered the stream has ended.

Specified by:
isNullLegalValue in interface StreamProperties

setNullLegalValue

public void setNullLegalValue(boolean nullLegalValue)

getCurrentSize

public int getCurrentSize()

getList

public java.util.List<E> getList()
Description copied from interface: ListStream
Get underlying list.

Specified by:
getList in interface ListStream<E>

setList

public void setList(java.util.List<E> list)
Description copied from interface: ListStream
Get underlying list.

Specified by:
setList in interface ListStream<E>

getIndex

public int getIndex()
Description copied from interface: NumberedStream
Index of last successfully processed element.

Specified by:
getIndex in interface NumberedStream<E>

getName

public java.lang.String getName()
Description copied from interface: StreamProperties
If a stream has a name.

Specified by:
getName in interface StreamProperties

setup

public void setup()
Description copied from interface: LazyCalc
Currently not super well define used for 2 purposes: Reset: called from the outside if you want to reuse it init: Called from the inside when calculation start to have everything setup maybe the semantic could work for both? TODO: This should be separated better.

Specified by:
setup in interface LazyCalc<E>

getContext

public java.util.Map getContext()
Description copied from interface: StreamProperties
The context if needed. Maybe it would be better to make a link to the task that contains the context.

Specified by:
getContext in interface RecursiveContext
Specified by:
getContext in interface StreamProperties

getParentContext

public RecursiveContext getParentContext()
Specified by:
getParentContext in interface RecursiveContext

getValue

public E getValue()
Description copied from interface: CalcValue
Starts a lazy calculation. If dirty do calc() else return cached value. This should maybe be moved up in the hierarchy

Specified by:
getValue in interface CalcValue<E>

isDeterministic

public boolean isDeterministic()
Description copied from interface: StreamProperties
Is the result of a calculation deterministic.

Specified by:
isDeterministic in interface StreamProperties

isDirty

public boolean isDirty()
Description copied from interface: LazyCalc
When dirty is false that means that the calculated value can be used

Specified by:
isDirty in interface LazyCalc<E>

remove

public void remove()
Specified by:
remove in interface java.util.Iterator<E>

iterator

public java.util.Iterator<E> iterator()
Specified by:
iterator in interface java.lang.Iterable<E>

getInContext

public java.lang.Object getInContext(java.lang.Object key)
Specified by:
getInContext in interface ContextGettable


Copyright © 2008. All Rights Reserved.