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