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<

There is a newer version: 0.6
Show newest version
/**
 * 
 */
package monniasza.collects;

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

import com.google.common.collect.Iterators;

/**
 * @author oskar
 * An iterator which iterates over all of its collections
 * @param  type of iterator
 */
public class Iterator2 implements Iterator {
	public static  Iterator2 iterIterCol(Iterator> ic) {
		return iterIterIter(Iterators.transform(ic, Collection::iterator));
	}
	public static  Iterator2 iterIterIter(Iterator> ii) {
		return new Iterator2<>(ii);
	}
	public static  Iterator2 iterColCol(Collection> cc) {
		return iterIterCol(cc.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");
	}}