package com.artemis.utils;

import java.util.NoSuchElementException;

/**
 * Base iterator for {@link IntBag}
 * 
 * example use 
 * 
 * IntBag ids;
 * ....
 * final IntBagIterator it = new IntBagIterator(ids);
 * while (it.hasNext())
 *    doSomething(it.next());
 *
 * or
 * 
 * for (final IntIterator it = iterate(ids); it.hasNext();)
 *    doSomething(it.next());
 *  
 */
public class IntBagIterator {

	protected final IntBag intBag;
	protected int index;

	/**
	 * Create iterator for IntBag
	 * @return true if the iteration has more elements
	 */
	public IntBagIterator(final IntBag intBag) {
		this.intBag = intBag;
		index = -1;
	}
	
	/**
	 * Create iterator for IntBag
	 * @return true if the iteration has more elements
	 */
	public IntBagIterator(final IntBag intBag, final int indexBegin) {
		this.intBag = intBag;
		index = indexBegin - 1;
	}

	/**
	 * Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)
	 * @return true if the iteration has more elements
	 */
	public boolean hasNext() {
		return intBag != null && index < intBag.size() - 1;
	}

	/**
	 * Returns the next element in the iteration.
	 * @return the next element in the iteration
	 */
	public int next() {
		if (hasNext()) {
			index++;
			return intBag.get(index);
		}
		throw new NoSuchElementException();
	}
	
	/**
	 * Removes from the underlying collection the last element returned by this iterator (optional operation).
	 * This method can be called only once per call to next().
	 * The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.
	 */
	public void remove() {
		
	}
	
}

@@ -454,16 +454,47 @@
 t index;
+%0A%09protected boolean isNextCall;
 %0A%0A%09/**%0A%09
@@ -664,16 +664,38 @@
 x = -1;%0A
+%09%09isNextCall = false;%0A
 %09%7D%0A%09%0A%09/*
@@ -1362,16 +1362,38 @@
 ndex++;%0A
+%09%09%09isNextCall = true;%0A
 %09%09%09retur
@@ -1809,36 +1809,325 @@
 %0A%09 *
-/%0A%09public void remove() %7B%0A%09%09
+ %0A%09 * @throws IllegalStateException - if the next method has not yet been called, or the remove method has already been called after the last call to the next method%0A%09 */%0A%09public void remove() %7B%0A%09%09if (isNextCall && index %3C intBag.size()) %7B%0A%09%09%09intBag.remove(index);%0A%09%09%09index--;%0A%09%09%7D%0A%09%09throw new IllegalStateException();
 %0A%09%7D%0A

add remove method in IntBagIterator