Search in sources :

Example 1 with CompositeIterator

use of io.requery.util.CompositeIterator in project requery by requery.

the class ModifiableResult method iterator.

@Override
public CloseableIterator<E> iterator() {
    // iterator that combines query results with not added elements and filtering out
    // removed elements
    final Iterator<E> queried = result == null ? Collections.<E>emptyIterator() : result.iterator();
    final Iterator<E> added = changes.addedElements().iterator();
    final Iterator<E> composite = new CompositeIterator<>(queried, added);
    final Iterator<E> filtered = new FilteringIterator<>(composite, new Predicate<E>() {

        @Override
        public boolean test(E e) {
            return !changes.removedElements().contains(e);
        }
    });
    return new CloseableIterator<E>() {

        @Override
        public void close() {
            if (queried instanceof CloseableIterator) {
                CloseableIterator closeable = (CloseableIterator) queried;
                closeable.close();
            }
        }

        @Override
        public boolean hasNext() {
            return filtered.hasNext();
        }

        @Override
        public E next() {
            return filtered.next();
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : CloseableIterator(io.requery.util.CloseableIterator) CompositeIterator(io.requery.util.CompositeIterator) FilteringIterator(io.requery.util.FilteringIterator)

Aggregations

CloseableIterator (io.requery.util.CloseableIterator)1 CompositeIterator (io.requery.util.CompositeIterator)1 FilteringIterator (io.requery.util.FilteringIterator)1