1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.util;
23
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import com.opensymphony.xwork2.Action;
29 import com.opensymphony.xwork2.util.logging.Logger;
30 import com.opensymphony.xwork2.util.logging.LoggerFactory;
31
32
33 /***
34 * A bean that takes an iterator and outputs a subset of it.
35 *
36 */
37 public class SubsetIteratorFilter extends IteratorFilterSupport implements Iterator, Action {
38
39 private static final Logger LOG = LoggerFactory.getLogger(SubsetIteratorFilter.class);
40
41 Iterator iterator;
42 Object source;
43 int count = -1;
44 int currentCount = 0;
45
46 Decider decider;
47
48
49 int start = 0;
50
51
52 public void setCount(int aCount) {
53 this.count = aCount;
54 }
55
56
57 public void setSource(Object anIterator) {
58 source = anIterator;
59 }
60
61 public void setStart(int aStart) {
62 this.start = aStart;
63 }
64
65 public void setDecider(Decider aDecider) {
66 this.decider = aDecider;
67 }
68
69
70 public String execute() {
71 if (source == null) {
72 LoggerFactory.getLogger(SubsetIteratorFilter.class.getName()).warn("Source is null returning empty set.");
73
74 return ERROR;
75 }
76
77
78 source = getIterator(source);
79
80
81 if (source instanceof Iterator) {
82 iterator = (Iterator) source;
83
84
85
86 for (int i = 0; (i < start) && iterator.hasNext(); i++) {
87 iterator.next();
88 }
89
90
91
92 if (decider != null) {
93 List list = new ArrayList();
94 while(iterator.hasNext()) {
95 Object currentElement = iterator.next();
96 if (decide(currentElement)) {
97 list.add(currentElement);
98 }
99 }
100 iterator = list.iterator();
101 }
102
103 } else if (source.getClass().isArray()) {
104 ArrayList list = new ArrayList(((Object[]) source).length);
105 Object[] objects = (Object[]) source;
106 int len = objects.length;
107
108 if (count >= 0) {
109 len = start + count;
110 if (len > objects.length) {
111 len = objects.length;
112 }
113 }
114
115 for (int j = start; j < len; j++) {
116 if (decide(objects[j])) {
117 list.add(objects[j]);
118 }
119 }
120
121 count = -1;
122 iterator = list.iterator();
123 }
124
125 if (iterator == null) {
126 throw new IllegalArgumentException("Source is not an iterator:" + source);
127 }
128
129 return SUCCESS;
130 }
131
132
133 public boolean hasNext() {
134 return (iterator == null) ? false : (iterator.hasNext() && ((count < 0) || (currentCount < count)));
135 }
136
137 public Object next() {
138 currentCount++;
139
140 return iterator.next();
141 }
142
143 public void remove() {
144 iterator.remove();
145 }
146
147
148 /***
149 * A decider determines if the given element should be added to the list or not.
150 */
151 public static interface Decider {
152
153 /***
154 * Should the object be added to the list?
155 * @param element the object
156 * @return true to add.
157 * @throws Exception can be thrown.
158 */
159 boolean decide(Object element) throws Exception;
160 }
161
162
163 protected boolean decide(Object element) {
164 if (decider != null) {
165 try {
166 boolean okToAdd = decider.decide(element);
167 return okToAdd;
168 }
169 catch(Exception e) {
170 LOG.warn("decider ["+decider+"] encountered an error while decide adding element ["+element+"], element will be ignored, it will not appeared in subseted iterator", e);
171 return false;
172 }
173 }
174 return true;
175 }
176 }