It would be pretty simple to implement a circular Iterator
:
enum Direction implements Iterable<Direction> { east, north, west, south; @Override public Iterator<Direction> iterator() { return new DirectionIterator(); } class DirectionIterator implements Iterator<Direction> { Direction next = Direction.this; @Override public Direction next() { try { return next; } finally { next = values()[(next.ordinal() + 1) % values().length]; } } @Override public boolean hasNext() { return true; } @Override public void remove() { throw new NotImplementedException(); } } }
Using:
public static void main(String[] args) { Iterator<Direction> it = Direction.north.iterator(); for (int i = 0; i < 10; i++) System.out.println(it.next()); }
Outputs:
north west south east north west south east north west
dacwe source share