org.apache.velocity.tools
Class FisherMill

java.lang.Object
  |
  +--org.apache.velocity.tools.Camwood
        |
        +--org.apache.velocity.tools.FisherMill
All Implemented Interfaces:
java.util.Iterator

public class FisherMill
extends 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.

Example of use:

  
  Java
  ----
  context.put("mill", new FisherMill());
   
  
  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
 

Warning: It is not recommended to call hasNext() directly as this method is used to control the #foreach. Use hasMore() instead.


Field Summary
protected  boolean cachedNext
          True if next contains the value of the next element in the list.
protected  java.lang.Object next
          The next element in the list, if it was cached.
 
Constructor Summary
FisherMill()
          Create a FisherMill instance to use as tool.
FisherMill(java.lang.Object list)
          Create a FisherMill instance to use in #foreach.
 
Method Summary
 void cont()
          Proceeds to the next element in the list.
 boolean hasNext()
          Returns true if there are more elements in the list and more() was called.
 java.lang.Object more()
           Asks for the next element in the list.
 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.
 Camwood wrap(java.lang.Object list)
          Wraps a list with the tool.
 
Methods inherited from class org.apache.velocity.tools.Camwood
hasMore, isTool, stop, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

cachedNext

protected boolean cachedNext
True if next contains the value of the next element in the list.

next

protected java.lang.Object next
The next element in the list, if it was cached.
Constructor Detail

FisherMill

public FisherMill()
Create a FisherMill 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.

FisherMill

public FisherMill(java.lang.Object list)
Create a FisherMill instance to use in #foreach.
Parameters:
list - The list to wrap.
Method Detail

wrap

public Camwood wrap(java.lang.Object list)
Description copied from class: Camwood
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 wrapper iterates over the values.
If the list is an Iterator or an Enumeration, the wrapper can be used only once.
Overrides:
wrap in class Camwood
Following copied from class: org.apache.velocity.tools.Camwood
Parameters:
list - The list to wrap.
Returns:
A new wrapper if this object is used as a tool, or itself if it is a wrapper.

reset

public void reset()
Description copied from class: Camwood

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.

Overrides:
reset in class Camwood

next

public java.lang.Object next()

Gets the next element 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 it if they want to read the value of the next item without doing more().

Overrides:
next in class Camwood
Returns:
The next element in the list.
Throws:
NoSuchElementException - if there are no more elements in the list.
See Also:
more(), Camwood.hasMore()

hasNext

public boolean hasNext()
Returns true if there are more elements in the list and more() was called.
This code always return false:
 tool.hasNext()? tool.hasNext(): false;
Overrides:
hasNext in class Camwood
Returns:
true if there are more elements, and either more() or cont() was called since last call.
See Also:
Camwood.hasMore()

remove

public void remove()
            throws java.lang.UnsupportedOperationException
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().
Overrides:
remove in class Camwood
Throws:
java.lang.UnsupportedOperationException - if the wrapped list iterator doesn't support this operation.

more

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.

Returns:
The next element in the list, or null if there are no more elements.
See Also:
Camwood.hasMore()

cont

public void cont()
Proceeds to the next element in the list. Same as more(), but the current element is not added in the template.
The #foreach loop will continue after this iteration (if there are more elements).
Overrides:
cont in class Camwood
See Also:
more()