org.apache.velocity.tools.generic

## Class LoopTool.ManagedIterator

- java.lang.Object
- - org.apache.velocity.tools.generic.LoopTool.ManagedIterator

All Implemented Interfaces:
  Iterator

Enclosing class:
  LoopTool

    public class LoopTool.ManagedIterator
    extends Object
    implements Iterator

Iterator implementation that wraps a standard `Iterator` and allows it to be prematurely stopped, skipped ahead, and associated with a name for advanced nested loop control. This also allows a arbitrary `LoopTool.ActionCondition`s to be added in order to have it automatically skip over or stop before certain elements in the iterator.

### Constructor Summary

Constructors
Constructor and Description
`ManagedIterator(String name, Iterator iterator, LoopTool owner)`

### Method Summary

All Methods Instance Methods Concrete Methods
`LoopTool.ManagedIterator` `condition(LoopTool.ActionCondition condition)` Adds a new `LoopTool.ActionCondition` for this instance to check against the elements in the iterator being managed.
`LoopTool.ManagedIterator` `exclude(Object compare)` Directs this instance to completely exclude any elements equal to the specified Object.
`Object` `get(String name)`
`int` `getCount()`
`boolean` `getFirst()`
`boolean` `getHasNext()`
`int` `getIndex()`
`boolean` `getLast()`
`Map<String,Object>` `getLastSyncedValues()`
`String` `getName()`
`boolean` `hasNext()`
`boolean` `isFirst()`
`boolean` `isLast()`
`boolean` `isSyncedWith(String name)`
`Object` `next()`
`void` `remove()` This operation is unsupported.
`void` `stop()` Stops this iterator from doing any further iteration.
`LoopTool.ManagedIterator` `stop(Object compare)` Directs this instance to stop iterating immediately prior to any element equal to the specified Object.
`LoopTool.ManagedIterator` `sync(Object iterable)` Adds another iterator to be kept in sync with the one being managed by this instance.
`LoopTool.ManagedIterator` `sync(Object iterable, String name)` Adds another iterator to be kept in sync with the one being managed by this instance.
`String` `toString()`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface java.util.Iterator

`forEachRemaining`

### Constructor Detail

#### ManagedIterator

    public ManagedIterator(String name,
                           Iterator iterator,
                           LoopTool owner)

### Method Detail

#### getName

    public String getName()

Returns:
  the name of this instance.

#### isFirst

    public boolean isFirst()

Returns:
  true if either 0 or 1 elements have been returned by `next()`.

#### isLast

    public boolean isLast()

Returns:
  true if the last element returned by `next()` is the last element available in the iterator being managed which satisfies any/all `LoopTool.ActionCondition`s set for this instance. Otherwise, returns false.

#### getFirst

    public boolean getFirst()

Returns:
  the result of `isFirst()`. Exists to allow $loop.this.first syntax.

#### getLast

    public boolean getLast()

Returns:
  the result of `isLast()`. Exists to allow $loop.this.last syntax.

#### hasNext

    public boolean hasNext()

Specified by:
  `hasNext` in interface `Iterator`
Returns:
  true if there are more elements in the iterator being managed by this instance which satisfy all the `LoopTool.ActionCondition`s set for this instance. Returns false if there are no more valid elements available.

#### getHasNext

    public boolean getHasNext()

Returns:
  the result of `hasNext()`. Exists to allow $loop.this.hasNext syntax.

#### isSyncedWith

    public boolean isSyncedWith(String name)

Parameters:
  `name` - syn'ed iterator name
Returns:
  `true` if this ManagedIterator has a sync'ed iterator with the specified name.

#### get

    public Object get(String name)

Parameters:
  `name` - iterator 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 return `null`.

#### getCount

    public int getCount()

Returns:
  the number of elements returned by `next()` so far.

#### getIndex

    public int getIndex()

Returns:
  the 0-based index of the current item.

#### next

    public Object next()

Specified by:
  `next` in interface `Iterator`
Returns:
  the next element that meets the set `LoopTool.ActionCondition`s (if any) in the iterator being managed. If there are none left, then this will throw a `NoSuchElementException`.

#### remove

    public void remove()

This operation is unsupported.

Specified by:
  `remove` in interface `Iterator`

#### stop

    public void stop()

Stops this iterator from doing any further iteration.

#### exclude

    public LoopTool.ManagedIterator exclude(Object compare)

Directs this instance to completely exclude any elements equal to the specified Object.

Parameters:
  `compare` - value to exclude
Returns:
  This same `LoopTool.ManagedIterator` instance

#### stop

    public LoopTool.ManagedIterator stop(Object compare)

Directs this instance to stop iterating immediately prior to any element equal to the specified Object.

Parameters:
  `compare` - value to stop at
Returns:
  This same `LoopTool.ManagedIterator` instance

#### condition

    public LoopTool.ManagedIterator condition(LoopTool.ActionCondition condition)

Adds a new `LoopTool.ActionCondition` for this instance to check against the elements in the iterator being managed.

Parameters:
  `condition` - condition
Returns:
  This same `LoopTool.ManagedIterator` instance

#### sync

    public LoopTool.ManagedIterator sync(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.synced 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.

Parameters:
  `iterable` - Iterator to synchronize with
Returns:
  This same `LoopTool.ManagedIterator` instance
See Also:
  `LoopTool.SyncedIterator`, `get(String)`

#### sync

    public LoopTool.ManagedIterator sync(Object iterable,
                                         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.

Parameters:
  `iterable` - iterator to synchronize with
  `name` - loop name
Returns:
  This same `LoopTool.ManagedIterator` instance
See Also:
  `LoopTool.SyncedIterator`, `get(String)`

#### getLastSyncedValues

    public Map<String,Object> getLastSyncedValues()

Returns:
  last sync'ed values

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`