From 32ad8ad824683844c6dc65aca5b96a116038a954 Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Tue, 8 Feb 2022 22:26:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8ValidateException=E6=96=B0=E5=A2=9Emat?= =?UTF-8?q?chThrow=E5=92=8CnonMatchThrow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/exceptions/ValidateException.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/hutool-core/src/main/java/cn/hutool/core/exceptions/ValidateException.java b/hutool-core/src/main/java/cn/hutool/core/exceptions/ValidateException.java index 98423d60f..04f33fc0e 100644 --- a/hutool-core/src/main/java/cn/hutool/core/exceptions/ValidateException.java +++ b/hutool-core/src/main/java/cn/hutool/core/exceptions/ValidateException.java @@ -44,4 +44,29 @@ public class ValidateException extends StatefulException { public ValidateException(int status, String msg, Throwable throwable) { super(status, msg, throwable); } + + /** + * 满足条件就抛出异常 + * + * @param condition 条件 + * @param msg 异常消息 + */ + public static void matchThrow(boolean condition, String msg) { + if (condition) { + throw new ValidateException(msg); + } + } + + /** + * 不满足条件就抛出异常 + * + * @param condition 条件 + * @param msg 异常消息 + */ + public static void nonMatchThrow(boolean condition, String msg) { + if (false == condition) { + throw new ValidateException(msg); + } + } + }