org.apache.velocity.tools.generic
public class Alternator extends java.lang.Object
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
Modifier and Type | Field and Description |
---|---|
private boolean |
auto |
private int |
index |
private java.lang.Object[] |
list |
Constructor and Description |
---|
Alternator(boolean auto,
java.lang.Object... list)
Creates a new Alternator for the specified list with the specified
automatic shifting preference.
|
Alternator(java.lang.Object... list)
Creates a new Alternator for the specified list.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
getCurrent()
Returns the current item without shifting the list index.
|
java.lang.Object |
getNext()
Returns the current item, then shifts the list index.
|
boolean |
isAuto() |
void |
setAuto(boolean auto)
If set to true, the list index will shift automatically after a
call to toString().
|
void |
shift()
Manually shifts the list index.
|
java.lang.String |
toString()
Returns a string representation of the current item or
null if the current item is null. |
public Alternator(java.lang.Object... list)
public Alternator(boolean auto, java.lang.Object... list)
auto
- See setAuto(boolean auto)
.list
- The elements to alternate overpublic boolean isAuto()
toString()
.public void setAuto(boolean auto)
public void shift()
public java.lang.Object getCurrent()
public java.lang.Object getNext()
public java.lang.String toString()
null
if the current item is null. If auto
is true, this will shift after returning the current
item.toString
in class java.lang.Object
Copyright (c) 2003-2007 Apache Software Foundation