1 5 package com.opensymphony.webwork.util; 6 7 import com.opensymphony.xwork.Action; 8 9 import java.util.ArrayList ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.StringTokenizer ; 13 14 15 21 public class IteratorGenerator implements Iterator , Action { 22 24 List values; 25 Object value; 26 String separator; 27 28 int count = 0; 30 int currentCount = 0; 31 32 34 public void setCount(int aCount) { 35 this.count = aCount; 36 } 37 38 public boolean getHasNext() { 39 return hasNext(); 40 } 41 42 public Object getNext() { 43 return next(); 44 } 45 46 public void setSeparator(String aChar) { 47 separator = aChar; 48 } 49 50 public void setValues(Object aValue) { 52 value = aValue; 53 } 54 55 public String execute() { 57 if (value == null) { 58 return ERROR; 59 } else { 60 values = new ArrayList (); 61 62 if (separator != null) { 63 StringTokenizer tokens = new StringTokenizer (value.toString(), separator); 64 65 while (tokens.hasMoreTokens()) { 66 values.add(tokens.nextToken()); 67 } 68 } else { 69 values.add(value.toString()); 70 } 71 72 if (count == 0) { 74 count = values.size(); 75 } 76 77 return SUCCESS; 78 } 79 } 80 81 public boolean hasNext() { 83 return (value == null) ? false : ((currentCount < count) || (count == -1)); 84 } 85 86 public Object next() { 87 try { 88 return values.get(currentCount % values.size()); 89 } finally { 90 currentCount++; 91 } 92 } 93 94 public void remove() { 95 throw new UnsupportedOperationException ("Remove is not supported in IteratorGenerator."); 96 } 97 } 98 | Popular Tags |