KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > util > IteratorGenerator


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.util;
6
7 import com.opensymphony.xwork.Action;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.StringTokenizer JavaDoc;
13
14
15 /**
16  * A bean that generates an iterator filled with a given object
17  *
18  * @author Rickard Öberg (rickard@middleware-company.com)
19  * @version $Revision: 1.5 $
20  */

21 public class IteratorGenerator implements Iterator JavaDoc, Action {
22     //~ Instance fields ////////////////////////////////////////////////////////
23

24     List JavaDoc values;
25     Object JavaDoc value;
26     String JavaDoc separator;
27
28     // Attributes ----------------------------------------------------
29
int count = 0;
30     int currentCount = 0;
31
32     //~ Methods ////////////////////////////////////////////////////////////////
33

34     public void setCount(int aCount) {
35         this.count = aCount;
36     }
37
38     public boolean getHasNext() {
39         return hasNext();
40     }
41
42     public Object JavaDoc getNext() {
43         return next();
44     }
45
46     public void setSeparator(String JavaDoc aChar) {
47         separator = aChar;
48     }
49
50     // Public --------------------------------------------------------
51
public void setValues(Object JavaDoc aValue) {
52         value = aValue;
53     }
54
55     // Action implementation -----------------------------------------
56
public String JavaDoc execute() {
57         if (value == null) {
58             return ERROR;
59         } else {
60             values = new ArrayList JavaDoc();
61
62             if (separator != null) {
63                 StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(value.toString(), separator);
64
65                 while (tokens.hasMoreTokens()) {
66                     values.add(tokens.nextToken());
67                 }
68             } else {
69                 values.add(value.toString());
70             }
71
72             // Count default is the size of the list of values
73
if (count == 0) {
74                 count = values.size();
75             }
76
77             return SUCCESS;
78         }
79     }
80
81     // Iterator implementation ---------------------------------------
82
public boolean hasNext() {
83         return (value == null) ? false : ((currentCount < count) || (count == -1));
84     }
85
86     public Object JavaDoc 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 JavaDoc("Remove is not supported in IteratorGenerator.");
96     }
97 }
98
Popular Tags