Class Alternator
- java.lang.Object
-
- org.apache.velocity.tools.generic.Alternator
-
public class Alternator extends java.lang.ObjectUtility 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$
-
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.ObjectgetCurrent()Returns the current item without shifting the list index.java.lang.ObjectgetNext()Returns the current item, then shifts the list index.booleanisAuto()voidsetAuto(boolean auto)If set to true, the list index will shift automatically after a call to toString().voidshift()Manually shifts the list index.java.lang.StringtoString()Returns a string representation of the current item ornullif the current item is null.
-
-
-
Constructor Detail
-
Alternator
public Alternator(java.lang.Object... list)
Creates a new Alternator for the specified list. Alternation defaults to automatic.- Parameters:
list- the elements to alternate over
-
Alternator
public Alternator(boolean auto, java.lang.Object... list)Creates a new Alternator for the specified list with the specified automatic shifting preference.- Parameters:
auto- SeesetAuto(boolean auto).list- The elements to alternate over
-
-
Method Detail
-
isAuto
public boolean isAuto()
- Returns:
- Whether this Alternator shifts the list index
automatically after a call to
toString().
-
setAuto
public void setAuto(boolean auto)
If set to true, the list index will shift automatically after a call to toString().- Parameters:
auto- flag value
-
shift
public void shift()
Manually shifts the list index. If it reaches the end of the list, it will start over again at zero.
-
getCurrent
public java.lang.Object getCurrent()
Returns the current item without shifting the list index.- Returns:
- current item
-
getNext
public java.lang.Object getNext()
Returns the current item, then shifts the list index.- Returns:
- current item
-
toString
public java.lang.String toString()
Returns a string representation of the current item ornullif the current item is null. Ifautois true, this will shift after returning the current item.- Overrides:
toStringin classjava.lang.Object- Returns:
- string representation of the current item
-
-