英文:
Elegant way of returning opposite enum
问题
以下是您要翻译的内容:
I have an enum for which each element has an opposite. I'd like an elegant way to encapsulate this inside each element of the enum. My preferred options is not legal as it uses forward references.
enum Direction {
NORTH(SOUTH), SOUTH(NORTH), EAST(WEST), WEST(EAST);
private final Direction opposite;
Direction(Direction opposite) {
this.opposite = opposite;
}
public Direction getOpposite() {
return opposite;
}
}
Using a supplier is also illegal.
enum Direction {
NORTH(() -> SOUTH), SOUTH(() -> NORTH), EAST(() -> WEST), WEST(() -> EAST);
private final Supplier<Direction> opposite;
Direction(Supplier<Direction> opposite) {
this.opposite = opposite;
}
public Direction getOpposite() {
return opposite.get();
}
}
Which leaves me with overriding the method:
enum Direction {
NORTH{
public Direction getOpposite() {
return SOUTH;
}
},
SOUTH{
public Direction getOpposite() {
return NORTH;
}
},
EAST{
public Direction getOpposite() {
return WEST;
}
},
WEST{
public Direction getOpposite() {
return EAST;
}
};
public abstract Direction getOpposite();
}
Using a switch:
enum Direction {
NORTH, SOUTH, EAST, WEST;
public Direction getOpposite() {
return switch(this) {
case NORTH -> SOUTH;
case SOUTH -> NORTH;
case EAST -> WEST;
case WEST -> EAST;
}
}
}
Or a map:
enum Direction {
NORTH, SOUTH, EAST, WEST;
private static final Map<Direction,Direction> OPPOSITES =
Map.of(NORTH, SOUTH, SOUTH, NORTH, EAST, WEST, WEST, EAST);
public Direction getOpposite() {
OPPOSITES.get(this);
}
}
None of the alternatives are as straightforward or readable as just listing the opposite as an argument.
Is there an elegant way to avoid the forward reference issue?
英文:
I have an enum for which each element has an opposite. I'd like an elegant way to encapsulate this inside each element of the enum. My preferred options is not legal as it uses forward references.
enum Direction {
NORTH(SOUTH), SOUTH(NORTH), EAST(WEST), WEST(EAST);
private final Direction opposite;
Direction(Direction opposite) {
this.opposite = opposite;
}
public Direction getOpposite() {
return opposite;
}
}
Using a supplier is also illegal.
enum Direction {
NORTH(() -> SOUTH), SOUTH(() -> NORTH), EAST(() -> WEST), WEST(() -> EAST);
private final Supplier<Direction> opposite;
Direction(Supplier<Direction> opposite) {
this.opposite = opposite;
}
public Direction getOpposite() {
return opposite.get();
}
}
Which leaves me with overriding the method:
enum Direction {
NORTH{
public Direction getOpposite() {
return SOUTH;
}
},
SOUTH{
public Direction getOpposite() {
return NORTH;
}
},
EAST{
public Direction getOpposite() {
return WEST;
}
},
WEST{
public Direction getOpposite() {
return EAST;
}
};
public abstract Direction getOpposite();
}
Using a switch:
enum Direction {
NORTH, SOUTH, EAST, WEST;
public Direction getOpposite() {
return switch(this) {
case NORTH -> SOUTH;
case SOUTH -> NORTH;
case EAST -> WEST;
case WEST -> EAST;
}
}
}
Or a map:
enum Direction {
NORTH, SOUTH, EAST, WEST;
private static final Map<Direction,Direction> OPPOSITES =
Map.of(NORTH, SOUTH, SOUTH, NORTH, EAST, WEST, WEST, EAST);
public Direction getOpposite() {
OPPOSITES.get(this);
}
}
None of the alternatives are as straightforward or readable as just listing the opposite as an argument.
Is there an elegant way to avoid the forward reference issue?
答案1
得分: 1
这是您提供的代码的翻译:
不确定是否稍微好一点:
枚举 Direction {
北("南"), 南("北"), 东("西"), 西("东");
private final String 相反;
Direction(String 相反) {
this.相反 = 相反;
}
public Direction 获取相反() {
return Direction.valueOf(相反);
}
}
英文:
Not sure if it is a little better:
enum Direction {
NORTH("SOUTH"), SOUTH("NORTH"), EAST("WEST"), WEST("EAST");
private final String opposite;
Direction(String opposite) {
this.opposite = opposite;
}
public Direction getOpposite() {
return Direction.valueOf(opposite);
}
}
答案2
得分: 0
我建议你改变顺序:
enum Direction {
NORTH, EAST, SOUTH, WEST:
...
}
并使用 Direction.values()
:
public Direction opposite() {
Direction[] array = values();
return array[(ordinal() + 2) % array.length];
}
英文:
I suggest you change the order:
enum Direction {
NORTH, EAST, WEST, SOUTH:
...
}
And use Direction.values()
:
public Direction opposite() {
Direction[] array = values();
return array[(ordinal() + 2) % array.length];
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论