All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.aksw.commons.collections.sets.SetIterator Maven / Gradle / Ivy

package org.aksw.commons.collections.sets;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.aksw.commons.collections.SinglePrefetchIterator;

public class SetIterator
    extends SinglePrefetchIterator
{
    protected Collection backend;

    public SetIterator(Collection backend) {
        this.backend = backend;
    }

    protected Set seen = new HashSet<>();
    protected Iterator current = null;

    @Override
    protected T prefetch() throws Exception {
        if(current == null) {
            current = backend.iterator();
        }

        T result;
        while(current.hasNext()) {
            result = current.next();

            if(!seen.contains(result)) {
                seen.add(result);
                return result;
            }
        }

        current = null; // Let's be polite and clean up the reference
        return finish();
    }

    @Override
    protected void doRemove(T item) {
        while(backend.remove(item)) {
            /* remove until no more change */
        }
        // Invalidate the current iterator
        // The next call to .prefetch() will position the underlying iterator to the
        // first item not in the set of seen items
        current = null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy