1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.iterators;
18
19 import java.text.MessageFormat;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.ListIterator;
24 import java.util.NoSuchElementException;
25 import java.util.Objects;
26
27 import org.apache.commons.collections4.ResettableListIterator;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class ListIteratorWrapper<E> implements ResettableListIterator<E> {
53
54
55 private static final String UNSUPPORTED_OPERATION_MESSAGE =
56 "ListIteratorWrapper does not support optional operations of ListIterator.";
57
58
59 private static final String CANNOT_REMOVE_MESSAGE = "Cannot remove element at index {0}.";
60
61
62 private final Iterator<? extends E> iterator;
63
64 private final List<E> list = new ArrayList<>();
65
66
67 private int currentIndex;
68
69 private int wrappedIteratorIndex;
70
71 private boolean removeState;
72
73
74
75
76
77
78
79
80 public ListIteratorWrapper(final Iterator<? extends E> iterator) {
81 this.iterator = Objects.requireNonNull(iterator, "iterator");
82 }
83
84
85
86
87
88
89
90
91
92 @Override
93 public void add(final E obj) throws UnsupportedOperationException {
94 if (iterator instanceof ListIterator) {
95 @SuppressWarnings("unchecked")
96 final ListIterator<E> li = (ListIterator<E>) iterator;
97 li.add(obj);
98 return;
99 }
100 throw new UnsupportedOperationException(UNSUPPORTED_OPERATION_MESSAGE);
101 }
102
103
104
105
106
107
108 @Override
109 public boolean hasNext() {
110 if (currentIndex == wrappedIteratorIndex || iterator instanceof ListIterator) {
111 return iterator.hasNext();
112 }
113 return true;
114 }
115
116
117
118
119
120
121 @Override
122 public boolean hasPrevious() {
123 if (iterator instanceof ListIterator) {
124 final ListIterator<?> li = (ListIterator<?>) iterator;
125 return li.hasPrevious();
126 }
127 return currentIndex > 0;
128 }
129
130
131
132
133
134
135
136 @Override
137 public E next() throws NoSuchElementException {
138 if (iterator instanceof ListIterator) {
139 return iterator.next();
140 }
141
142 if (currentIndex < wrappedIteratorIndex) {
143 ++currentIndex;
144 return list.get(currentIndex - 1);
145 }
146
147 final E retval = iterator.next();
148 list.add(retval);
149 ++currentIndex;
150 ++wrappedIteratorIndex;
151 removeState = true;
152 return retval;
153 }
154
155
156
157
158
159
160 @Override
161 public int nextIndex() {
162 if (iterator instanceof ListIterator) {
163 final ListIterator<?> li = (ListIterator<?>) iterator;
164 return li.nextIndex();
165 }
166 return currentIndex;
167 }
168
169
170
171
172
173
174
175 @Override
176 public E previous() throws NoSuchElementException {
177 if (iterator instanceof ListIterator) {
178 @SuppressWarnings("unchecked")
179 final ListIterator<E> li = (ListIterator<E>) iterator;
180 return li.previous();
181 }
182
183 if (currentIndex == 0) {
184 throw new NoSuchElementException();
185 }
186 removeState = wrappedIteratorIndex == currentIndex;
187 return list.get(--currentIndex);
188 }
189
190
191
192
193
194
195 @Override
196 public int previousIndex() {
197 if (iterator instanceof ListIterator) {
198 final ListIterator<?> li = (ListIterator<?>) iterator;
199 return li.previousIndex();
200 }
201 return currentIndex - 1;
202 }
203
204
205
206
207
208
209
210 @Override
211 public void remove() throws IllegalStateException {
212 if (iterator instanceof ListIterator) {
213 iterator.remove();
214 return;
215 }
216 int removeIndex = currentIndex;
217 if (currentIndex == wrappedIteratorIndex) {
218 --removeIndex;
219 }
220 if (!removeState || wrappedIteratorIndex - currentIndex > 1) {
221 throw new IllegalStateException(MessageFormat.format(CANNOT_REMOVE_MESSAGE, Integer.valueOf(removeIndex)));
222 }
223 iterator.remove();
224 list.remove(removeIndex);
225 currentIndex = removeIndex;
226 wrappedIteratorIndex--;
227 removeState = false;
228 }
229
230
231
232
233
234
235
236 @Override
237 public void reset() {
238 if (iterator instanceof ListIterator) {
239 final ListIterator<?> li = (ListIterator<?>) iterator;
240 while (li.previousIndex() >= 0) {
241 li.previous();
242 }
243 return;
244 }
245 currentIndex = 0;
246 }
247
248
249
250
251
252
253
254
255
256 @Override
257 public void set(final E obj) throws UnsupportedOperationException {
258 if (iterator instanceof ListIterator) {
259 @SuppressWarnings("unchecked")
260 final ListIterator<E> li = (ListIterator<E>) iterator;
261 li.set(obj);
262 return;
263 }
264 throw new UnsupportedOperationException(UNSUPPORTED_OPERATION_MESSAGE);
265 }
266
267 }