KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.commons.logging.LogFactory;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13
14 /**
15  * A bean that takes an iterator and outputs a subset of it.
16  *
17  * @author Rickard Öberg (rickard@middleware-company.com)
18  * @version $Revision: 1.7 $
19  */

20 public class SubsetIteratorFilter extends IteratorFilterSupport implements Iterator JavaDoc, Action {
21     //~ Instance fields ////////////////////////////////////////////////////////
22

23     Iterator JavaDoc iterator;
24     Object JavaDoc source;
25     int count = -1;
26     int currentCount = 0;
27
28     // Attributes ----------------------------------------------------
29
int start = 0;
30
31     //~ Methods ////////////////////////////////////////////////////////////////
32

33     public void setCount(int aCount) {
34         this.count = aCount;
35     }
36
37     // Public --------------------------------------------------------
38
public void setSource(Object JavaDoc anIterator) {
39         source = anIterator;
40     }
41
42     public void setStart(int aStart) {
43         this.start = aStart;
44     }
45
46     // Action implementation -----------------------------------------
47
public String JavaDoc execute() {
48         if (source == null) {
49             LogFactory.getLog(SubsetIteratorFilter.class.getName()).warn("Source is null returning empty set.");
50
51             return ERROR;
52         }
53
54         // Make source transformations
55
source = getIterator(source);
56
57         // Calculate iterator filter
58
if (source instanceof Iterator JavaDoc) {
59             iterator = (Iterator JavaDoc) source;
60
61             // Read away <start> items
62
for (int i = 0; (i < start) && iterator.hasNext(); i++) {
63                 iterator.next();
64             }
65         } else if (source.getClass().isArray()) {
66             ArrayList JavaDoc list = new ArrayList JavaDoc(((Object JavaDoc[]) source).length);
67             Object JavaDoc[] objects = (Object JavaDoc[]) source;
68             int len = objects.length;
69
70             if (count != -1) {
71                 len -= count;
72             }
73
74             for (int j = start; j < len; j++) {
75                 list.add(objects[j]);
76             }
77
78             count = -1; // Don't have to check this in the iterator code
79
iterator = list.iterator();
80         }
81
82         if (iterator == null) {
83             throw new IllegalArgumentException JavaDoc("Source is not an iterator:" + source);
84         }
85
86         return SUCCESS;
87     }
88
89     // Iterator implementation ---------------------------------------
90
public boolean hasNext() {
91         return (iterator == null) ? false : (iterator.hasNext() && ((count == -1) || (currentCount < count)));
92     }
93
94     public Object JavaDoc next() {
95         currentCount++;
96
97         return iterator.next();
98     }
99
100     public void remove() {
101         iterator.remove();
102     }
103 }
104
Popular Tags