// License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.data.osm; import java.util.AbstractCollection; import java.util.Collection; import java.util.Iterator; import org.openstreetmap.josm.tools.Predicate; public class DatasetCollection extends AbstractCollection { private class FilterIterator implements Iterator { private final Iterator iterator; private OsmPrimitive current; public FilterIterator(Iterator iterator) { this.iterator = iterator; } private void findNext() { if (current == null) { while (iterator.hasNext()) { current = iterator.next(); if (predicate.evaluate(current)) return; } current = null; } } public boolean hasNext() { findNext(); return current != null; } @SuppressWarnings("unchecked") public T next() { findNext(); OsmPrimitive old = current; current = null; return (T)old; } public void remove() { throw new UnsupportedOperationException(); } } private final Collection primitives; private final Predicate predicate; int size = -1; public DatasetCollection(Collection primitives, Predicate predicate) { this.primitives = primitives; this.predicate = predicate; } @Override public Iterator iterator() { return new FilterIterator(primitives.iterator()); } @Override public int size() { if (size == -1) { size = 0; Iterator it = iterator(); while (it.hasNext()) { size++; it.next(); } } return size; } @Override public boolean isEmpty() { return !iterator().hasNext(); } }