From f8244eaa4bc6083654c3be0c7536617629f8359f Mon Sep 17 00:00:00 2001 From: senssic Date: Wed, 26 May 2021 13:56:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=8D=E8=BF=90=E7=AE=97=E6=A0=87=E8=AF=86?= =?UTF-8?q?=E7=8A=B6=E6=80=81,=E9=80=82=E5=90=88=E4=BA=8Emysql=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=AD=97=E6=AE=B5=E8=A1=A8=E7=A4=BA=E5=A4=9A=E7=A7=8D?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/math/BitStatusUtils.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 hutool-core/src/main/java/cn/hutool/core/math/BitStatusUtils.java 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 new file mode 100644 index 000000000..ca4764c4e --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/math/BitStatusUtils.java @@ -0,0 +1,77 @@ +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 + " 不是偶数"); + } + } + } +}