Critique of Cardinal Direction Enum I'm working on a simple game in which I need to track the cardinal direction of an object. I experimented with using the enum's ordinal value, as well as using switches for the rotation, but both seemed wrong. This is what I ended up with. Is the following an adequate solution?

// Defines cardinal direction
public enum Direction {
    NORTH(0) {
        @Override
        public String getMessage() {
            return getDegrees() + " degrees due north";
        }
    },
    EAST(90) {
        @Override
        public String getMessage() {
            return getDegrees() + " degrees due east";
        }
    },
    SOUTH(180) {
        @Override
        public String getMessage() {
            return getDegrees() + " degrees due south";
        }
    },
    WEST(270) {
        @Override
        public String getMessage() {
            return getDegrees() + " degrees due west";
        }
    };

    private final int degrees;

    public abstract String getMessage();

    private Direction(final int degrees) {
        this.degrees = degrees;
    }

    public int getDegrees() {
        return degrees;
    }

    private static final Map&lt;Integer, Direction&gt; lookup = new HashMap&lt;Integer, Direction&gt;();

    static {
        for (Direction d : EnumSet.allOf(Direction.class))
            lookup.put(d.getDegrees(), d);
    }

    public static Direction get(int degrees) {
        return lookup.get(degrees);
    }

    public Direction rotateRight() {
        return Direction.get((degrees + 90) % 360);
    }

    public Direction rotateLeft() {
        return Direction.get((degrees + 270) % 360);
    }

}
Conceptually it is a great solution. I can suggest some changes, but in reality they are minor. It would also be 'fun' to tune it  abit, but in the bigger picture the changes would be miniscule... still...

My biggest observation is that the constructors can be simplified a lot. Instead of having each Enum member create a unique method implementation (and that method builds a String each time - though the compiler will probably fix that), you could simplify it a lot with:

private final int degrees;
private final String message;

private Direction(final int degrees, final String name) {
    this.degrees = degrees;
    this.message = degrees + " degrees due " + name;
}

public int getDegrees() {
    return degrees;
}

public String getMessage() {
    return message;
}


With the change to the constructor, you have:


no abstract methods to implement
simple message which is a constant, instead of re-creating it each time you call getMessage()
Your enums are initialized in a simpler way:

NORTH(0, "North"),
EAST(90, "East"),
SOUTH(180, "South"),
WEST(270, "West")



Apart from this, the code is pretty good, but, there is a way you can 'play' with the lookup system to use the ordinals only.... consider the following code (I'll leave it to you to figure out.... ;-):

public static Direction get(final int degrees) {
    int ordinal = ((degrees % 360) / 90) - ( 4 * (degrees % 90));
    return ordinal &lt; 0 ?  null : values()[ordinal];
}