Your enum could like this:
public enum Direction {
UP(0), DOWN(1), LEFT(2), RIGHT(3), NEUTRAL(4);
private Integer index;
Direction(Integer index){
this.index = index;
};
public Integer getIndex(){
return this.index;
}
}
and then your if statements :
Direction desired_direction = Direction.LEFT;
int[] LINK = {2,0,0,0,0};
if (desired_direction == Direction.LEFT || LINK[Direction.LEFT.getIndex()] > 0){
return true;
}
Or you could reference the ordinal() method rather than explicity creating an index Integer. Like so LINK[Direction.LEFT.ordinal()] > 0
However maybe using a hashmap could be appropriate (althoguh the logic is not quite the same as your question :
Map<Direction, Integer> myLink = new HashMap<Direction, Integer>(5);
myLink.put(Direction.LEFT, 2);
myLink.put(Direction.RIGHT, 0);
myLink.put(Direction.UP, 0);
myLink.put(Direction.DOWN, 0);
if(myLink.get(desired_direction) > 0)
{
return true;
}
But my favourite is putting all the logic on the enum itself and then passing the int array as a parameter :
public Boolean getAnalysis(int[] link){
return link[this.getIndex()] > 0 ? Boolean.TRUE:Boolean.FALSE;
}
I also like explicit setting an index property rather than rely on the ordinal, just in case someone alters the enum in the future
and your call would like this
Direction desired_direction = Direction.LEFT;
return desired_direction.getAnalysis(LINK);
()chars in yourifstatements even for C++.