com.databasesandlife.util.CompositeIterable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-common Show documentation
Show all versions of java-common Show documentation
Utility classes developed at Adrian Smith Software (A.S.S.)
package com.databasesandlife.util;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Wraps a number of iterables into a single iterable.
* This is especially useful when the iterables themselves are futures.
*
* @param The objects to be iterated over
* @author This source is copyright Adrian Smith and licensed under the LGPL 3.
* @see Project on GitHub
*/
public class CompositeIterable implements Iterable {
Iterable extends Iterable extends T>> iterables;
class I implements Iterator {
Iterator extends T> currentIteratorWithinIterable; // null means finished
Iterator extends Iterable extends T>> iteratorThroughIterables;
public I() {
iteratorThroughIterables = iterables.iterator();
if (iteratorThroughIterables.hasNext()) currentIteratorWithinIterable = iteratorThroughIterables.next().iterator();
else currentIteratorWithinIterable = null;
}
@Override public boolean hasNext() {
while (currentIteratorWithinIterable != null && ! currentIteratorWithinIterable.hasNext()) {
if (iteratorThroughIterables.hasNext()) currentIteratorWithinIterable = iteratorThroughIterables.next().iterator();
else currentIteratorWithinIterable = null;
}
return currentIteratorWithinIterable != null;
}
@Override public T next() {
if ( ! hasNext()) throw new NoSuchElementException();
return currentIteratorWithinIterable.next();
}
@Override public void remove() { throw new UnsupportedOperationException(); }
}
public CompositeIterable(Iterable extends Iterable extends T>> iterables) {
this.iterables = iterables;
}
@Override public Iterator iterator() {
return new I();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy