在java中创建权限位掩码

时间:2010-01-01 02:31:15

标签: java bitmask

我想做这样的事情:

public enum Permissions
{
    CanBlah1,
    CanBlah2,
    CanBlah3
}

byte[] userPerm = Permissions.CanBlah1 | Permissions.CanBlah2;

// check permssions
//
if(userPerm && Permissions.CanBlah1 == Permissions.CanBlah1)
{
      // do something
}

你能用Java做到这一点吗? (我来自c#背景)

5 个答案:

答案 0 :(得分:38)

您可以使用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
    }

}

答案 1 :(得分:7)

这是另一个选项,类似于序数解决方案,除了你可以使用|和&amp;运营商:

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

或:

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

答案 2 :(得分:3)

虽然我不推荐它,但你可以要求enum的ordinal()并将其用于位操作。当然因为你无法定义枚举的序数,你必须插入伪造的值才能使序数正确

enum Example {
   Bogus,            --> 0
   This,             --> 1
   That,             --> 2
   ThisOrThat        --> 3
};

注意需要引入Bogus枚举以便

ThisOrThat.ordinal() == This.ordinal() | That.ordinal()

答案 3 :(得分:2)

如果您坚持使用Java 7时代(Android),您可以尝试以下代码:

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

}

答案 4 :(得分:0)

据我所知,对于枚举类型

,未定义位运算符