001: /**
002: * $Id: CollectionIterator.java,v 1.2 2002/06/01 22:16:15 sorensen Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wireless.taglibs.base;
014:
015: import java.util.*;
016:
017: /**
018: * CollectionIterator - collection iteration bean
019: *
020: * The state of an iteration over a collection.
021: * It is used by CollectionTag to make the
022: * iteration state available as bean properties.
023: *
024: * @author Robert O'Brien
025: * @version 1.0
026: * @see com.sun.portal.wireless.taglibs.base.CollectionTag
027: */
028: public class CollectionIterator implements Iterator {
029:
030: /**
031: * The collection to iterate
032: */
033: protected Collection collection;
034:
035: /**
036: * Object array from the collection
037: */
038: protected Object[] objects;
039:
040: /**
041: * The current index into the iteration
042: */
043: protected int index;
044:
045: /**
046: * The current index into the iteration
047: */
048: protected int start;
049:
050: /**
051: * The number of iterations
052: */
053: protected int count;
054:
055: /**
056: * Iteration related properties...
057: */
058: private int end;
059: private int startOne, endOne;
060: private int nextStart, prevStart;
061: private int size;
062: private boolean nextPage, prevPage;
063:
064: /**
065: * Recompute the various iteration limits...
066: */
067: private void computeLimits() {
068:
069: if ((prevStart = start - count) < 0) {
070: prevStart = 0;
071: }
072:
073: if (start == 0) {
074: prevPage = false;
075: } else {
076: prevPage = true;
077: }
078:
079: if ((nextStart = start + count) >= size) {
080: count = size - start;
081: nextPage = false;
082: } else {
083: nextPage = true;
084: }
085:
086: end = start + count;
087: startOne = start + 1;
088: endOne = end + 1;
089: index = start;
090:
091: }
092:
093: /**
094: * Construct an iteration for a collection
095: *
096: * @param collection the collection to iterate over
097: */
098: public CollectionIterator(Collection collection) {
099: this (collection, 0, collection.size());
100: }
101:
102: /**
103: * Construct an iteration for a collection
104: *
105: * @param collection the collection to iterate over
106: * @param start the starting index of the iteration
107: * @param count the number of iterations
108: */
109: public CollectionIterator(Collection collection, int start,
110: int count) {
111: this .collection = collection;
112: this .start = start;
113: this .count = count;
114: this .objects = collection.toArray();
115: this .size = objects.length;
116:
117: computeLimits();
118:
119: }
120:
121: /**
122: * Return the collection being iterated
123: */
124: public Collection getCollection() {
125: return collection;
126: }
127:
128: /**
129: * Returns true if the iteration has more elements
130: *
131: * @return true if the iteration has more items
132: * false if the iteration is complete
133: */
134: public boolean hasNext() {
135: return (index < end);
136: }
137:
138: /**
139: * Returns the next element in the iteration
140: *
141: * @return the next object
142: */
143: public Object next() {
144: if (index < end) {
145: return objects[index++];
146: }
147: return null;
148: }
149:
150: /**
151: * Removes from the underlying collection the last element
152: * returned by the iterator; not supported
153: *
154: * @throws UnsupportedOperationException
155: */
156: public void remove() throws UnsupportedOperationException {
157: throw new UnsupportedOperationException();
158: }
159:
160: /**
161: * Get the index of the current item
162: *
163: * @return index of the current item
164: */
165: public int getIndex() {
166: return index - 1;
167: }
168:
169: /**
170: * Get the one-based index of the current item
171: *
172: * @return index of the current item
173: */
174: public int getIndexOne() {
175: return index;
176: }
177:
178: /***********************
179: * Process properties...
180: **********************/
181:
182: /**
183: * Process "start" property...
184: */
185:
186: public String getStart() {
187: return Integer.toString(start);
188: }
189:
190: public int getStartInt() {
191: return start;
192: }
193:
194: public void setStart(String s) {
195: if (s == null || s.length() == 0) {
196: start = 0;
197: } else
198: try {
199: start = Integer.parseInt(s);
200: } catch (Exception e) {
201: start = 0;
202: }
203:
204: computeLimits();
205: }
206:
207: /**
208: * Process "count" property...
209: */
210:
211: public String getCount() {
212: return Integer.toString(count);
213: }
214:
215: public int getCountInt() {
216: return count;
217: }
218:
219: public void setCount(String s) {
220: if (s == null || s.length() == 0) {
221: count = size;
222: } else
223: try {
224: count = Integer.parseInt(s);
225: } catch (Exception e) {
226: count = 0;
227: }
228:
229: computeLimits();
230: }
231:
232: /**
233: * Process "end" property...
234: */
235:
236: public int getEnd() {
237: return end - 1;
238: }
239:
240: /**
241: * Process "startOne" property...
242: */
243:
244: public int getStartOne() {
245: return startOne;
246: }
247:
248: /**
249: * Process "endOne" property...
250: */
251:
252: public int getEndOne() {
253: return endOne - 1;
254: }
255:
256: /**
257: * Process "size" property...
258: */
259:
260: public int getSize() {
261: return size;
262: }
263:
264: /**
265: * Process "nextPage" property...
266: */
267:
268: public boolean isNextPage() {
269: return nextPage;
270: }
271:
272: /**
273: * Process "prevPage" property...
274: */
275:
276: public boolean isPrevPage() {
277: return prevPage;
278: }
279:
280: /**
281: * Process "nextStart" property...
282: */
283:
284: public int getNextStart() {
285: return nextStart;
286: }
287:
288: /**
289: * Process "prevStart" property...
290: */
291:
292: public int getPrevStart() {
293: return prevStart;
294: }
295:
296: }
|