1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.util.AbstractCollection;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.Iterator;
|
---|
7 | import java.util.NoSuchElementException;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Filtered view of a collection.
|
---|
11 | * (read-only collection, but elements can be changed, of course)
|
---|
12 | * Lets you iterate through those elements of a given collection that satisfy a
|
---|
13 | * certain condition (imposed by a predicate).
|
---|
14 | * <p>
|
---|
15 | * The behaviour of this class is undefined if the underlying collection is changed.
|
---|
16 | * @param <S> element type of the underlying collection
|
---|
17 | * @param <T> element type of filtered collection (and subclass of S). The predicate
|
---|
18 | * must accept only objects of type T.
|
---|
19 | * @since 3147
|
---|
20 | */
|
---|
21 | public class SubclassFilteredCollection<S, T extends S> extends AbstractCollection<T> {
|
---|
22 |
|
---|
23 | private final Collection<? extends S> collection;
|
---|
24 | private final java.util.function.Predicate<? super S> predicate;
|
---|
25 | private int size = -1;
|
---|
26 |
|
---|
27 | private class FilterIterator implements Iterator<T> {
|
---|
28 |
|
---|
29 | private final Iterator<? extends S> iterator;
|
---|
30 | private S current;
|
---|
31 |
|
---|
32 | FilterIterator(Iterator<? extends S> iterator) {
|
---|
33 | this.iterator = iterator;
|
---|
34 | }
|
---|
35 |
|
---|
36 | private void findNext() {
|
---|
37 | if (current == null) {
|
---|
38 | while (iterator.hasNext()) {
|
---|
39 | current = iterator.next();
|
---|
40 | if (predicate.test(current))
|
---|
41 | return;
|
---|
42 | }
|
---|
43 | current = null;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public boolean hasNext() {
|
---|
49 | findNext();
|
---|
50 | return current != null;
|
---|
51 | }
|
---|
52 |
|
---|
53 | @SuppressWarnings("unchecked")
|
---|
54 | @Override
|
---|
55 | public T next() {
|
---|
56 | if (!hasNext())
|
---|
57 | throw new NoSuchElementException();
|
---|
58 | S old = current;
|
---|
59 | current = null;
|
---|
60 | // we are save because predicate only accepts objects of type T
|
---|
61 | return (T) old;
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public void remove() {
|
---|
66 | throw new UnsupportedOperationException();
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Constructs a new {@code SubclassFilteredCollection}.
|
---|
72 | * @param collection The base collection to filter
|
---|
73 | * @param predicate The predicate to use as filter
|
---|
74 | * @deprecated Use java predicates instead.
|
---|
75 | */
|
---|
76 | @Deprecated
|
---|
77 | public SubclassFilteredCollection(Collection<? extends S> collection, Predicate<? super S> predicate) {
|
---|
78 | this(collection, (java.util.function.Predicate<? super S>) predicate);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Constructs a new {@code SubclassFilteredCollection}.
|
---|
83 | * @param collection The base collection to filter
|
---|
84 | * @param predicate The predicate to use as filter
|
---|
85 | * @see #filter(Collection, java.util.function.Predicate) for an alternative way to construct this.
|
---|
86 | */
|
---|
87 | public SubclassFilteredCollection(Collection<? extends S> collection, java.util.function.Predicate<? super S> predicate) {
|
---|
88 | this.collection = collection;
|
---|
89 | this.predicate = predicate;
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public Iterator<T> iterator() {
|
---|
94 | return new FilterIterator(collection.iterator());
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | public int size() {
|
---|
99 | if (size == -1) {
|
---|
100 | size = 0;
|
---|
101 | Iterator<T> it = iterator();
|
---|
102 | while (it.hasNext()) {
|
---|
103 | size++;
|
---|
104 | it.next();
|
---|
105 | }
|
---|
106 | }
|
---|
107 | return size;
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | public boolean isEmpty() {
|
---|
112 | return !iterator().hasNext();
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Create a new filtered collection without any constraints on the predicate type.
|
---|
117 | * @param <T> The collection type.
|
---|
118 | * @param collection The collection to filter.
|
---|
119 | * @param predicate The predicate to filter for.
|
---|
120 | * @return The filtered collection. It is a {@code Collection<T>}.
|
---|
121 | */
|
---|
122 | public static <T> SubclassFilteredCollection<T, T> filter(Collection<T> collection, java.util.function.Predicate<T> predicate) {
|
---|
123 | return new SubclassFilteredCollection<>(collection, predicate);
|
---|
124 | }
|
---|
125 | }
|
---|