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

org.securegraph.util.SelectManyIterable Maven / Gradle / Ivy

The newest version!
package org.securegraph.util;

import java.util.Iterator;

public abstract class SelectManyIterable implements Iterable {
    private final Iterable source;

    public SelectManyIterable(Iterable source) {
        this.source = source;
    }

    @Override
    public Iterator iterator() {
        final Iterator it = source.iterator();
        return new Iterator() {
            private TDest next;
            private TDest current;
            public Iterator innerIterator = null;

            @Override
            public boolean hasNext() {
                loadNext();
                return next != null;
            }

            @Override
            public TDest next() {
                loadNext();
                this.current = this.next;
                this.next = null;
                return this.current;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }

            private void loadNext() {
                if (this.next != null) {
                    return;
                }
                while (true) {
                    if (innerIterator != null) {
                        if (innerIterator.hasNext()) {
                            TDest dest = innerIterator.next();
                            if (isIncluded(dest)) {
                                this.next = dest;
                                return;
                            }
                        } else {
                            innerIterator = null;
                        }
                    } else {
                        if (it.hasNext()) {
                            TSource nextSource = it.next();
                            innerIterator = getIterable(nextSource).iterator();
                        } else {
                            return;
                        }
                    }
                }
            }
        };
    }

    protected boolean isIncluded(TDest dest) {
        return true;
    }

    protected abstract Iterable getIterable(TSource source);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy