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