1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.list;
18
19 import java.lang.reflect.InvocationTargetException;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.ListIterator;
26 import java.util.Objects;
27 import java.util.Set;
28 import java.util.function.Predicate;
29
30 import org.apache.commons.collections4.ListUtils;
31 import org.apache.commons.collections4.iterators.AbstractIteratorDecorator;
32 import org.apache.commons.collections4.iterators.AbstractListIteratorDecorator;
33 import org.apache.commons.collections4.set.UnmodifiableSet;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
58
59
60
61
62 static class SetListIterator<E> extends AbstractIteratorDecorator<E> {
63
64 private final Set<E> set;
65 private E last;
66
67 protected SetListIterator(final Iterator<E> it, final Set<E> set) {
68 super(it);
69 this.set = set;
70 }
71
72 @Override
73 public E next() {
74 last = super.next();
75 return last;
76 }
77
78 @Override
79 public void remove() {
80 super.remove();
81 set.remove(last);
82 last = null;
83 }
84 }
85
86
87
88
89 static class SetListListIterator<E> extends
90 AbstractListIteratorDecorator<E> {
91
92 private final Set<E> set;
93 private E last;
94
95 protected SetListListIterator(final ListIterator<E> it, final Set<E> set) {
96 super(it);
97 this.set = set;
98 }
99
100 @Override
101 public void add(final E object) {
102 if (!set.contains(object)) {
103 super.add(object);
104 set.add(object);
105 }
106 }
107
108 @Override
109 public E next() {
110 last = super.next();
111 return last;
112 }
113
114 @Override
115 public E previous() {
116 last = super.previous();
117 return last;
118 }
119
120 @Override
121 public void remove() {
122 super.remove();
123 set.remove(last);
124 last = null;
125 }
126
127 @Override
128 public void set(final E object) {
129 throw new UnsupportedOperationException("ListIterator does not support set");
130 }
131 }
132
133
134 private static final long serialVersionUID = 7196982186153478694L;
135
136
137
138
139
140
141
142
143
144
145
146
147
148 public static <E> SetUniqueList<E> setUniqueList(final List<E> list) {
149 Objects.requireNonNull(list, "list");
150 if (list.isEmpty()) {
151 return new SetUniqueList<>(list, new HashSet<>());
152 }
153 final List<E> temp = new ArrayList<>(list);
154 list.clear();
155 final SetUniqueList<E> sl = new SetUniqueList<>(list, new HashSet<>());
156 sl.addAll(temp);
157 return sl;
158 }
159
160
161 private final Set<E> set;
162
163
164
165
166
167
168
169
170
171
172 protected SetUniqueList(final List<E> list, final Set<E> set) {
173 super(list);
174 this.set = Objects.requireNonNull(set, "set");
175 }
176
177
178
179
180
181
182
183
184
185
186
187 @Override
188 public boolean add(final E object) {
189
190 final int sizeBefore = size();
191
192
193 add(size(), object);
194
195
196 return sizeBefore != size();
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210 @Override
211 public void add(final int index, final E object) {
212
213 if (!set.contains(object)) {
214 set.add(object);
215 super.add(index, object);
216 }
217 }
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232 @Override
233 public boolean addAll(final Collection<? extends E> coll) {
234 return addAll(size(), coll);
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252 @Override
253 public boolean addAll(final int index, final Collection<? extends E> coll) {
254 final List<E> temp = new ArrayList<>();
255 for (final E e : coll) {
256 if (set.add(e)) {
257 temp.add(e);
258 }
259 }
260 return super.addAll(index, temp);
261 }
262
263
264
265
266
267
268 public Set<E> asSet() {
269 return UnmodifiableSet.unmodifiableSet(set);
270 }
271
272 @Override
273 public void clear() {
274 super.clear();
275 set.clear();
276 }
277
278 @Override
279 public boolean contains(final Object object) {
280 return set.contains(object);
281 }
282
283 @Override
284 public boolean containsAll(final Collection<?> coll) {
285 return set.containsAll(coll);
286 }
287
288
289
290
291
292
293
294
295
296
297 protected Set<E> createSetBasedOnList(final Set<E> set, final List<E> list) {
298 Set<E> subSet;
299 if (set.getClass().equals(HashSet.class)) {
300 subSet = new HashSet<>(list.size());
301 } else {
302 try {
303 subSet = set.getClass().getDeclaredConstructor(set.getClass()).newInstance(set);
304 } catch (final InstantiationException
305 | IllegalAccessException
306 | InvocationTargetException
307 | NoSuchMethodException ie) {
308 subSet = new HashSet<>();
309 }
310 }
311 subSet.addAll(list);
312 return subSet;
313 }
314
315 @Override
316 public Iterator<E> iterator() {
317 return new SetListIterator<>(super.iterator(), set);
318 }
319
320 @Override
321 public ListIterator<E> listIterator() {
322 return new SetListListIterator<>(super.listIterator(), set);
323 }
324
325 @Override
326 public ListIterator<E> listIterator(final int index) {
327 return new SetListListIterator<>(super.listIterator(index), set);
328 }
329
330 @Override
331 public E remove(final int index) {
332 final E result = super.remove(index);
333 set.remove(result);
334 return result;
335 }
336
337 @Override
338 public boolean remove(final Object object) {
339 final boolean result = set.remove(object);
340 if (result) {
341 super.remove(object);
342 }
343 return result;
344 }
345
346 @Override
347 public boolean removeAll(final Collection<?> coll) {
348 boolean result = false;
349 for (final Object name : coll) {
350 result |= remove(name);
351 }
352 return result;
353 }
354
355
356
357
358 @Override
359 public boolean removeIf(final Predicate<? super E> filter) {
360 final boolean result = super.removeIf(filter);
361 set.removeIf(filter);
362 return result;
363 }
364
365
366
367
368
369
370
371
372
373
374 @Override
375 public boolean retainAll(final Collection<?> coll) {
376 final boolean result = set.retainAll(coll);
377 if (!result) {
378 return false;
379 }
380 if (set.isEmpty()) {
381 super.clear();
382 } else {
383
384 super.retainAll(set);
385 }
386 return result;
387 }
388
389
390
391
392
393
394
395
396
397
398
399
400 @Override
401 public E set(final int index, final E object) {
402 final int pos = indexOf(object);
403 final E removed = super.set(index, object);
404
405 if (pos != -1 && pos != index) {
406
407
408 super.remove(pos);
409 }
410
411 set.remove(removed);
412 set.add(object);
413
414 return removed;
415 }
416
417
418
419
420
421
422
423 @Override
424 public List<E> subList(final int fromIndex, final int toIndex) {
425 final List<E> superSubList = super.subList(fromIndex, toIndex);
426 final Set<E> subSet = createSetBasedOnList(set, superSubList);
427 return ListUtils.unmodifiableList(new SetUniqueList<>(superSubList, subSet));
428 }
429
430 }