package com.eactive.eai.transformer.layout;

import java.util.Iterator;
import java.util.Stack;


public class ItemIterator implements Iterator {
	
	Stack stack = new Stack();
	
	public ItemIterator(Iterator iterator) {
		stack.push(iterator);
	}
	
	public boolean hasNext() {
		if (stack.empty()) {
			return false;
		} else {
			Iterator iter = (Iterator) stack.peek();
			if (!iter.hasNext()) {
				stack.pop();
				return hasNext();
			} else {
				return true;
			}
		}
	}

	public Object next() {	
		if (hasNext()) {
			Iterator iter = (Iterator) stack.peek();
			Item item = (Item) iter.next();
			
			if (item instanceof Composite && item.getIndex() != -1) {
				stack.push(item.iterator()); 
			}
			
			return item;
		} else {
			return null;
		}
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException();
	}	
}
