| java.lang.Object org.apache.velocity.tools.generic.Alternator
Alternator | public class Alternator (Code) | | Utility class for easily alternating over values in a list.
Example usage:
java...
String[] myColors = new String[]{"red", "blue"};
context.put("color", new Alternator(myColors));
String[] myStyles = new String[]{"hip", "fly", "groovy"};
// demonstrate manual alternation with this one
context.put("style", new Alternator(false, myStyles));
template...
#foreach( $foo in [1..5] )
$foo is $color and $style.next
#end
output...
1 is red and hip
2 is blue and fly
3 is red and groovy
4 is blue and hip
5 is red and fly
since: Velocity Tools 1.2 version: $Id: Alternator.java 477914 2006-11-21 21:52:11Z henning $ |
Constructor Summary | |
public | Alternator(List list) Creates a new Alternator for the specified list. | public | Alternator(Object[] list) Creates a new Alternator for the specified list. | public | Alternator(boolean auto, List list) Creates a new Alternator for the specified list with the specified
automatic shifting preference. | public | Alternator(boolean auto, Object[] list) Creates a new Alternator for the specified list with the specified
automatic shifting preference. |
Method Summary | |
public Object | getCurrent() Returns the current item without shifting the list index. | public Object | getNext() Returns the current item, then shifts the list index. | public boolean | isAuto() | public void | setAuto(boolean auto) If set to true, the list index will shift automatically after a
call to toString(). | public void | shift() Manually shifts the list index. | public String | toString() Returns a string representation of the current item or
null if the current item is null. |
Alternator | public Alternator(List list)(Code) | | Creates a new Alternator for the specified list. Alternation
defaults to automatic.
|
Alternator | public Alternator(Object[] list)(Code) | | Creates a new Alternator for the specified list. Alternation
defaults to automatic.
|
Alternator | public Alternator(boolean auto, List list)(Code) | | Creates a new Alternator for the specified list with the specified
automatic shifting preference.
Parameters: auto - See Alternator.setAuto(boolean auto). Parameters: list - The (non-null ) list of elements toalternate. |
Alternator | public Alternator(boolean auto, Object[] list)(Code) | | Creates a new Alternator for the specified list with the specified
automatic shifting preference.
Parameters: auto - See Alternator.setAuto(boolean auto). Parameters: list - The (non-null ) list of elements toalternate. |
getCurrent | public Object getCurrent()(Code) | | Returns the current item without shifting the list index.
|
getNext | public Object getNext()(Code) | | Returns the current item, then shifts the list index.
|
setAuto | public void setAuto(boolean auto)(Code) | | If set to true, the list index will shift automatically after a
call to toString().
|
shift | public void shift()(Code) | | Manually shifts the list index. If it reaches the end of the list,
it will start over again at zero.
|
toString | public String toString()(Code) | | Returns a string representation of the current item or
null if the current item is null. If
Alternator.auto is true, this will shift after returning the current
item.
|
|
|