|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.velocity.tools.generic.IteratorTool
public class IteratorTool
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.
Field Summary | |
---|---|
private boolean |
cachedNext
|
private java.util.Iterator |
iterator
|
protected java.lang.Object |
next
|
private boolean |
wantMore
|
private java.lang.Object |
wrapped
|
Constructor Summary | |
---|---|
IteratorTool()
Create a IteratorTool instance to use as tool. |
|
IteratorTool(java.lang.Object wrapped)
Create a IteratorTool instance to use in #foreach. |
Method Summary | |
---|---|
boolean |
hasMore()
Returns true if there are more elements in the wrapped list. |
boolean |
hasNext()
Returns true if there are more elements in the list and more() was called. |
private void |
internalWrap(java.lang.Object wrapped)
Wraps a list with the tool. |
java.lang.Object |
more()
Asks for the next element in the list. |
java.lang.Object |
next()
Gets the next object in the list. |
void |
remove()
Removes the current element from the list. |
void |
reset()
Resets the wrapper so that it starts over at the beginning of the list. |
void |
stop()
Puts a condition to break out of the loop. |
java.lang.String |
toString()
Returns this object as a String. |
IteratorTool |
wrap(java.lang.Object list)
Wraps a list with the tool. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
private java.lang.Object wrapped
private java.util.Iterator iterator
private boolean wantMore
private boolean cachedNext
protected java.lang.Object next
Constructor Detail |
---|
public IteratorTool()
public IteratorTool(java.lang.Object wrapped)
wrapped
- The list to wrap.Method Detail |
---|
public IteratorTool wrap(java.lang.Object list)
list
- The list to wrap.
private void internalWrap(java.lang.Object wrapped)
wrapped
- 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 java.lang.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().
next
in interface java.util.Iterator
NoSuchElementException
- if there are no more
elements in the list.public boolean hasNext()
tool.hasNext()? tool.hasNext(): false;
hasNext
in interface java.util.Iterator
public void remove() throws java.lang.UnsupportedOperationException
remove
in interface java.util.Iterator
java.lang.UnsupportedOperationException
- if the wrapped list
iterator doesn't support this operation.public java.lang.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()
public java.lang.String toString()
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |