001: package org.apache.velocity.tools.generic;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.List;
023:
024: /**
025: * Utility class for easily alternating over values in a list.
026: *
027: * <p><b>Example usage:</b>
028: * <pre>
029: * java...
030: * String[] myColors = new String[]{"red", "blue"};
031: * context.put("color", new Alternator(myColors));
032: * String[] myStyles = new String[]{"hip", "fly", "groovy"};
033: * // demonstrate manual alternation with this one
034: * context.put("style", new Alternator(false, myStyles));
035: *
036: * template...
037: * #foreach( $foo in [1..5] )
038: * $foo is $color and $style.next
039: * #end
040: *
041: * output...
042: * 1 is red and hip
043: * 2 is blue and fly
044: * 3 is red and groovy
045: * 4 is blue and hip
046: * 5 is red and fly
047: * </pre></p>
048: *
049: * @since Velocity Tools 1.2
050: * @version $Id: Alternator.java 477914 2006-11-21 21:52:11Z henning $
051: */
052: public class Alternator {
053: private Object[] list;
054: private int index = 0;
055: private boolean auto = true;
056:
057: /**
058: * Creates a new Alternator for the specified list. Alternation
059: * defaults to automatic.
060: */
061: public Alternator(List list) {
062: this (true, list);
063: }
064:
065: /**
066: * Creates a new Alternator for the specified list. Alternation
067: * defaults to automatic.
068: */
069: public Alternator(Object[] list) {
070: this (true, list);
071: }
072:
073: /**
074: * Creates a new Alternator for the specified list with the specified
075: * automatic shifting preference.
076: *
077: * @param auto See {@link #setAuto(boolean auto)}.
078: * @param list The (non-<code>null</code>) list of elements to
079: * alternate.
080: */
081: public Alternator(boolean auto, List list) {
082: this (auto, list.toArray(new Object[list.size()]));
083: }
084:
085: /**
086: * Creates a new Alternator for the specified list with the specified
087: * automatic shifting preference.
088: *
089: * @param auto See {@link #setAuto(boolean auto)}.
090: * @param list The (non-<code>null</code>) list of elements to
091: * alternate.
092: */
093: public Alternator(boolean auto, Object[] list) {
094: this .auto = auto;
095: this .list = list;
096: }
097:
098: /**
099: * @return Whether this Alternator shifts the list index
100: * automatically after a call to {@link #toString()}.
101: */
102: public boolean isAuto() {
103: return auto;
104: }
105:
106: /**
107: * If set to true, the list index will shift automatically after a
108: * call to toString().
109: */
110: public void setAuto(boolean auto) {
111: this .auto = auto;
112: }
113:
114: /**
115: * Manually shifts the list index. If it reaches the end of the list,
116: * it will start over again at zero.
117: */
118: public void shift() {
119: index = (index + 1) % list.length;
120: }
121:
122: /**
123: * Returns the current item without shifting the list index.
124: */
125: public Object getCurrent() {
126: return list[index];
127: }
128:
129: /**
130: * Returns the current item, then shifts the list index.
131: */
132: public Object getNext() {
133: Object o = getCurrent();
134: shift();
135: return o;
136: }
137:
138: /**
139: * Returns a string representation of the current item or
140: * <code>null</code> if the current item is null. <b>If {@link
141: * #auto} is true, this will shift after returning the current
142: * item</b>.
143: */
144: public String toString() {
145: Object o = list[index];
146: if (auto) {
147: shift();
148: }
149: if (o == null) {
150: return null;
151: }
152: return o.toString();
153: }
154:
155: }
|