位运算&位掩码示例(权限)
程序员文章站
2022-03-24 19:01:34
...
参考资料地址:
1、https://juejin.cn/post/6844903909333401607
2、https://www.jianshu.com/p/78a984173985
3、【精华】《位运算(位掩码BitMask)的简单应用场景浅析》 https://www.jianshu.com/p/78a984173985
位掩码示例:
import org.apache.commons.lang3.StringUtils; import com.google.common.collect.Lists; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; /** * Hello world! * */ public class App { public static void main(String[] args) { System.out.println(String.format("%2s:%s", Perms.READ, toBinaryStringFormat(Perms.READ))); System.out.println(String.format("%2s:%s", Perms.WRITE, toBinaryStringFormat(Perms.WRITE))); System.out.println(String.format("%2s:%s", Perms.CREATE, toBinaryStringFormat(Perms.CREATE))); System.out.println(String.format("%2s:%s", Perms.DELETE, toBinaryStringFormat(Perms.DELETE))); System.out.println(String.format("%2s:%s", Perms.ADMIN, toBinaryStringFormat(Perms.ADMIN))); System.out.println(String.format("%2s:%s", Perms.ALL, toBinaryStringFormat(Perms.ALL))); System.out.println(String.format("%2s:%s", Perms.DENY_ALL, toBinaryStringFormat(Perms.DENY_ALL))); System.out.println(String.format("%s", StringUtils.repeat("=", 100))); Perms p = new Perms(); p.setPermission(Perms.READ); System.out .println(String.format("%2s:%s", p.getPermission(), toBinaryStringFormat(p.getPermission()))); p.enable(Perms.WRITE | Perms.CREATE); System.out .println(String.format("%2s:%s", p.getPermission(), toBinaryStringFormat(p.getPermission()))); p.enable(Perms.ADMIN); System.out .println(String.format("%2s:%s", p.getPermission(), toBinaryStringFormat(p.getPermission()))); } public static String toBinaryStringFormat(int perm) { String string = StringUtils.leftPad(Integer.toBinaryString(perm), Integer.SIZE, '0'); List<List<String>> lists = Lists.partition(Arrays.asList(string.split("")), Byte.SIZE); return lists.stream().map(list -> { return String.join("", list); }).collect(Collectors.joining(" ")); } } class Perms { /** * 00000000 00000000 00000000 00000001 */ public static final int READ = 1 << 0; /** * 00000000 00000000 00000000 00000010 */ public static final int WRITE = 1 << 1; /** * 00000000 00000000 00000000 00000100 */ public static final int CREATE = 1 << 2; /** * 00000000 00000000 00000000 00001000 */ public static final int DELETE = 1 << 3; /** * 00000000 00000000 00000000 00010000 */ public static final int ADMIN = 1 << 4; /** * 00000000 00000000 00000000 00011111 */ public static final int ALL = READ | WRITE | CREATE | DELETE | ADMIN; public static final int DENY_ALL = READ & WRITE & CREATE & DELETE & ADMIN; // 存储目前的权限状态 private int flag; /** * 移除一项或多项权限 */ public void disable(int permission) { flag &= ~permission; } /** * 添加一项或多项权限 */ public void enable(int permission) { flag |= permission; } /** * * 得到当前权限 */ public int getPermission() { return flag; } /** * 是否拥有某项权限 */ public boolean isAllow(int permission) { return (flag & permission) == permission; } /** * 是否禁用了某些权限 */ public boolean isNotAllow(int permission) { return (flag & permission) == 0; } /** * 是否仅仅拥有某些权限 */ public boolean isOnlyAllow(int permission) { return flag == permission; } /** * 重新设置权限 */ public void setPermission(int permission) { flag = permission; } }