|
|||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||
java.lang.Object | +--org.apache.velocity.tools.Camwood
A convenience tool for use in #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.
Important note: The method stop() doesn't stop the #foreach immediately, instead it makes the loop terminate after the end of the current iteration. See also the FisherMill class for an alternate way to terminate the current iteration.
Example of use:
Java
----
context.put("cam", new Camwood());
VTL
---
#set ($list = [1, 2, 3, 5, 8, 13])
#set ($numbers = $cam.wrap($list))
#foreach ($item in $numbers)
$item##
#if ($item < 8)$numbers.stop()#end
#end
$numbers.next()
Output
------
1 2 3 5
8
Warning: It is not recommended to call hasNext() directly as this method is used to control the #foreach. Use hasMore() instead.
| Constructor Summary | |
Camwood()
Create a Camwood instance to use as tool. |
|
Camwood(java.lang.Object list)
Create a Camwood instance to use in #foreach. |
|
| Method Summary | |
void |
cont()
Puts a condition to continue the loop. |
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 stop() was not called. |
boolean |
isTool()
Returns true if this object is used as a tool. |
java.lang.Object |
next()
Gets the next element 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. |
Camwood |
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 |
| Constructor Detail |
public Camwood()
public Camwood(java.lang.Object list)
wrapped - The list to wrap.| Method Detail |
public boolean isTool()
wrap(java.lang.Object)public Camwood wrap(java.lang.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 java.lang.Object next()
#foreach( $item in $list )
next in interface java.util.IteratorNoSuchElementException - if there are no more elements in the list.hasMore()public boolean hasNext()
hasNext in interface java.util.IteratorhasMore()
public void remove()
throws java.lang.UnsupportedOperationException
remove in interface java.util.Iteratorjava.lang.UnsupportedOperationException - if the wrapped list iterator doesn't support this
operation.public java.lang.String toString()
toString in class java.lang.Objectpublic boolean hasMore()
next()public void stop()
cont(),
hasMore()public void cont()
stop()
|
|||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||