Java Enum Methods

I would like to declare an enumeration direction that has a method that returns the opposite direction (the following is not syntactically correct, i.e. enums cannot be created, but this illustrates my point). Is this possible in Java?

Here is the code:

public enum Direction { NORTH(1), SOUTH(-1), EAST(-2), WEST(2); Direction(int code){ this.code=code; } protected int code; public int getCode() { return this.code; } static Direction getOppositeDirection(Direction d){ return new Direction(d.getCode() * -1); } } 
+100
java enums enumeration
Sep 18 '13 at 22:55
source share
6 answers

For those who lure here by name: yes, you can define your own methods in enum. If you are interested in how to call such a non-static method, you do it the same way as any other non-static method - you call it in an instance of the type that defines or inherits this method. In the case of transfers, such instances are simply ENUM_CONSTANT .

So all you need is EnumType.ENUM_CONSTANT.methodName(arguments) .




Now let's get back to the issue from the question. One solution might be

 public enum Direction { NORTH, SOUTH, EAST, WEST; private Direction opposite; static { NORTH.opposite = SOUTH; SOUTH.opposite = NORTH; EAST.opposite = WEST; WEST.opposite = EAST; } public Direction getOppositeDirection() { return opposite; } } 

Now Direction.NORTH.getOppositeDirection() will return Direction.SOUTH .




Here's a slightly more β€œhacky” way to illustrate @ jedwards comment, but it’s not as flexible as the first approach, since adding more fields or changing their order will break our code.

 public enum Direction { NORTH, EAST, SOUTH, WEST; // cached values to avoid recreating such array each time method is called private static final Direction[] VALUES = values(); public Direction getOppositeDirection() { return VALUES[(ordinal() + 2) % 4]; } } 
+188
Sep 18 '13 at 23:01
source share

For a small listing like this, I find the most readable solution is:

 public enum Direction { NORTH { @Override public Direction getOppositeDirection() { return SOUTH; } }, SOUTH { @Override public Direction getOppositeDirection() { return NORTH; } }, EAST { @Override public Direction getOppositeDirection() { return WEST; } }, WEST { @Override public Direction getOppositeDirection() { return EAST; } }; public abstract Direction getOppositeDirection(); } 
+143
Sep 18 '13 at 23:06 on
source share

It works:

 public enum Direction { NORTH, SOUTH, EAST, WEST; public Direction oppose() { switch(this) { case NORTH: return SOUTH; case SOUTH: return NORTH; case EAST: return WEST; case WEST: return EAST; } throw new RuntimeException("Case not implemented"); } } 
+22
Nov 19 '14 at 17:50
source share

Create an abstract method, and each of your enumeration values ​​will override it. Since you know the opposite when you create it, there is no need to dynamically generate or create it.

It is not well read; maybe switch will be more manageable?

 public enum Direction { NORTH(1) { @Override public Direction getOppositeDirection() { return Direction.SOUTH; } }, SOUTH(-1) { @Override public Direction getOppositeDirection() { return Direction.NORTH; } }, EAST(-2) { @Override public Direction getOppositeDirection() { return Direction.WEST; } }, WEST(2) { @Override public Direction getOppositeDirection() { return Direction.EAST; } }; Direction(int code){ this.code=code; } protected int code; public int getCode() { return this.code; } public abstract Direction getOppositeDirection(); } 
+14
Sep 18 '13 at 23:04 on
source share

Yes, we do this all the time. You are returning a static instance instead of a new object

  static Direction getOppositeDirection(Direction d){ Direction result = null; if (d != null){ int newCode = -d.getCode(); for (Direction direction : Direction.values()){ if (d.getCode() == newCode){ result = direction; } } } return result; } 
+5
Sep 18 '13 at 23:00
source share
 public enum Direction { NORTH, EAST, SOUTH, WEST; public Direction getOppositeDirection(){ return Direction.values()[(this.ordinal() + 2) % 4]; } } 

Enumerations have a static value method that returns an array containing all the enumeration values ​​in the order in which they are declared. source

since NORTH gets 1, EAST gets 2, SOUTH gets 3, WEST gets 4; you can create a simple equation to get the opposite:

(value + 2)% 4

0
Jun 26 '17 at 19:48
source share



All Articles