| 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 | |
|---|
| 8 | /** |
|---|
| 9 | * Filtered view of a collection. |
|---|
| 10 | * (read-only collection, but elements can be changed, of course) |
|---|
| 11 | * Lets you iterate through those elements of a given collection that satisfy a |
|---|
| 12 | * certain condition (imposed by a predicate). |
|---|
| 13 | * @param <S> element type of the underlying collection |
|---|
| 14 | * @param <T> element type of filtered collection (and subclass of S). The predicate |
|---|
| 15 | * must accept only objects of type T. |
|---|
| 16 | */ |
|---|
| 17 | public class SubclassFilteredCollection<S, T extends S> extends AbstractCollection<T> { |
|---|
| 18 | |
|---|
| 19 | private final Collection<? extends S> collection; |
|---|
| 20 | private final Predicate<? super S> predicate; |
|---|
| 21 | int size = -1; |
|---|
| 22 | |
|---|
| 23 | private class FilterIterator implements Iterator<T> { |
|---|
| 24 | |
|---|
| 25 | private final Iterator<? extends S> iterator; |
|---|
| 26 | private S current; |
|---|
| 27 | |
|---|
| 28 | public FilterIterator(Iterator<? extends S> iterator) { |
|---|
| 29 | this.iterator = iterator; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | private void findNext() { |
|---|
| 33 | if (current == null) { |
|---|
| 34 | while (iterator.hasNext()) { |
|---|
| 35 | current = iterator.next(); |
|---|
| 36 | if (predicate.evaluate(current)) |
|---|
| 37 | return; |
|---|
| 38 | } |
|---|
| 39 | current = null; |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | public boolean hasNext() { |
|---|
| 44 | findNext(); |
|---|
| 45 | return current != null; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | public T next() { |
|---|
| 49 | findNext(); |
|---|
| 50 | S old = current; |
|---|
| 51 | current = null; |
|---|
| 52 | // we are save because predicate only accepts objects of type T |
|---|
| 53 | @SuppressWarnings("unchecked") T res = (T) old; |
|---|
| 54 | return res; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | public void remove() { |
|---|
| 58 | throw new UnsupportedOperationException(); |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | public SubclassFilteredCollection(Collection<? extends S> collection, Predicate<? super S> predicate) { |
|---|
| 63 | this.collection = collection; |
|---|
| 64 | this.predicate = predicate; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | @Override |
|---|
| 68 | public Iterator<T> iterator() { |
|---|
| 69 | return new FilterIterator(collection.iterator()); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | @Override |
|---|
| 73 | public int size() { |
|---|
| 74 | if (size == -1) { |
|---|
| 75 | size = 0; |
|---|
| 76 | Iterator<T> it = iterator(); |
|---|
| 77 | while (it.hasNext()) { |
|---|
| 78 | size++; |
|---|
| 79 | it.next(); |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | return size; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | @Override |
|---|
| 86 | public boolean isEmpty() { |
|---|
| 87 | return !iterator().hasNext(); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | } |
|---|