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