net.jqwik.engine.support.combinatorics.CombinedIterator Maven / Gradle / Ivy
package net.jqwik.engine.support.combinatorics;
import java.util.ArrayList;
import java.util.*;
import static java.util.Arrays.*;
public class CombinedIterator implements Iterator> {
private final Iterator first;
private final ArrayList> rest;
private Iterator> next;
private Object current = null;
public CombinedIterator(List> iterables) {
this.rest = new ArrayList<>(iterables);
this.first = this.rest.remove(0).iterator();
this.next = restIterator();
}
private Iterator> restIterator() {
return this.rest.isEmpty()
? emptyListIterator()
: new CombinedIterator<>(this.rest);
}
private Iterator> emptyListIterator() {
return asList((List) new ArrayList()).iterator();
}
@Override
public boolean hasNext() {
if (current == null) {
return next.hasNext() && first.hasNext();
} else {
return next.hasNext() || first.hasNext();
}
}
@Override
public List next() {
if (next.hasNext()) {
if (current == null) {
current = first.next();
}
} else {
current = first.next();
this.next = restIterator();
}
return prepend(current, next.next());
}
@SuppressWarnings("unchecked")
private List prepend(Object head, List tail) {
List rest = new ArrayList(tail);
rest.add(0, head);
return rest;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy