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

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

The newest version!
package net.jqwik.engine.support.combinatorics;

import org.jspecify.annotations.*;

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

public class ConcatIterator implements Iterator {

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

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

	private @Nullable 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