net.jqwik.engine.support.combinatorics.SetIterator Maven / Gradle / Ivy
The newest version!
package net.jqwik.engine.support.combinatorics;
import org.jspecify.annotations.*;
import java.util.*;
public class SetIterator implements Iterator> {
private final Iterator> combinedListIterator;
private final Set> generatedSets = new LinkedHashSet<>();
private final int setSize;
private Set next;
public SetIterator(Iterable extends T> elementIterable, int setSize) {
this.setSize = setSize;
List> iterables = new ArrayList<>();
for (int i = 0; i < setSize; i++) {
iterables.add(elementIterable);
}
combinedListIterator = new CombinedIterator<>(iterables);
next = findNext();
}
private Set findNext() {
while (combinedListIterator.hasNext()) {
HashSet candidate = new LinkedHashSet<>(combinedListIterator.next());
if (candidate.size() != setSize || generatedSets.contains(candidate)) {
continue;
}
generatedSets.add(candidate);
return candidate;
}
return null;
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public Set next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Set current = next;
next = findNext();
return current;
}
}