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