mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-05 13:17:58 +08:00
add BitStatusUtil
This commit is contained in:
parent
fd66c17884
commit
0134a66a6b
@ -17,6 +17,7 @@
|
||||
* 【json 】 JSONObject构造增加SortedMap判断(pr#333@Gitee)
|
||||
* 【core 】 Tuple增加部分方法(pr#333@Gitee)
|
||||
* 【log 】 增加LogTube支持
|
||||
* 【core 】 增加BitStatusUtil(pr#1600@Github)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复XmlUtil中omitXmlDeclaration参数无效问题(issue#1581@Github)
|
||||
|
@ -0,0 +1,79 @@
|
||||
package cn.hutool.core.math;
|
||||
|
||||
/**
|
||||
* 通过位运算表示状态的工具类<br>
|
||||
* 参数必须是 `偶数` 且 `大于等于0`!
|
||||
*
|
||||
* @author senssic
|
||||
* @since 5.6.6
|
||||
*/
|
||||
public class BitStatusUtil {
|
||||
|
||||
/**
|
||||
* 增加状态
|
||||
*
|
||||
* @param states 原状态
|
||||
* @param stat 要添加的状态
|
||||
* @return 新的状态值
|
||||
*/
|
||||
public static int add(int states, int stat) {
|
||||
check(states, stat);
|
||||
return states | stat;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否含有状态
|
||||
*
|
||||
* @param states 原状态
|
||||
* @param stat 要判断的状态
|
||||
* @return true:有
|
||||
*/
|
||||
public static boolean has(int states, int stat) {
|
||||
check(states, stat);
|
||||
return (states & stat) == stat;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除一个状态
|
||||
*
|
||||
* @param states 原状态
|
||||
* @param stat 要删除的状态
|
||||
* @return 新的状态值
|
||||
*/
|
||||
public static int remove(int states, int stat) {
|
||||
check(states, stat);
|
||||
if (has(states, stat)) {
|
||||
return states ^ stat;
|
||||
}
|
||||
return states;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空状态就是0
|
||||
*
|
||||
* @return 0
|
||||
*/
|
||||
public static int clear() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查
|
||||
* <ul>
|
||||
* <li>必须大于0</li>
|
||||
* <li>必须为偶数</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param args 被检查的状态
|
||||
*/
|
||||
private static void check(int... args) {
|
||||
for (int arg : args) {
|
||||
if (arg < 0) {
|
||||
throw new IllegalArgumentException(arg + " 必须大于等于0");
|
||||
}
|
||||
if ((arg & 1) == 1) {
|
||||
throw new IllegalArgumentException(arg + " 不是偶数");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,77 +0,0 @@
|
||||
package cn.hutool.core.math;
|
||||
|
||||
/**
|
||||
* 通过位运算表示状态的工具类<br>
|
||||
* 参数必须是 `偶数` 且 `大于等于0`!
|
||||
*
|
||||
* @author senssic
|
||||
* @date 2021-5-26 15:23
|
||||
*/
|
||||
public class BitStatusUtils {
|
||||
|
||||
/**
|
||||
* 增加状态
|
||||
*
|
||||
* @param states 原状态
|
||||
* @param stat 要添加的状态
|
||||
* @return 新的状态值
|
||||
*/
|
||||
public static int add(int states, int stat) {
|
||||
|
||||
check(states, stat);
|
||||
|
||||
return states | stat;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否含有状态
|
||||
*
|
||||
* @param states 原状态
|
||||
* @param stat 要判断的状态
|
||||
* @return true:有
|
||||
*/
|
||||
public static boolean has(int states, int stat) {
|
||||
|
||||
check(states, stat);
|
||||
|
||||
return (states & stat) == stat;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除一个状态
|
||||
*
|
||||
* @param states 原状态
|
||||
* @param stat 要删除的状态
|
||||
* @return 新的状态值
|
||||
*/
|
||||
public static int remove(int states, int stat) {
|
||||
check(states, stat);
|
||||
if (has(states, stat)) {
|
||||
return states ^ stat;
|
||||
}
|
||||
return states;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空状态就是0
|
||||
* @return
|
||||
*/
|
||||
public static int clear() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查
|
||||
* @param args
|
||||
*/
|
||||
private static void check(int... args) {
|
||||
for (int arg : args) {
|
||||
if (arg < 0) {
|
||||
throw new IllegalArgumentException(arg + " 必须大于等于0");
|
||||
}
|
||||
if ((arg & 1) == 1) {
|
||||
throw new IllegalArgumentException(arg + " 不是偶数");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user