I want to do something like this:
public enum Permissions
{
CanBlah1,
CanBlah2,
CanBlah3
}
byte[] userPerm = Permissions.CanBlah1 | Permissions.CanBlah2;
// check permssions
//
if(userPerm && Permissions.CanBlah1 == Permissions.CanBlah1)
{
// do something
}
Can you do this in Java like that? (I'm coming from a c# background)
You can easily do it using EnumSet
import java.util.EnumSet;
import static java.util.EnumSet.of;
import static java.util.EnumSet.range;
import static so.User.Permissions.CanBlah1;
import static so.User.Permissions.CanBlah2;
import static so.User.Permissions.CanBlah3;
public class User {
public enum Permissions {
CanBlah1,
CanBlah2,
CanBlah3
}
public static void main(String[] args) throws Exception {
EnumSet<Permissions> userPerms = of(CanBlah1, CanBlah2);
System.out.println(userPerms.contains(CanBlah1)); //true
System.out.println(userPerms.contains(CanBlah2)); //true
System.out.println(userPerms.contains(CanBlah3)); //false
System.out.println(userPerms.containsAll(of(CanBlah1, CanBlah3))); //false
System.out.println(userPerms.containsAll(range(CanBlah1, CanBlah2))); //true
System.out.println(userPerms.containsAll(range(CanBlah1, CanBlah3))); //false
}
}
This is another option, which is similar to the ordinal solution, except that you can use the | and & operators with this:
public enum Permissions {
CanBlah1(1),
CanBlah2(2),
CanBlah3(4);
public int value;
Permissions(int value) {
this.value = value;
}
public int value() {
return value;
}
}
public static void main(String[] args) {
int userPerm = Permissions.CanBlah1.value() | Permissions.CanBlah2.value();
// check permssions
//
if((userPerm & Permissions.CanBlah1.value()) == Permissions.CanBlah1.value())
{
// do something
}
}
or:
public enum Permissions {
CanBlah1,
CanBlah2,
CanBlah3;
public int value() {
return 1<<ordinal();
}
}
public static void main(String[] args) {
int userPerm = Permissions.CanBlah1.value() | Permissions.CanBlah2.value();
// check permssions
//
if((userPerm & Permissions.CanBlah1.value()) == Permissions.CanBlah1.value())
{
// do something
}
}
While i wouldn't recommend it, you can ask for the ordinal() of an enum and use that for bit operations. Of course since you can't define what the ordinal is for an enum, you have to insert bogus values to get the ordinals right
enum Example {
Bogus, --> 0
This, --> 1
That, --> 2
ThisOrThat --> 3
};
Notice a Bogus enum needed to be introduced so that
ThisOrThat.ordinal() == This.ordinal() | That.ordinal()
If you stuck in the pre Java 7 Era (Android) you can try the following code:
public enum STUFF_TO_BIT_BASK {
THIS,THAT,OTHER;
public static int getBitMask(STUFF_TO_BIT_BASK... masks) {
int res = 0;
for (STUFF_TO_BIT_BASK cap : masks) {
res |= (int) Math.pow(2, cap.ordinal());
}
return res;
}
public boolean is(int maskToCheck){
return maskToCheck | (int) Math.pow(2, this.ordinal());
}
}
As far as I know bitwise operator is undefined for enum types
来源:https://stackoverflow.com/questions/1987738/create-a-permission-bit-mask-in-java