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 T current = null;
// This must be tracked because there can be null values
private boolean currentIsSet = false;
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(Collections.emptyList()).iterator();
}
@Override
public boolean hasNext() {
if (currentIsSet) {
return next.hasNext() || first.hasNext();
} else {
return next.hasNext() && first.hasNext();
}
}
@Override
public List next() {
if (next.hasNext()) {
if (!currentIsSet) {
current = first.next();
currentIsSet = true;
}
} else {
current = first.next();
this.next = restIterator();
}
return prepend(current, next.next());
}
private List prepend(T head, List tail) {
List rest = new ArrayList<>(tail);
rest.add(0, head);
return rest;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy