package iterator; import exceptions.BoundaryViolationException; import position.Position; import position.PositionList; import java.util.Iterator; import java.util.NoSuchElementException; /** * Created with MONSTER. * User: xgiovio * Date: 06/04/2014 * Time: 21:06 */ //iterable of positions implementation with cursor for PositionList data structures. public class IterablePosition implements Iterable> { PositionList new_structure = null; public IterablePosition(PositionList structure){ new_structure = structure; } @Override public Iterator> iterator() { return new getpositionsiterator(new_structure); } class getpositionsiterator implements Iterator> { PositionList new_structure = null; Position pos = null; public getpositionsiterator (PositionList in){ new_structure = in; } @Override public boolean hasNext() { if (pos == null){ if (new_structure. size() <= 0){ return false; } else { return true; } } else { try { new_structure.next(pos); return true; } catch (BoundaryViolationException err){ return false; } } } @Override public Position next() throws NoSuchElementException { if (hasNext()){ if (pos == null){ pos = new_structure.first(); }else { pos = new_structure.next(pos); } return pos; } else{ throw new NoSuchElementException(); } } @Override public void remove() { throw new UnsupportedOperationException (); } } }