zoukankan      html  css  js  c++  java
  • 可用于权限计算的帮助类

    http://blog.csdn.net/fulinkster/article/details/6597938

     1 package com.fh.util;
     2 
     3 import java.math.BigInteger;
     4 
     5 /**
     6  * @author Administrator
     7  * 权限计算帮助类
     8  */
     9 public class RightsHelper {
    10     /**
    11      * 利用BigInteger对权限进行2的权的和计算
    12      * @param rights int型权限编码数组
    13      * @return 2的权的和
    14      */
    15     public static BigInteger sumRights(int[] rights){
    16         BigInteger num = new BigInteger("0");
    17         for(int i=0; i<rights.length; i++){
    18             num = num.setBit(rights[i]);
    19         }
    20         return num;
    21     }
    22     /**
    23      * 利用BigInteger对权限进行2的权的和计算
    24      * @param rights String型权限编码数组
    25      * @return 2的权的和
    26      */
    27     public static BigInteger sumRights(String[] rights){
    28         BigInteger num = new BigInteger("0");
    29         for(int i=0; i<rights.length; i++){
    30             num = num.setBit(Integer.parseInt(rights[i]));
    31         }
    32         return num;
    33     }
    34     
    35     /**
    36      * 测试是否具有指定编码的权限
    37      * @param sum
    38      * @param targetRights
    39      * @return
    40      */
    41     public static boolean testRights(BigInteger sum,int targetRights){
    42         return sum.testBit(targetRights);
    43     }
    44     
    45     /**
    46      * 测试是否具有指定编码的权限
    47      * @param sum
    48      * @param targetRights
    49      * @return
    50      */
    51     public static boolean testRights(String sum,int targetRights){
    52         if(Tools.isEmpty(sum))
    53             return false;
    54         return testRights(new BigInteger(sum),targetRights);
    55     }
    56     
    57     /**
    58      * 测试是否具有指定编码的权限
    59      * @param sum
    60      * @param targetRights
    61      * @return
    62      */
    63     public static boolean testRights(String sum,String targetRights){
    64         if(Tools.isEmpty(sum))
    65             return false;
    66         return testRights(new BigInteger(sum),targetRights);
    67     }
    68     
    69     /**
    70      * 测试是否具有指定编码的权限
    71      * @param sum
    72      * @param targetRights
    73      * @return
    74      */
    75     public static boolean testRights(BigInteger sum,String targetRights){
    76         return testRights(sum,Integer.parseInt(targetRights));
    77     }
    78 }

    sumRights方法调用了BigInteger的setBit方法,实际上就是进行2的权的和运算。

    比如:RightsHelper.sumRights(new int[]{1,2,3,4});实际上就是:1^2+2^2+3^2+4^2

  • 相关阅读:
    myeclipse学习总结一(在MyEclipse中设置生成jsp页面时默认编码为utf-8编码)
    使用Android Studio调试UiAutomator过程中遇到的问题
    手游性能之渲染分析2
    手游性能之渲染分析1
    手脱ASProtect v1.23 RC1(有Stolen Code)之以壳解壳
    手脱ASProtect v1.23 RC1(有Stolen Code)
    Java 中extends与implements使用方法
    Java在处理大数据的时候一些小技巧
    Oracle 分页原理
    powerdesigner连接数据库 导出数据
  • 原文地址:https://www.cnblogs.com/a757956132/p/5467823.html
Copyright © 2011-2022 走看看