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