java BigInteger 对权限进行2的权的和计算

起航

packagecom.fh.util;

importjava.math.BigInteger;

/**
*说明:权限计算帮助类
*作者:FHAdmin
*官网:fhadmin.cn
*/
publicclassRightsHelper{
/**
*利用BigInteger对权限进行2的权的和计算
*@paramrightsint型权限编码数组
*@return2的权的和
*/
publicstaticBigIntegersumRights(int[]rights){
BigIntegernum=newBigInteger("0");
for(inti=0;i<rights.length;i++){
num=num.setBit(rights[i]);
}
returnnum;
}
/**
*利用BigInteger对权限进行2的权的和计算
*@paramrightsString型权限编码数组
*@return2的权的和
*/
publicstaticBigIntegersumRights(String[]rights){
BigIntegernum=newBigInteger("0");
for(inti=0;i<rights.length;i++){
num=num.setBit(Integer.parseInt(rights[i]));
}
returnnum;
}

/**
*测试是否具有指定编码的权限
*@paramsum
*@paramtargetRights
*@return
*/
publicstaticbooleantestRights(BigIntegersum,inttargetRights){
returnsum.testBit(targetRights);
}

/**
*测试是否具有指定编码的权限
*@paramsum
*@paramtargetRights
*@return
*/
publicstaticbooleantestRights(Stringsum,inttargetRights){
if(Tools.isEmpty(sum))
returnfalse;
returntestRights(newBigInteger(sum),targetRights);
}

/**
*测试是否具有指定编码的权限
*@paramsum
*@paramtargetRights
*@return
*/
publicstaticbooleantestRights(Stringsum,StringtargetRights){
if(Tools.isEmpty(sum))
returnfalse;
returntestRights(newBigInteger(sum),targetRights);
}

/**
*测试是否具有指定编码的权限
*@paramsum
*@paramtargetRights
*@return
*/
publicstaticbooleantestRights(BigIntegersum,StringtargetRights){
returntestRights(sum,Integer.parseInt(targetRights));
}
}

精彩阅读

粤ICP备16095388号