Ignore:
Timestamp:
2011-01-22T14:36:36+01:00 (13 years ago)
Author:
bastiK
Message:

generalize DatasetCollection.java (make OsmPrimitive a generic type parameter)

File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/SubclassFilteredCollection.java

    r3800 r3801  
    11// License: GPL. For details, see LICENSE file.
    2 package org.openstreetmap.josm.data.osm;
     2package org.openstreetmap.josm.tools;
    33
    44import java.util.AbstractCollection;
     
    66import java.util.Iterator;
    77
    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 */
     17public class SubclassFilteredCollection<S, T extends S> extends AbstractCollection<T> {
    918
    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;
    1122
    1223    private class FilterIterator implements Iterator<T> {
    1324
    14         private final Iterator<? extends OsmPrimitive> iterator;
    15         private OsmPrimitive current;
     25        private final Iterator<? extends S> iterator;
     26        private S current;
    1627
    17         public FilterIterator(Iterator<? extends OsmPrimitive> iterator) {
     28        public FilterIterator(Iterator<? extends S> iterator) {
    1829            this.iterator = iterator;
    1930        }
     
    3546        }
    3647
    37         @SuppressWarnings("unchecked")
    3848        public T next() {
    3949            findNext();
    40             OsmPrimitive old = current;
     50            S old = current;
    4151            current = null;
    42             return (T)old;
     52            return (T) old;
    4353        }
    4454
     
    4858    }
    4959
    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;
    5662        this.predicate = predicate;
    5763    }
     
    5965    @Override
    6066    public Iterator<T> iterator() {
    61         return new FilterIterator(primitives.iterator());
     67        return new FilterIterator(collection.iterator());
    6268    }
    6369
Note: See TracChangeset for help on using the changeset viewer.