LoopTool
instead@DefaultKey(value="mill") @Deprecated public class IteratorTool extends Object implements Iterator
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 tools.xml config (if you want to use this with VelocityView): <tools> <toolbox scope="request"> <tool class="org.apache.velocity.tools.generic.IteratorTool"/> </toolbox> </tools>
Warning: It is not recommended to use hasNext() with this tool as it is used to control the #foreach. Use hasMore() instead.
Constructor and Description |
---|
IteratorTool()
Deprecated.
Create a IteratorTool instance to use as tool.
|
IteratorTool(Object wrapped)
Deprecated.
Create a IteratorTool instance to use in #foreach.
|
Modifier and Type | Method and Description |
---|---|
boolean |
hasMore()
Deprecated.
Returns true if there are more elements in the wrapped list.
|
boolean |
hasNext()
Deprecated.
Returns true if there are more elements in the
list and more() was called.
|
Object |
more()
Deprecated.
Asks for the next element in the list.
|
Object |
next()
Deprecated.
Gets the next object in the list.
|
void |
remove()
Deprecated.
Removes the current element from the list.
|
void |
reset()
Deprecated.
Resets the wrapper so that it starts over at the beginning of the list.
|
void |
stop()
Deprecated.
Puts a condition to break out of the loop.
|
String |
toString()
Deprecated.
Returns this object as a String.
|
IteratorTool |
wrap(Object list)
Deprecated.
Wraps a list with the tool.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forEachRemaining
protected Object next
public IteratorTool()
public IteratorTool(Object wrapped)
wrapped
- The list to wrap.public IteratorTool wrap(Object list)
list
- The list to wrap.public void reset()
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.
public Object next()
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().
public boolean hasNext()
tool.hasNext()? tool.hasNext(): false;
public void remove() throws UnsupportedOperationException
remove
in interface Iterator
UnsupportedOperationException
- if the wrapped list
iterator doesn't support this operation.public Object more()
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.
public boolean hasMore()
public void stop()
Copyright © 2002–2021 Apache Software Foundation. All rights reserved.