在java中创建权限位掩码

发布于 2024-08-16 04:19:36 字数 328 浏览 8 评论 0原文

我想做这样的事情:

public enum Permissions
{
    CanBlah1,
    CanBlah2,
    CanBlah3
}

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

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

你能用 Java 那样做吗? (我来自ac#背景)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甚是思念 2024-08-23 04:19:36

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

}

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
    }

}
黑白记忆 2024-08-23 04:19:36

这是另一个选项,与序数解类似,只是您可以使用 |和&运算符:

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

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
        }
     }
许久 2024-08-23 04:19:36

虽然我不推荐它,但您可以请求枚举的 ordinal() 并将其用于位操作。当然,由于您无法定义枚举的序数,因此您必须插入虚假值才能获得正确的序数。

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

请注意,需要引入虚假枚举,以便

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

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()
绅士风度i 2024-08-23 04:19:36

如果您停留在 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());
}

}

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

}

那伤。 2024-08-23 04:19:36

据我所知,枚举类型的按位运算符未定义

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文