Elegant way of returning opposite enum: 以优雅的方式返回相反的枚举值

huangapple go评论74阅读模式
英文:

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(() -&gt; SOUTH), SOUTH(() -&gt; NORTH), EAST(() -&gt; WEST), WEST(() -&gt; EAST);

    private final Supplier&lt;Direction&gt; opposite;

    Direction(Supplier&lt;Direction&gt; 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 -&gt; SOUTH;
            case SOUTH -&gt; NORTH;
            case EAST -&gt; WEST;
            case WEST -&gt; EAST;
        }
    }
}

Or a map:

enum Direction {
    NORTH, SOUTH, EAST, WEST;

    private static final Map&lt;Direction,Direction&gt; 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(&quot;SOUTH&quot;), SOUTH(&quot;NORTH&quot;), EAST(&quot;WEST&quot;), WEST(&quot;EAST&quot;);

    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];
}

huangapple
  • 本文由 发表于 2020年8月2日 09:21:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63211504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定