I\'m programming a game in java which is made up of a grid of tiles. I wan\'t to be able to inuitively define the edges of the tiles and how they relate to each other, e.g.
You can create a static Map where key is the original enum and the value the opposite edge. Initialize it in a static block and the return the mapping from the opposite() method.
private static Map oppostiteMapping;
static {
oppositeMapping = new EnumMap();
oppositeMapping.put(TOP, BOTTOM);
...
}
public Edge opposite() {
return oppositeMapping.get(this);
}
EDIT: as proposed in comment better to use EnumMap, so I upgraded accordingly
Btw. this approach is generally useful when you create something like static fromString() method etc.