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

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

There is a newer version: 1.9.1
Show newest version
package net.jqwik.engine.support.combinatorics;

import java.util.*;
import java.util.concurrent.atomic.*;

public class ConcatIterator implements Iterator {

	private final List> iterators;
	private final AtomicInteger position;
	private Iterator next;

	public ConcatIterator(List> iterators) {
		this.iterators = iterators;
		position = new AtomicInteger(0);
		if (!iterators.isEmpty()) {
			next = findNext();
		}
	}

	private Iterator findNext() {
		while (!iterators.get(position.get()).hasNext()) {
			if (position.get() >= iterators.size() -1)
				return null;
			position.getAndIncrement();
		}
		return iterators.get(position.get());
	}

	@Override
	public boolean hasNext() {
		return next != null && next.hasNext();
	}

	@Override
	public T next() {
		if (!hasNext()) {
			throw new NoSuchElementException();
		}
		T current = next.next();
		next = findNext();
		return current;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy