Suppose you have an enum Direction
enum Direction{
North,South,East West
}
Could I write a method that uses bitwise or\'s to compare mu
You can't use a bitwise or like that. Try something more like this:
public boolean canGoDirection(Set<Direction> dirs){
return dirs.contains(Direction.North);
}
called like this:
this.canGoDirection(EnumSet.of(Direction.South,Direction.North));
The functionality that you are looking for is implemented by EnumSet.
Your example can be boiled to:
final static EnumSet<Direction> ALLOWED_DIRECTIONS =
EnumSet.of( Direction.North, Direction.South );
public boolean canGoDirection(Direction dir){
return ALLOWED_DIRECTIONS.contains( dir );
}
Not directly, but if you add a primitive (e.g. int
) field to your enum, you can OR that int
value.
However, the resulting value is of int
(which can not be implicitly converted to boolean
, much less back to enum
).
Probably you would be better off using an EnumSet instead. This is much more idiomatic to Java.