Changeset 3801 in josm for trunk/src/org/openstreetmap/josm/tools/SubclassFilteredCollection.java
- Timestamp:
- 2011-01-22T14:36:36+01:00 (13 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/SubclassFilteredCollection.java
r3800 r3801 1 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm. data.osm;2 package org.openstreetmap.josm.tools; 3 3 4 4 import java.util.AbstractCollection; … … 6 6 import java.util.Iterator; 7 7 8 import org.openstreetmap.josm.tools.Predicate; 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 except only objects of type T. 16 */ 17 public class SubclassFilteredCollection<S, T extends S> extends AbstractCollection<T> { 9 18 10 public class DatasetCollection<T extends OsmPrimitive> extends AbstractCollection<T> { 19 private final Collection<? extends S> collection; 20 private final Predicate<? super S> predicate; 21 int size = -1; 11 22 12 23 private class FilterIterator implements Iterator<T> { 13 24 14 private final Iterator<? extends OsmPrimitive> iterator;15 private OsmPrimitivecurrent;25 private final Iterator<? extends S> iterator; 26 private S current; 16 27 17 public FilterIterator(Iterator<? extends OsmPrimitive> iterator) {28 public FilterIterator(Iterator<? extends S> iterator) { 18 29 this.iterator = iterator; 19 30 } … … 35 46 } 36 47 37 @SuppressWarnings("unchecked")38 48 public T next() { 39 49 findNext(); 40 OsmPrimitiveold = current;50 S old = current; 41 51 current = null; 42 return (T) old;52 return (T) old; 43 53 } 44 54 … … 48 58 } 49 59 50 private final Collection<? extends OsmPrimitive> primitives; 51 private final Predicate<OsmPrimitive> predicate; 52 int size = -1; 53 54 public DatasetCollection(Collection<? extends OsmPrimitive> primitives, Predicate<OsmPrimitive> predicate) { 55 this.primitives = primitives; 60 public SubclassFilteredCollection(Collection<? extends S> collection, Predicate<? super S> predicate) { 61 this.collection = collection; 56 62 this.predicate = predicate; 57 63 } … … 59 65 @Override 60 66 public Iterator<T> iterator() { 61 return new FilterIterator( primitives.iterator());67 return new FilterIterator(collection.iterator()); 62 68 } 63 69
Note:
See TracChangeset
for help on using the changeset viewer.