nbubna 2004/05/03 20:32:22 Added: src/java/org/apache/velocity/tools/generic Alternator.java AlternatorTool.java Log: initial revision of convenient 'alternator' support Revision Changes Path 1.1 jakarta-velocity-tools/src/java/org/apache/velocity/tools/generic/Alternator.java Index: Alternator.java =================================================================== /* * Copyright 2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.velocity.tools.generic; import java.util.List; /** * Utility class for easily alternating over values in a list. * * <p><b>Example Use:</b> * <pre> * java... * List myColors = new ArrayList(); * myColors.add("red"); * myColors.add("blue"); * context.put("color", new Alternator(myColors)); * List myStyles = new ArrayList(); * myColors.add("hip"); * myColors.add("fly"); * myColors.add("groovy"); * // use auto alternation * context.put("style", new Alternator(true, myStyles)); * * template... * #foreach( $foo in [1..5] ) * $foo is $color.next and $style * #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 * </pre></p> * * @version $Revision: 1.1 $ $Date: 2004/05/04 03:32:22 $ */ public class Alternator { private List list; private int index = 0; private boolean auto = false; /** * Creates a new Alternator for the specified list. */ public Alternator(List list) { this.list = list; } /** * Creates a new Alternator for the specified list with the specified * automatic shifting preference. See [EMAIL PROTECTED] #setAuto(boolean auto)}. */ public Alternator(boolean auto, List list) { this.auto = auto; this.list = list; } /** * Returns true if this Alternator shifts the list index automatically * after a call to toString(). */ public boolean isAuto() { return auto; } /** * If set to true, the list index will shift automatically after a * call to toString(). */ public void setAuto(boolean auto) { this.auto = auto; } /** * Manually shifts the list index. If it reaches the end of the list, * it will start over again at zero. */ public void shift() { index = (index + 1) % list.size(); } /** * Returns the current item without shifting the list index. */ public Object getCurrent() { return list.get(index); } /** * Returns the current item before shifting the list index. */ public Object getNext() { Object o = getCurrent(); shift(); return o; } /** * Returns a string representation of the current item or * <code>null</code> if the current item is null. Also, * if <i>auto</i> is true, this will shift after returning * the current item. */ public String toString() { Object o = list.get(index); if (auto) { shift(); } if (o == null) { return null; } return o.toString(); } } 1.1 jakarta-velocity-tools/src/java/org/apache/velocity/tools/generic/AlternatorTool.java Index: AlternatorTool.java =================================================================== /* * Copyright 2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.velocity.tools.generic; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; /** * Simple tool to provide easy in-template instantiation of * [EMAIL PROTECTED] Alternator}s from varying "list" types. * * <p><b>Example Use:</b> * <pre> * toolbox.xml... * <tool> * <key>alternator</key> * <scope>application</scope> * <class>org.apache.velocity.tools.generic.AlternatorTool</class> * </tool> * * template... * #set( $color = $alternator.make('red', 'blue') ) * #set( $style = $alternator.make(true, ['hip','fly','groovy']) ) * #foreach( $i in [1..5] ) * $i is $color.next and $style * #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 * </pre></p> * * @version $Revision: 1.1 $ $Date: 2004/05/04 03:32:22 $ */ public class AlternatorTool { public AlternatorTool() {} /** * Make a non-automatic [EMAIL PROTECTED] Alternator} from a List. */ public Alternator make(List list) { return make(false, list); } /** * Make an [EMAIL PROTECTED] Alternator} from a List. */ public Alternator make(boolean auto, List list) { if (list == null) { return null; } return new Alternator(auto, list); } /** * Make a non-automatic [EMAIL PROTECTED] Alternator} from the values * in the specified collection. */ public Alternator make(Collection collection) { return make(false, collection); } /** * Make an [EMAIL PROTECTED] Alternator} from the values * in the specified collection. */ public Alternator make(boolean auto, Collection collection) { if (collection == null) { return null; } return make(auto, new ArrayList(collection)); } /** * Make a non-automatic [EMAIL PROTECTED] Alternator} from an object array. */ public Alternator make(Object[] array) { return make(false, array); } /** * Make an [EMAIL PROTECTED] Alternator} from an object array. */ public Alternator make(boolean auto, Object[] array) { if (array == null) { return null; } return make(auto, Arrays.asList(array)); } /** * Make a non-automatic [EMAIL PROTECTED] Alternator} from a list containing the two * specified objects. */ public Alternator make(Object o1, Object o2) { return make(false, o1, o2); } /** * Make an [EMAIL PROTECTED] Alternator} from a list containing the two * specified objects. */ public Alternator make(boolean auto, Object o1, Object o2) { List list = new ArrayList(); list.add(o1); list.add(o2); return make(auto, list); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]