cyclic traversal over enum

enums, java

Solution

Convert to int (through `ordinal()`), cycle and convert back to enum (through `values[i]`).

Like this:

Direction next() {
    return values()[(ordinal() + 1) % values().length];
}

Generic Solution

A more generic way of cycling through enum values is:

class CyclicIterator<T> implements Iterator<T> {

    private final T[] values;
    private int current;

    public CyclicIterator(T[] values) {
        this.values = values;
        this.current = 0;
    }

    @Override
    public boolean hasNext() {
        return true;
    }

    @Override
    public T next() {
        current = (current + 1) % values.length;
        return values[current];
    }
}

Which can be used as follows:

CyclicIterator<Direction> iter = new CyclicIterator<>(Direction.values());
for (int i = 0; i < 6; i++) {
    System.out.println(i + ": " + iter.next());
}

Output:

0: north
1: west
2: south
3: east
4: north
5: west

Guava solution

If you depend on Guava, a cyclic iterator (identical to the one above) is already available through `Iterators.cycle`

Problem

what is the best way of cyclic traversal over an enum. I have an enum Directions and i want to traverse over it cyclically. currently i have implemented next method in enum which returns next value, i was just wondering is there any better way/inbuilt support for cyclic traversal. current code ``` enum Direction { east, north, west, south; Direction next() { switch (this) { case east: return north; case north: return west; case west: return south; case south: return east; } return null; } } ```

Original source