实现响应式定向枚举的最简单方法是什么? java的

时间:2015-03-13 19:35:16

标签: java enums direction

我会以“最清晰”的方式快速展示我想要达到的目标:

public enum Orientation {
    NORTH, WEST, SOUTH, EAST }

public enum Turn {
    LEFT, RIGHT }

所以我希望这两个枚举做的是安全的,并根据移动有效地查找改变的方向:

Orientation orient = Orientation.NORTH;
// orient points to NORTH now
orient = orient.turn(Turn.LEFT);
// orient points to WEST now

我尝试实现这一目标的第一种方法是创建地图:

 EnumMap<Orientation, EnumMap<Turn, Orientation>>

静态地映射所有方向,但这是一大块map.get.put.get ....并且可能有点过量,导致这种期望的效果:

directionMap.get(NORTH).get(LEFT)
// the value would then be WEST

我接下来的方式是通过Compass类将所有方向链接成一个圆圈。像LinkedCircuitList一样工作......&lt; - &gt; NORTH&lt; - &gt; EAST&lt; - &gt; SOUTH&lt; - &gt; WEST&lt ; - &gt; NORTH&lt; - &gt; ...因此,Compass可以有一个静态函数,可以调用该链接列表的任何成员并向左或向右移动,从而导致方向的正确更改。但是代码并没有按照我想要的方式运行。

所以我最后的问题是,是否有人对这种代码有过经验,或者想知道如何以一种美好的方式实现理想的结果?

4 个答案:

答案 0 :(得分:4)

我认为地图解决方案没有错。如果你想要更合理的东西:

public enum Orientation {
    NORTH, EAST, SOUTH, WEST;

    private static Orientation[] vals = values();

    Orientation turnTo(Turn t) {
        return vals[(4 + this.ordinal() + (t == Turn.RIGHT ? 1 : -1)) % 4];
    }
}

然而,这不太干净和可维护(如果有人改变了枚举的顺序,它会破裂。)

一点点清洁(但不太明确):

public enum Orientation {
    NORTH(0), EAST(1), SOUTH(2), WEST(3);

    private final int p;

    Orientation(int p) {
        this.p = p;
    }

    private static Orientation[] vals = new Orientation[4];
    static {
        for( Orientation o : Orientation.values() )
            vals[o.p] = o;
    }

    Orientation turnTo(Turn t) {
        return vals[(4 + this.p + (t == Turn.RIGHT ? 1 : -1)) % 4];
    }
}

答案 1 :(得分:1)

我认为您对循环列表的想法非常好:

public enum Turn {
    LEFT(-1), RIGHT(1);

    private final int offset;

    private Turn(int offset) {
        this.offset = offset;
    }

    public int offset() {
        return this.offset;
    }
}

public enum Orientation {
    NORTH, EAST, SOUTH, WEST;

    private static final List<Orientation> orientations = 
        Arrays.asList(NORTH, EAST, SOUTH, WEST); // to not depend on ordinal

    public Orientation turn(Turn to) {
        int size = orientations.size();
        int myIndex = orientations.indexOf(this);
        int remainder = (myIndex + to.offset()) % size;
        int index = remainder < 0 ? size + remainder : remainder;
        return orientations.get(index);
    }
}

这似乎是非常可扩展的,即HARD_LEFT将有一个-2偏移量,圆形的方向列表应该从左到右排序。

答案 2 :(得分:1)

不使用序数,并且易于理解:

public enum Orientation { NORTH, WEST, EAST, SOUTH;

    static {
        NORTH.left = WEST;
        NORTH.right = EAST;
        WEST.left = SOUTH;
        WEST.right = NORTH;
        EAST.left = NORTH;
        EAST.right = SOUTH;
        SOUTH.left = EAST;
        SOUTH.right = WEST;
    }

    private Orientation left;
    private Orientation right;

    public Orientation turnTo(Turn t) { return t == Turn.LEFT ? left : right; }
}

答案 3 :(得分:1)

欺骗java接受此方法的另一种方法是使用方法而不是字段:

enum Orientation {

    NORTH {
        Orientation left(){return WEST;}
        Orientation right(){return EAST;}
    },
    EAST {
        Orientation left(){return NORTH;}
        Orientation right(){return SOUTH;}
    },
    SOUTH {
        Orientation left(){return EAST;}
        Orientation right(){return WEST;}
    },
    WEST {
        Orientation left(){return SOUTH;}
        Orientation right(){return NORTH;}
    };

    abstract Orientation left();
    abstract Orientation right();

    public Orientation turn(Turn where){
        return where == Turn.LEFT ? this.left() : this.right();
    }
}

如果您愿意,可以自己保存turn()并撰写Orientation.North.left()之类的内容。给你非常简洁的语法。