source: josm/trunk/src/org/openstreetmap/josm/tools/SubclassFilteredCollection.java@ 10972

Last change on this file since 10972 was 10691, checked in by Don-vip, 8 years ago

see #11390, fix #12890 - finish transition to Java 8 predicates/functions

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.util.AbstractCollection;
5import java.util.Collection;
6import java.util.Iterator;
7import java.util.NoSuchElementException;
8import java.util.function.Predicate;
9
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).
15 * <p>
16 * The behaviour of this class is undefined if the underlying collection is changed.
17 * @param <S> element type of the underlying collection
18 * @param <T> element type of filtered collection (and subclass of S). The predicate
19 * must accept only objects of type T.
20 * @since 3147
21 */
22public class SubclassFilteredCollection<S, T extends S> extends AbstractCollection<T> {
23
24 private final Collection<? extends S> collection;
25 private final Predicate<? super S> predicate;
26 private int size = -1;
27
28 private class FilterIterator implements Iterator<T> {
29
30 private final Iterator<? extends S> iterator;
31 private S current;
32
33 FilterIterator(Iterator<? extends S> iterator) {
34 this.iterator = iterator;
35 }
36
37 private void findNext() {
38 if (current == null) {
39 while (iterator.hasNext()) {
40 current = iterator.next();
41 if (predicate.test(current))
42 return;
43 }
44 current = null;
45 }
46 }
47
48 @Override
49 public boolean hasNext() {
50 findNext();
51 return current != null;
52 }
53
54 @SuppressWarnings("unchecked")
55 @Override
56 public T next() {
57 if (!hasNext())
58 throw new NoSuchElementException();
59 S old = current;
60 current = null;
61 // we are save because predicate only accepts objects of type T
62 return (T) old;
63 }
64
65 @Override
66 public void remove() {
67 throw new UnsupportedOperationException();
68 }
69 }
70
71 /**
72 * Constructs a new {@code SubclassFilteredCollection}.
73 * @param collection The base collection to filter
74 * @param predicate The predicate to use as filter
75 * @see #filter(Collection, Predicate) for an alternative way to construct this.
76 */
77 public SubclassFilteredCollection(Collection<? extends S> collection, Predicate<? super S> predicate) {
78 this.collection = collection;
79 this.predicate = predicate;
80 }
81
82 @Override
83 public Iterator<T> iterator() {
84 return new FilterIterator(collection.iterator());
85 }
86
87 @Override
88 public int size() {
89 if (size == -1) {
90 size = 0;
91 Iterator<T> it = iterator();
92 while (it.hasNext()) {
93 size++;
94 it.next();
95 }
96 }
97 return size;
98 }
99
100 @Override
101 public boolean isEmpty() {
102 return !iterator().hasNext();
103 }
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.
110 * @return The filtered collection. It is a {@code Collection<T>}.
111 */
112 public static <T> SubclassFilteredCollection<T, T> filter(Collection<? extends T> collection, Predicate<T> predicate) {
113 return new SubclassFilteredCollection<>(collection, predicate);
114 }
115}
Note: See TracBrowser for help on using the repository browser.