1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.batch.core.step.item;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24
25
26
27
28
29
30
31
32
33
34
35 public class Chunk<W> implements Iterable<W> {
36
37 private List<W> items = new ArrayList<W>();
38
39 private List<SkipWrapper<W>> skips = new ArrayList<SkipWrapper<W>>();
40
41 private List<Exception> errors = new ArrayList<Exception>();
42
43 private Object userData;
44
45 private boolean end;
46
47 private boolean busy;
48
49 public Chunk() {
50 this(null, null);
51 }
52
53 public Chunk(Collection<? extends W> items) {
54 this(items, null);
55 }
56
57 public Chunk(Collection<? extends W> items, List<SkipWrapper<W>> skips) {
58 super();
59 if (items != null) {
60 this.items = new ArrayList<W>(items);
61 }
62 if (skips != null) {
63 this.skips = new ArrayList<SkipWrapper<W>>(skips);
64 }
65 }
66
67
68
69
70
71 public void add(W item) {
72 items.add(item);
73 }
74
75
76
77
78 public void clear() {
79 items.clear();
80 skips.clear();
81 userData = null;
82 }
83
84
85
86
87 public List<W> getItems() {
88 return Collections.unmodifiableList(new ArrayList<W>(items));
89 }
90
91
92
93
94 public List<SkipWrapper<W>> getSkips() {
95 return Collections.unmodifiableList(skips);
96 }
97
98
99
100
101 public List<Exception> getErrors() {
102 return Collections.unmodifiableList(errors);
103 }
104
105
106
107
108
109
110
111 public void skip(Exception e) {
112 errors.add(e);
113 }
114
115
116
117
118 public boolean isEmpty() {
119 return items.isEmpty();
120 }
121
122
123
124
125
126 public ChunkIterator iterator() {
127 return new ChunkIterator(items);
128 }
129
130
131
132
133 public int size() {
134 return items.size();
135 }
136
137
138
139
140
141
142 public boolean isEnd() {
143 return end;
144 }
145
146
147
148
149
150 public void setEnd() {
151 this.end = true;
152 }
153
154
155
156
157
158
159
160 public boolean isBusy() {
161 return busy;
162 }
163
164
165
166
167
168
169
170 public void setBusy(boolean busy) {
171 this.busy = busy;
172 }
173
174
175
176
177 public void clearSkips() {
178 skips.clear();
179 }
180
181 public Object getUserData() {
182 return userData;
183 }
184
185 public void setUserData(Object userData) {
186 this.userData = userData;
187 }
188
189
190
191
192
193
194 @Override
195 public String toString() {
196 return String.format("[items=%s, skips=%s]", items, skips);
197 }
198
199
200
201
202
203
204
205
206 public class ChunkIterator implements Iterator<W> {
207
208 final private Iterator<W> iterator;
209
210 private W next;
211
212 public ChunkIterator(List<W> items) {
213 iterator = items.iterator();
214 }
215
216 public boolean hasNext() {
217 return iterator.hasNext();
218 }
219
220 public W next() {
221 next = iterator.next();
222 return next;
223 }
224
225 public void remove(Throwable e) {
226 remove();
227 skips.add(new SkipWrapper<W>(next, e));
228 }
229
230 public void remove() {
231 if (next == null) {
232 if (iterator.hasNext()) {
233 next = iterator.next();
234 }
235 else {
236 return;
237 }
238 }
239 iterator.remove();
240 }
241
242 @Override
243 public String toString() {
244 return String.format("[items=%s, skips=%s]", items, skips);
245 }
246
247 }
248
249 }