Class LoopTool.ManagedIterator
- java.lang.Object
-
- org.apache.velocity.tools.generic.LoopTool.ManagedIterator
-
- All Implemented Interfaces:
java.util.Iterator
- Enclosing class:
- LoopTool
public static class LoopTool.ManagedIterator extends java.lang.Object implements java.util.Iterator
Iterator implementation that wraps a standardIterator
and allows it to be prematurely stopped, skipped ahead, and associated with a name for advanced nested loop control. This also allows a arbitraryLoopTool.ActionCondition
s to be added in order to have it automatically skip over or stop before certain elements in the iterator.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<LoopTool.ActionCondition>
conditions
private int
count
private java.lang.Boolean
first
private java.util.Iterator
iterator
private java.lang.String
name
private java.lang.Object
next
private LoopTool
owner
private boolean
stopped
private java.util.Map<java.lang.String,LoopTool.SyncedIterator>
synced
-
Constructor Summary
Constructors Constructor Description ManagedIterator(java.lang.String name, java.util.Iterator iterator, LoopTool owner)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private boolean
cacheNext(boolean popWhenDone)
LoopTool.ManagedIterator
condition(LoopTool.ActionCondition condition)
Adds a newLoopTool.ActionCondition
for this instance to check against the elements in the iterator being managed.LoopTool.ManagedIterator
exclude(java.lang.Object compare)
Directs this instance to completely exclude any elements equal to the specified Object.java.lang.Object
get(java.lang.String name)
Returns the parallel value from the specified sync'ed iterator.int
getCount()
Returns the number of elements returned bynext()
so far.boolean
getFirst()
Returns the result ofisFirst()
.boolean
getHasNext()
Returns the result ofhasNext()
.int
getIndex()
Returns the 0-based index of the current item.boolean
getLast()
Returns the result ofisLast()
.java.lang.String
getName()
Returns the name of this instance.boolean
hasNext()
Returns true if there are more elements in the iterator being managed by this instance which satisfy all theLoopTool.ActionCondition
s set for this instance.private boolean
hasNext(boolean popWhenDone)
boolean
isFirst()
Returns true if either 0 or 1 elements have been returned bynext()
.boolean
isLast()
Returns true if the last element returned bynext()
is the last element available in the iterator being managed which satisfies any/allLoopTool.ActionCondition
s set for this instance.boolean
isSyncedWith(java.lang.String name)
Returnstrue
if this ManagedIterator has a sync'ed iterator with the specified name.java.lang.Object
next()
Returns the next element that meets the setLoopTool.ActionCondition
s (if any) in the iterator being managed.void
remove()
This operation is unsupported.private void
shiftSynced()
void
stop()
Stops this iterator from doing any further iteration.LoopTool.ManagedIterator
stop(java.lang.Object compare)
Directs this instance to stop iterating immediately prior to any element equal to the specified Object.LoopTool.ManagedIterator
sync(java.lang.Object iterable)
Adds another iterator to be kept in sync with the one being managed by this instance.LoopTool.ManagedIterator
sync(java.lang.Object iterable, java.lang.String name)
Adds another iterator to be kept in sync with the one being managed by this instance.java.lang.String
toString()
-
-
-
Field Detail
-
name
private java.lang.String name
-
iterator
private java.util.Iterator iterator
-
owner
private LoopTool owner
-
stopped
private boolean stopped
-
first
private java.lang.Boolean first
-
count
private int count
-
next
private java.lang.Object next
-
conditions
private java.util.List<LoopTool.ActionCondition> conditions
-
synced
private java.util.Map<java.lang.String,LoopTool.SyncedIterator> synced
-
-
Constructor Detail
-
ManagedIterator
public ManagedIterator(java.lang.String name, java.util.Iterator iterator, LoopTool owner)
-
-
Method Detail
-
getName
public java.lang.String getName()
Returns the name of this instance.
-
isFirst
public boolean isFirst()
Returns true if either 0 or 1 elements have been returned bynext()
.
-
isLast
public boolean isLast()
Returns true if the last element returned bynext()
is the last element available in the iterator being managed which satisfies any/allLoopTool.ActionCondition
s set for this instance. Otherwise, returns false.
-
getFirst
public boolean getFirst()
Returns the result ofisFirst()
. Exists to allow $loop.this.first syntax.
-
getLast
public boolean getLast()
Returns the result ofisLast()
. Exists to allow $loop.this.last syntax.
-
hasNext
public boolean hasNext()
Returns true if there are more elements in the iterator being managed by this instance which satisfy all theLoopTool.ActionCondition
s set for this instance. Returns false if there are no more valid elements available.- Specified by:
hasNext
in interfacejava.util.Iterator
-
getHasNext
public boolean getHasNext()
Returns the result ofhasNext()
. Exists to allow $loop.this.hasNext syntax.
-
hasNext
private boolean hasNext(boolean popWhenDone)
-
cacheNext
private boolean cacheNext(boolean popWhenDone)
-
shiftSynced
private void shiftSynced()
-
isSyncedWith
public boolean isSyncedWith(java.lang.String name)
Returnstrue
if this ManagedIterator has a sync'ed iterator with the specified name.
-
get
public java.lang.Object get(java.lang.String name)
Returns the parallel value from the specified sync'ed iterator. If no sync'ed iterator exists with that name or that iterator is finished, this will returnnull
.
-
getCount
public int getCount()
Returns the number of elements returned bynext()
so far.
-
getIndex
public int getIndex()
Returns the 0-based index of the current item.
-
next
public java.lang.Object next()
Returns the next element that meets the setLoopTool.ActionCondition
s (if any) in the iterator being managed. If there are none left, then this will throw aNoSuchElementException
.- Specified by:
next
in interfacejava.util.Iterator
-
remove
public void remove()
This operation is unsupported.- Specified by:
remove
in interfacejava.util.Iterator
-
stop
public void stop()
Stops this iterator from doing any further iteration.
-
exclude
public LoopTool.ManagedIterator exclude(java.lang.Object compare)
Directs this instance to completely exclude any elements equal to the specified Object.- Returns:
- This same
LoopTool.ManagedIterator
instance
-
stop
public LoopTool.ManagedIterator stop(java.lang.Object compare)
Directs this instance to stop iterating immediately prior to any element equal to the specified Object.- Returns:
- This same
LoopTool.ManagedIterator
instance
-
condition
public LoopTool.ManagedIterator condition(LoopTool.ActionCondition condition)
Adds a newLoopTool.ActionCondition
for this instance to check against the elements in the iterator being managed.- Returns:
- This same
LoopTool.ManagedIterator
instance
-
sync
public LoopTool.ManagedIterator sync(java.lang.Object iterable)
Adds another iterator to be kept in sync with the one being managed by this instance. The values of the parallel iterator can be retrieved from the LoopTool under the name s"synced" (e.g. $loop.synched or $loop.get('synced')) and are automatically updated for each iteration by this instance.
NOTE: if you are sync'ing multiple iterators with the same managed iterator, you must use
sync(Object,String)
or else your the later iterators will simply replace the earlier ones under the default 'synced' key.- Returns:
- This same
LoopTool.ManagedIterator
instance - See Also:
LoopTool.SyncedIterator
,get(String)
-
sync
public LoopTool.ManagedIterator sync(java.lang.Object iterable, java.lang.String name)
Adds another iterator to be kept in sync with the one being managed by this instance. The values of the parallel iterator can be retrieved from the LoopTool under the name specified here (e.g. $loop.name or $loop.get('name')) and are automatically updated for each iteration by this instance.- Returns:
- This same
LoopTool.ManagedIterator
instance - See Also:
LoopTool.SyncedIterator
,get(String)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-