create a permission bit mask in java

拥有回忆 提交于 2019-12-03 18:59:53

问题


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)


回答1:


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
    }

}



回答2:


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
        }
     }



回答3:


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()



回答4:


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());
}

}




回答5:


As far as I know bitwise operator is undefined for enum types



来源:https://stackoverflow.com/questions/1987738/create-a-permission-bit-mask-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!