From 0b9635880e9fe1f2e3c14f8561d9cad4a94270b6 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Fri, 30 Aug 2024 14:56:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20PreconditionsExt=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/util/PreconditionsExt.java | 4 ++- .../commons/util/PreconditionsExtTests.java | 28 ++++++++++--------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/PreconditionsExt.java b/src/main/java/xyz/zhouxy/plusone/commons/util/PreconditionsExt.java index c6ffbec..69e7e9e 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/PreconditionsExt.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/PreconditionsExt.java @@ -19,6 +19,8 @@ package xyz.zhouxy.plusone.commons.util; import java.util.Arrays; import java.util.function.Supplier; +import javax.annotation.Nonnull; + import com.google.common.base.Preconditions; /** @@ -30,7 +32,7 @@ import com.google.common.base.Preconditions; */ public class PreconditionsExt { - public static void check(boolean condition, Supplier e) throws E { + public static void check(boolean condition, @Nonnull Supplier e) throws E { if (!condition) { throw e.get(); } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/PreconditionsExtTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/PreconditionsExtTests.java index 923499f..1961427 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/PreconditionsExtTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/PreconditionsExtTests.java @@ -1,9 +1,9 @@ package xyz.zhouxy.plusone.commons.util; import org.junit.jupiter.api.Test; -import xyz.zhouxy.plusone.commons.exception.BaseException; import java.util.Arrays; +import java.util.List; import static org.junit.jupiter.api.Assertions.*; @@ -22,33 +22,35 @@ class PreconditionsExtTests { Object[] array = null; PreconditionsExt.checkAllNotNull(array); }); + Object obj = new Object(); + assertNotNull(obj); assertThrows(NullPointerException.class, - () -> PreconditionsExt.checkAllNotNull(new Object[]{new Object(), null})); + () -> PreconditionsExt.checkAllNotNull(new Object[]{obj, null})); assertThrows(NullPointerException.class, - () -> PreconditionsExt.checkAllNotNull(new Object(), null)); + () -> PreconditionsExt.checkAllNotNull(obj, null)); + List list = Arrays.asList(obj, null); + assertNotNull(list); assertThrows(NullPointerException.class, - () -> PreconditionsExt.checkAllNotNull(Arrays.asList(new Object(), null))); + () -> PreconditionsExt.checkAllNotNull(list)); - PreconditionsExt.checkAllNotNull(new Object[]{new Object(), "Test"}); - PreconditionsExt.checkAllNotNull(new Object(), "Test"); - PreconditionsExt.checkAllNotNull(Arrays.asList(new Object(), "Test")); + PreconditionsExt.checkAllNotNull(new Object[]{obj, "Test"}); + PreconditionsExt.checkAllNotNull(obj, "Test"); + PreconditionsExt.checkAllNotNull(Arrays.asList(obj, "Test")); } } -class TestException extends BaseException { +class TestException extends Exception { private static final long serialVersionUID = -8808661764734834820L; - private static final String ERR_CODE = "TEST"; - protected TestException(String msg) { - super(ERR_CODE, msg); + super(msg); } protected TestException(Throwable cause) { - super(ERR_CODE, cause); + super(cause); } protected TestException(String msg, Throwable cause) { - super(ERR_CODE, msg, cause); + super(msg, cause); } }