All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.jqwik.engine.support.combinatorics.CombinedIterator Maven / Gradle / Ivy

There is a newer version: 1.9.1
Show newest version
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