create a permission bit mask in java

那年仲夏 提交于 2019-11-30 03:22:39

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

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