Another alternative :) using the map. This is pretty verbose, but this way you can identify each pair only once , another direction will be drawn.
enum Baz { YIN, YANG, GOOD, EVIL; private static final Map<Baz, Baz> opposites = new EnumMap<>(Baz.class); static { opposites.put(YIN, YANG); opposites.put(GOOD, EVIL); for (Entry<Baz, Baz> entry : opposites.entrySet()) { opposites.put(entry.getValue(), entry.getKey()); } } public Baz opposite() { return opposites.get(this); } }
Personally, I like the second meriton example.
Natix
source share