package fr.pandacube.lib.core.util; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; public class IteratorIterator implements Iterator { public static IteratorIterator ofCollectionOfIterable(Collection> coll) { return new IteratorIterator<>(coll.stream().map(i -> i.iterator()).iterator()); } public static IteratorIterator ofCollectionOfIterator(Collection> coll) { return new IteratorIterator<>(new ArrayList<>(coll).iterator()); } @SafeVarargs public static IteratorIterator ofArrayOfIterable(Iterable... arr) { return new IteratorIterator<>(Arrays.stream(arr).map(i -> i.iterator()).iterator()); } @SafeVarargs public static IteratorIterator ofArrayOfIterator(Iterator... arr) { return new IteratorIterator<>(Arrays.asList(arr).iterator()); } private Iterator> iterators; private Iterator currentIterator = null; private IteratorIterator(Iterator> iterators) { this.iterators = iterators; } private void fixCurrentIterator() { if (currentIterator != null && !currentIterator.hasNext()) { currentIterator = null; } } private void fixState() { fixCurrentIterator(); while (currentIterator == null && iterators.hasNext()) { currentIterator = iterators.next(); fixCurrentIterator(); } } @Override public boolean hasNext() { fixState(); return currentIterator != null && currentIterator.hasNext(); } @Override public T next() { if (!hasNext()) throw new NoSuchElementException("No next value found in iterator."); return currentIterator.next(); } }