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

monniasza.collects.Iterator2 Maven / Gradle / Ivy

Go to download

Dependency for the MultiMachineBuilder, a voxel game about building an industrial empire in a finite world. THIS RELEASE IS NOT PLAYABLE. To play the game, donwload from >ITCH.IO LINK HERE< or >GH releases link here<

The newest version!
/**
 * 
 */
package monniasza.collects;

import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;

import com.google.common.collect.Iterators;

/**
 * An iterator which iterates over all of its collections
 * @author oskar
 * @param  type of iterator
 */
public class Iterator2 implements Iterator {
	/**
	 * Iterates over an iterator of collections
	 * @param  type of elements
	 * @param ic source collection
	 * @return a double iterator
	 */
	@SuppressWarnings("null")
	public static  Iterator2 iterIterCol(Iterator> ic) {
		return iterIterIter(Iterators.transform(ic, Collection::iterator));
	}
	/**
	 * Iterates over an iterator of iterators
	 * @param ii source collection
	 * @return a double iterator
	 */
	public static  Iterator2 iterIterIter(Iterator> ii) {
		return new Iterator2<>(ii);
	}
	/**
	 * Iterates over a collection of collections
	 * @param  type of elements
	 * @param cc source collection
	 * @return a double iterator
	 */
	public static  Iterator2 iterColCol(Collection> cc) {
		return iterIterCol(cc.iterator());
	}
	/**
	 * Iterates over a collection of iterators
	 * @param  type of elements
	 * @param ci source collection
	 * @return a double iterator
	 */
	public static  Iterator2 iterColIter(Collection> ci) {
		return iterIterIter(ci.iterator());
	}
	private Iterator2(Iterator> iter) {
		this.iter = iter;
	}

	private final Iterator> iter;
	private Iterator subiter;
	private boolean state;
	@Override
	public void remove() {
		if(!state) throw new IllegalStateException("Item not selected");
		if(subiter.hasNext()) subiter.remove();
		if(iter.hasNext()) {
			subiter = iter.next();
			remove();
		}
		state = false;
	}
	@Override
	public boolean hasNext() {
		if(subiter.hasNext()) return true;
		if(iter.hasNext()) {
			subiter = iter.next();
			return hasNext();
		}
		return false;
	}
	@Override
	public T next() {
		state = true;
		if(subiter.hasNext()) subiter.next();
		if(iter.hasNext()) {
			subiter = iter.next();
			return next();
		}
		state = false;
		throw new NoSuchElementException("No more iterators");
	}}