| java.lang.Object org.apache.velocity.tools.generic.IteratorTool
IteratorTool | public class IteratorTool implements Iterator(Code) | |
A convenience tool to use with #foreach loops. It wraps a list
to let the designer specify a condition to terminate the loop,
and reuse the same list in different loops.
Example of use:
Java
----
context.put("mill", new IteratorTool());
VTL
---
#set ($list = [1, 2, 3, 5, 8, 13])
#set ($numbers = $mill.wrap($list))
#foreach ($item in $numbers)
#if ($item < 8) $numbers.more()#end
#end
$numbers.more()
Output
------
1 2 3 5
8
Example toolbox.xml config (if you want to use this with VelocityView):
<tool>
<key>mill</key>
<scope>request</scope>
<class>org.apache.velocity.tools.generic.IteratorTool</class>
</tool>
Warning: It is not recommended to use hasNext() with this
tool as it is used to control the #foreach. Use hasMore() instead.
author: Denis Bredelet version: $Id: IteratorTool.java 477914 2006-11-21 21:52:11Z henning $ |
Constructor Summary | |
public | IteratorTool() Create a IteratorTool instance to use as tool.
When it is created this way, the tool returns a new
instance each time wrap() is called. | public | IteratorTool(Object wrapped) Create a IteratorTool instance to use in #foreach. |
Method Summary | |
public boolean | hasMore() Returns true if there are more elements in the wrapped list. | public boolean | hasNext() Returns true if there are more elements in the
list and more() was called. | public Object | more()
Asks for the next element in the list. | public Object | next()
Gets the next object in the list. | public void | remove() Removes the current element from the list. | public void | reset()
Resets the wrapper so that it starts over at the beginning of the list. | public void | stop() Puts a condition to break out of the loop. | public String | toString() Returns this object as a String. | public IteratorTool | wrap(Object list) Wraps a list with the tool.
The list can be an array, a Collection, a Map, an Iterator
or an Enumeration.
If the list is a Map, the tool iterates over the values.
If the list is an Iterator or an Enumeration, the tool can
be used only once.
Parameters: list - The list to wrap. |
IteratorTool | public IteratorTool()(Code) | | Create a IteratorTool instance to use as tool.
When it is created this way, the tool returns a new
instance each time wrap() is called. This is
useful when you want to allow the designers to create instances.
|
IteratorTool | public IteratorTool(Object wrapped)(Code) | | Create a IteratorTool instance to use in #foreach.
Parameters: wrapped - The list to wrap. |
hasMore | public boolean hasMore()(Code) | | Returns true if there are more elements in the wrapped list.
If this object doesn't wrap a list, the method always returns false.
true if there are more elements in the list. |
hasNext | public boolean hasNext()(Code) | | Returns true if there are more elements in the
list and more() was called.
This code always return false:
tool.hasNext()? tool.hasNext(): false;
true if there are more elements, and either more()or hasNext() was called since last call. |
more | public Object more()(Code) | |
Asks for the next element in the list. This method is to be used
by the template designer in #foreach loops.
If this method is called in the body of #foreach, the loop
continues as long as there are elements in the list.
If this method is not called the loop terminates after the
current iteration.
The next element in the list, or null if there are nomore elements. |
next | public Object next()(Code) | |
Gets the next object in the list. This method is called
by #foreach to define $item in:
#foreach( $item in $list )
This method is not intended for template designers, but they can use
them if they want to read the value of the next item without doing
more().
The next item in the list. throws: NoSuchElementException - if there are no moreelements in the list. |
remove | public void remove() throws UnsupportedOperationException(Code) | | Removes the current element from the list.
The current element is defined as the last element that was read
from the list, either with next() or with more().
throws: UnsupportedOperationException - if the wrapped listiterator doesn't support this operation. |
reset | public void reset()(Code) | |
Resets the wrapper so that it starts over at the beginning of the list.
Note to programmers: This method has no effect if the wrapped
object is an enumeration or an iterator.
|
stop | public void stop()(Code) | | Puts a condition to break out of the loop.
The #foreach loop will terminate after this iteration, unless more()
is called after stop().
|
toString | public String toString()(Code) | | Returns this object as a String.
If this object is used as a tool, it just gives the class name.
Otherwise it appends the wrapped list to the class name.
A string representation of this object. |
wrap | public IteratorTool wrap(Object list)(Code) | | Wraps a list with the tool.
The list can be an array, a Collection, a Map, an Iterator
or an Enumeration.
If the list is a Map, the tool iterates over the values.
If the list is an Iterator or an Enumeration, the tool can
be used only once.
Parameters: list - The list to wrap. A new wrapper if this object is used as a tool, oritself if it is a wrapper. |
|
|