日期:2014-05-20 浏览次数:20800 次
package util; public class PermissionController { public static final int INSERT = 2; public static final int UPDATE = 4; public static final int DELETE = 8; private int permission = 0; public boolean canInsert() { return hasPermission(INSERT); } public boolean canUpdate() { return hasPermission(UPDATE); } public boolean canDelete() { return hasPermission(DELETE); } public void addInsertPermission() { addPermission(INSERT); } public void addUpdatePermission() { addPermission(UPDATE); } public void addDeletePermission() { addPermission(DELETE); } public void removeInsertPermission() { removePermission(INSERT); } public void removeUpdatePermission() { removePermission(UPDATE); } public void removeDeletePermission() { removePermission(DELETE); } public int getPermissionValue() { return permission; } private void addPermission(int p) { permission |= p; } private void removePermission(int p) { permission = permission & (~p); } private boolean hasPermission(int p) { return (permission & p) == p ? true : false; } public static void main(String[] args) { PermissionController p = new PermissionController(); System.out.println("添加权限......"); // 添加权限 p.addInsertPermission(); System.out.printf("Can insert: %s\n", p.canInsert()); // 删除权限 p.addDeletePermission(); System.out.printf("Can delete: %s\n", p.canDelete()); System.out.printf("Can update: %s\n", p.canUpdate()); System.out.println(Integer.toBinaryString(p.getPermissionValue())); System.out.println("删除权限......"); // 去掉删除权限 p.removeDeletePermission(); System.out.printf("Can delete: %s\n", p.canDelete()); System.out.println(Integer.toBinaryString(p.getPermissionValue())); } }
------解决方案--------------------
使用enum的特性,能使得代码更加的简练
package util; public class PermissionController { private int permission = 0; static public enum Permission { INSERT(1), UPDATE(2), DELETE(4); // 每个权限的值是2的n次幂 private final int value; private Permission(int value) { this.value = value; } public int getValue() { return value; } } public void addPermission(Permission p) { for (Permission t : Permission.values()) { if (t == p) { permission |= p.getValue(); } } } public void removePermission(Permission p) { for (Permission t : Permission.values()) { if (t == p) { permission = permission & (~p.getValue()); } } } public boolean hasPermission(Permission p) { return (permission & p.getValue()) == p.getValue() ? true : false; } public int getPermissionValue() { return permission; } public static void main(String[] args) { PermissionController p = new PermissionController(); // 增加添加权限 System.out.println("增加添加权限"); p.addPermission(Permission.INSERT); System.out.printf("Can insert: %s\n", p.hasPermission(Permission.INSERT)); // 增加删除权限 System.out.println("增加删除权限"); p.addPermission(Permission.DELETE); System.out.printf("Can delete: %s\n", p.hasPermission(Permission.DELETE)); System.out.printf("Can update: %s\n", p.hasPermission(Permission.UPDATE)); System.out.println(Integer.toBinaryString(p.getPermissionValue())); System.out.println("去掉删除权限......"); // 去掉删除权限 p.removePermission(Permission.DELETE); System.out.printf("Can delete: %s\n", p.hasPermission(Permission.DELETE)); System.out.printf("Can insert: %s\n", p.hasPermission(Permission.INSERT)); System.out.println(Integer.toBinaryString(p.getPermissionValue())); } }