From bf082b56d9c15463aaf3ecfa393f214dffa1519d Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sat, 29 Apr 2023 18:38:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B7=A5=E5=85=B7=E7=B1=BB?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/commons/util/Assert.java | 53 +++++++------------ .../plusone/commons/util/MoreArrays.java | 42 +++++++++++++++ .../plusone/commons/util/MoreCollections.java | 19 +++++++ 3 files changed, 80 insertions(+), 34 deletions(-) create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/util/MoreArrays.java create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java b/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java index b1abfac..a009e66 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java @@ -113,71 +113,56 @@ public class Assert { } // isEmpty - Collection - private static boolean isEmpty(@Nullable Collection collection) { - return collection == null || collection.isEmpty(); + public static void isEmpty(@Nullable Collection collection, Supplier e) throws E { + Assert.isTrue(MoreCollections.isEmpty(collection), e); } - public static void isEmpty(@Nullable Collection collection, Supplier e) throws E { - Assert.isTrue(isEmpty(collection), e); + public static void isEmpty(@Nullable Collection collection, String errorMessage) { + Assert.isTrue(MoreCollections.isEmpty(collection), errorMessage); } - public static void isEmpty(@Nullable Collection collection, String errorMessage) { - Assert.isTrue(isEmpty(collection), errorMessage); - } - - public static void isEmpty(@Nullable Collection collection, String errorMessageTemplate, Object... args) { - Assert.isTrue(isEmpty(collection), errorMessageTemplate, args); + public static void isEmpty(@Nullable Collection collection, String errorMessageTemplate, Object... args) { + Assert.isTrue(MoreCollections.isEmpty(collection), errorMessageTemplate, args); } // isNotEmpty - Collection - private static boolean isNotEmpty(@Nullable Collection collection) { - return collection != null && !collection.isEmpty(); + public static void isNotEmpty(@Nullable Collection collection, Supplier e) throws E { + Assert.isTrue(MoreCollections.isNotEmpty(collection), e); } - public static void isNotEmpty(@Nullable Collection collection, Supplier e) throws E { - Assert.isTrue(isNotEmpty(collection), e); + public static void isNotEmpty(@Nullable Collection collection, String errorMessage) { + Assert.isTrue(MoreCollections.isNotEmpty(collection), errorMessage); } - public static void isNotEmpty(@Nullable Collection collection, String errorMessage) { - Assert.isTrue(isNotEmpty(collection), errorMessage); - } - - public static void isNotEmpty(@Nullable Collection collection, String errorMessageTemplate, Object... args) { - Assert.isTrue(isNotEmpty(collection), errorMessageTemplate, args); + public static void isNotEmpty(@Nullable Collection collection, String errorMessageTemplate, Object... args) { + Assert.isTrue(MoreCollections.isNotEmpty(collection), errorMessageTemplate, args); } // isEmpty - Array - private static boolean isEmpty(@Nullable T[] arr) { - return arr == null || arr.length == 0; - } - public static void isEmpty(@Nullable T[] arr, Supplier e) throws E { - Assert.isTrue(isEmpty(arr), e); + Assert.isTrue(MoreArrays.isEmpty(arr), e); } public static void isEmpty(@Nullable T[] arr, String errorMessage) { - Assert.isTrue(isEmpty(arr), errorMessage); + Assert.isTrue(MoreArrays.isEmpty(arr), errorMessage); } public static void isEmpty(@Nullable T[] arr, String errorMessageTemplate, Object... args) { - Assert.isTrue(isEmpty(arr), errorMessageTemplate, args); + Assert.isTrue(MoreArrays.isEmpty(arr), errorMessageTemplate, args); } // isNotEmpty - Array - private static boolean isNotEmpty(@Nullable T[] arr) { - return arr != null && arr.length > 0; - } - + // TODO int[] long[] double[] public static void isNotEmpty(@Nullable T[] arr, Supplier e) throws E { - Assert.isTrue(isNotEmpty(arr), e); + Assert.isTrue(MoreArrays.isNotEmpty(arr), e); } public static void isNotEmpty(@Nullable T[] arr, String errorMessage) { - Assert.isTrue(isNotEmpty(arr), errorMessage); + Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessage); } public static void isNotEmpty(@Nullable T[] arr, String errorMessageTemplate, Object... args) { - Assert.isTrue(isNotEmpty(arr), errorMessageTemplate, args); + Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessageTemplate, args); } // isEmpty - String diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/MoreArrays.java b/src/main/java/xyz/zhouxy/plusone/commons/util/MoreArrays.java new file mode 100644 index 0000000..810b0cd --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/MoreArrays.java @@ -0,0 +1,42 @@ +package xyz.zhouxy.plusone.commons.util; + +import javax.annotation.Nullable; + +public class MoreArrays { + + public static boolean isEmpty(@Nullable int[] arr) { + return arr == null || arr.length == 0; + } + + public static boolean isNotEmpty(@Nullable int[] arr) { + return arr != null && arr.length > 0; + } + + public static boolean isEmpty(@Nullable long[] arr) { + return arr == null || arr.length == 0; + } + + public static boolean isNotEmpty(@Nullable long[] arr) { + return arr != null && arr.length > 0; + } + + public static boolean isEmpty(@Nullable double[] arr) { + return arr == null || arr.length == 0; + } + + public static boolean isNotEmpty(@Nullable double[] arr) { + return arr != null && arr.length > 0; + } + + public static boolean isEmpty(@Nullable T[] arr) { + return arr == null || arr.length == 0; + } + + public static boolean isNotEmpty(@Nullable T[] arr) { + return arr != null && arr.length > 0; + } + + private MoreArrays() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java b/src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java new file mode 100644 index 0000000..f63f987 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java @@ -0,0 +1,19 @@ +package xyz.zhouxy.plusone.commons.util; + +import java.util.Collection; + +import javax.annotation.Nullable; + +public class MoreCollections { + public static boolean isEmpty(@Nullable Collection collection) { + return collection == null || collection.isEmpty(); + } + + public static boolean isNotEmpty(@Nullable Collection collection) { + return collection != null && !collection.isEmpty(); + } + + private MoreCollections() { + throw new IllegalStateException("Utility class"); + } +}