diff --git a/CHANGELOG.md b/CHANGELOG.md index d0bb010ad..a1975027d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/hutool-core/src/main/java/cn/hutool/core/math/BitStatusUtil.java b/hutool-core/src/main/java/cn/hutool/core/math/BitStatusUtil.java new file mode 100644 index 000000000..559929125 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/math/BitStatusUtil.java @@ -0,0 +1,79 @@ +package cn.hutool.core.math; + +/** + * 通过位运算表示状态的工具类
+ * 参数必须是 `偶数` 且 `大于等于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; + } + + /** + * 检查 + * + * + * @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 + " 不是偶数"); + } + } + } +} diff --git a/hutool-core/src/main/java/cn/hutool/core/math/BitStatusUtils.java b/hutool-core/src/main/java/cn/hutool/core/math/BitStatusUtils.java deleted file mode 100644 index ca4764c4e..000000000 --- a/hutool-core/src/main/java/cn/hutool/core/math/BitStatusUtils.java +++ /dev/null @@ -1,77 +0,0 @@ -package cn.hutool.core.math; - -/** - * 通过位运算表示状态的工具类
- * 参数必须是 `偶数` 且 `大于等于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 + " 不是偶数"); - } - } - } -}