2010-01-01 86 views
17

權限位掩碼我想要做這樣的事情:在Java中創建

public enum Permissions 
{ 
    CanBlah1, 
    CanBlah2, 
    CanBlah3 
} 

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

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

你能做到這一點在Java中這樣呢?

回答

37

您可以輕鬆地做到這一點使用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 
    } 

} 
+7

而且,當然,EnumSet在引擎蓋下實現爲位掩碼。 – Ross 2010-01-01 02:43:18

+0

他們是。它使用'long'作爲64個'Enum'字節數組,所謂'JubmoSet'。 – 2010-01-01 02:53:31

0

據我所知位運算符是未定義枚舉類型它可以請求枚舉的序號(),並將其用於位操作。當然,因爲你無法定義的順序是什麼一個枚舉,你必須插入僞造的價值觀得到正確

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

通知書應出臺需要一個假的枚舉的序號,這樣

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

是的,但'EnumSet'具有必要的設置操作,例如通過'addAll()'進行聯合,並通過'retainAll()'進行交集。另請參閱:http://en.wikipedia.org/wiki/Set_(mathematics) – trashgod 2010-01-01 04:35:44

3

雖然我不建議(我來自一個C#背景的)

+0

顯示序數的點,不知道! – Thufir 2013-09-09 06:07:11

7

這是另一個選項,它與序號解決方案類似,只不過您可以使用|和&運營商這樣的:

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

如果您在預Java 7的時代(安卓),你可以試試下面的代碼卡:

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

}