From f54b2d3cc6b470acd1241fa6b704e6042d4037bc Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 8 May 2023 21:56:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BE=9D=E8=B5=96=20commons-lang3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 17 +-- .../domain/ValidatableStringRecord.java | 2 +- .../zhouxy/plusone/commons/util/Assert.java | 125 ++++++++++-------- .../plusone/commons/util/Enumeration.java | 18 +-- .../plusone/commons/util/MoreArrays.java | 104 --------------- .../plusone/commons/util/MoreStrings.java | 23 ---- .../util/PagingAndSortingQueryParams.java | 18 +-- .../plusone/commons/EnumerationTests.java | 4 +- .../commons/function/FunctionTests.java | 2 +- 9 files changed, 100 insertions(+), 213 deletions(-) delete mode 100644 src/main/java/xyz/zhouxy/plusone/commons/util/MoreArrays.java delete mode 100644 src/main/java/xyz/zhouxy/plusone/commons/util/MoreStrings.java diff --git a/pom.xml b/pom.xml index bb2b9c9..dc4a1ae 100644 --- a/pom.xml +++ b/pom.xml @@ -13,15 +13,11 @@ 1.8 1.8 2.13.4 - 31.1-jre + 3.12.0 + 3.0.2 - - com.google.guava - guava - ${guava.version} - com.fasterxml.jackson.core jackson-annotations @@ -31,8 +27,13 @@ org.apache.commons commons-lang3 - 3.12.0 - test + ${commons-lang3.version} + + + + com.google.code.findbugs + jsr305 + ${google-jsr305.version} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java b/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java index 76cb3cd..6b66a6f 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java @@ -33,7 +33,7 @@ public abstract class ValidatableStringRecord { protected ValidatableStringRecord(String value, Pattern pattern) { Assert.notNull(pattern, "The pattern must not be null."); - Assert.hasText(value, "The value must be has text."); + Assert.isNotBlank(value, "The value must be has text."); Assert.isTrue(pattern.matcher(value).matches()); this.value = value; } 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 782b192..194d263 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java @@ -6,7 +6,8 @@ import java.util.function.Supplier; import javax.annotation.Nullable; -import com.google.common.base.Strings; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.StringUtils; public class Assert { @@ -152,149 +153,161 @@ public class Assert { // isEmpty - Array public static void isEmpty(@Nullable T[] arr, Supplier e) throws E { - Assert.isTrue(MoreArrays.isEmpty(arr), e); + Assert.isTrue(ArrayUtils.isEmpty(arr), e); } public static void isEmpty(@Nullable T[] arr, String errorMessage) { - Assert.isTrue(MoreArrays.isEmpty(arr), errorMessage); + Assert.isTrue(ArrayUtils.isEmpty(arr), errorMessage); } public static void isEmpty(@Nullable T[] arr, String errorMessageTemplate, Object... args) { - Assert.isTrue(MoreArrays.isEmpty(arr), errorMessageTemplate, args); + Assert.isTrue(ArrayUtils.isEmpty(arr), errorMessageTemplate, args); } // isEmpty - int[] public static void isEmpty(@Nullable int[] arr, Supplier e) throws E { - Assert.isTrue(MoreArrays.isEmpty(arr), e); + Assert.isTrue(ArrayUtils.isEmpty(arr), e); } public static void isEmpty(@Nullable int[] arr, String errorMessage) { - Assert.isTrue(MoreArrays.isEmpty(arr), errorMessage); + Assert.isTrue(ArrayUtils.isEmpty(arr), errorMessage); } public static void isEmpty(@Nullable int[] arr, String errorMessageTemplate, Object... args) { - Assert.isTrue(MoreArrays.isEmpty(arr), errorMessageTemplate, args); + Assert.isTrue(ArrayUtils.isEmpty(arr), errorMessageTemplate, args); } // isEmpty - long[] public static void isEmpty(@Nullable long[] arr, Supplier e) throws E { - Assert.isTrue(MoreArrays.isEmpty(arr), e); + Assert.isTrue(ArrayUtils.isEmpty(arr), e); } public static void isEmpty(@Nullable long[] arr, String errorMessage) { - Assert.isTrue(MoreArrays.isEmpty(arr), errorMessage); + Assert.isTrue(ArrayUtils.isEmpty(arr), errorMessage); } public static void isEmpty(@Nullable long[] arr, String errorMessageTemplate, Object... args) { - Assert.isTrue(MoreArrays.isEmpty(arr), errorMessageTemplate, args); + Assert.isTrue(ArrayUtils.isEmpty(arr), errorMessageTemplate, args); } // isEmpty - double[] public static void isEmpty(@Nullable double[] arr, Supplier e) throws E { - Assert.isTrue(MoreArrays.isEmpty(arr), e); + Assert.isTrue(ArrayUtils.isEmpty(arr), e); } public static void isEmpty(@Nullable double[] arr, String errorMessage) { - Assert.isTrue(MoreArrays.isEmpty(arr), errorMessage); + Assert.isTrue(ArrayUtils.isEmpty(arr), errorMessage); } public static void isEmpty(@Nullable double[] arr, String errorMessageTemplate, Object... args) { - Assert.isTrue(MoreArrays.isEmpty(arr), errorMessageTemplate, args); + Assert.isTrue(ArrayUtils.isEmpty(arr), errorMessageTemplate, args); } // isNotEmpty - Array public static void isNotEmpty(@Nullable T[] arr, Supplier e) throws E { - Assert.isTrue(MoreArrays.isNotEmpty(arr), e); + Assert.isTrue(ArrayUtils.isNotEmpty(arr), e); } public static void isNotEmpty(@Nullable T[] arr, String errorMessage) { - Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessage); + Assert.isTrue(ArrayUtils.isNotEmpty(arr), errorMessage); } public static void isNotEmpty(@Nullable T[] arr, String errorMessageTemplate, Object... args) { - Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessageTemplate, args); + Assert.isTrue(ArrayUtils.isNotEmpty(arr), errorMessageTemplate, args); } // isNotEmpty - int[] public static void isNotEmpty(@Nullable int[] arr, Supplier e) throws E { - Assert.isTrue(MoreArrays.isNotEmpty(arr), e); + Assert.isTrue(ArrayUtils.isNotEmpty(arr), e); } public static void isNotEmpty(@Nullable int[] arr, String errorMessage) { - Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessage); + Assert.isTrue(ArrayUtils.isNotEmpty(arr), errorMessage); } public static void isNotEmpty(@Nullable int[] arr, String errorMessageTemplate, Object... args) { - Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessageTemplate, args); + Assert.isTrue(ArrayUtils.isNotEmpty(arr), errorMessageTemplate, args); } // isNotEmpty - long[] public static void isNotEmpty(@Nullable long[] arr, Supplier e) throws E { - Assert.isTrue(MoreArrays.isNotEmpty(arr), e); + Assert.isTrue(ArrayUtils.isNotEmpty(arr), e); } public static void isNotEmpty(@Nullable long[] arr, String errorMessage) { - Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessage); + Assert.isTrue(ArrayUtils.isNotEmpty(arr), errorMessage); } public static void isNotEmpty(@Nullable long[] arr, String errorMessageTemplate, Object... args) { - Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessageTemplate, args); + Assert.isTrue(ArrayUtils.isNotEmpty(arr), errorMessageTemplate, args); } // isNotEmpty - double[] public static void isNotEmpty(@Nullable double[] arr, Supplier e) throws E { - Assert.isTrue(MoreArrays.isNotEmpty(arr), e); + Assert.isTrue(ArrayUtils.isNotEmpty(arr), e); } public static void isNotEmpty(@Nullable double[] arr, String errorMessage) { - Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessage); + Assert.isTrue(ArrayUtils.isNotEmpty(arr), errorMessage); } public static void isNotEmpty(@Nullable double[] arr, String errorMessageTemplate, Object... args) { - Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessageTemplate, args); + Assert.isTrue(ArrayUtils.isNotEmpty(arr), errorMessageTemplate, args); } // isEmpty - String public static void isEmpty(@Nullable String str, Supplier e) throws E { - Assert.isTrue(Strings.isNullOrEmpty(str), e); - } - - public static void isEmpty(@Nullable String str, String errorMessage) { - Assert.isTrue(Strings.isNullOrEmpty(str), errorMessage); - } - - public static void isEmpty(@Nullable String str, String errorMessageTemplate, Object... args) { - Assert.isTrue(Strings.isNullOrEmpty(str), errorMessageTemplate, args); - } - - // isNotEmpty - String - public static void isNotEmpty(@Nullable String str, Supplier e) throws E { - Assert.isTrue(!Strings.isNullOrEmpty(str), e); - } - - public static void isNotEmpty(@Nullable String str, String errorMessage) { - Assert.isTrue(!Strings.isNullOrEmpty(str), errorMessage); - } - - public static void isNotEmpty(@Nullable String str, String errorMessageTemplate, Object... args) { - Assert.isTrue(!Strings.isNullOrEmpty(str), errorMessageTemplate, args); - } - - // hasText - String - public static void hasText(@Nullable String str, Supplier e) throws E { - if (!MoreStrings.hasText(str)) { + if (!StringUtils.isEmpty(str)) { throw e.get(); } } - public static void hasText(@Nullable String str, String errorMessage) { - if (!MoreStrings.hasText(str)) { + public static void isEmpty(@Nullable String str, String errorMessage) { + if (!StringUtils.isEmpty(str)) { throw new IllegalArgumentException(errorMessage); } } - public static void hasText(@Nullable String str, String errorMessageTemplate, Object... args) { - if (!MoreStrings.hasText(str)) { + public static void isEmpty(@Nullable String str, String errorMessageTemplate, Object... args) { + if (!StringUtils.isEmpty(str)) { + throw new IllegalArgumentException(String.format(errorMessageTemplate, args)); + } + } + + // isNotEmpty - String + public static void isNotEmpty(@Nullable String str, Supplier e) throws E { + if (!StringUtils.isNotEmpty(str)) { + throw e.get(); + } + } + + public static void isNotEmpty(@Nullable String str, String errorMessage) { + if (!StringUtils.isNotEmpty(str)) { + throw new IllegalArgumentException(errorMessage); + } + } + + public static void isNotEmpty(@Nullable String str, String errorMessageTemplate, Object... args) { + if (!StringUtils.isNotEmpty(str)) { + throw new IllegalArgumentException(String.format(errorMessageTemplate, args)); + } + } + + // isNotBlank - String + public static void isNotBlank(@Nullable String str, Supplier e) throws E { + if (!StringUtils.isNotBlank(str)) { + throw e.get(); + } + } + + public static void isNotBlank(@Nullable String str, String errorMessage) { + if (!StringUtils.isNotBlank(str)) { + throw new IllegalArgumentException(errorMessage); + } + } + + public static void isNotBlank(@Nullable String str, String errorMessageTemplate, Object... args) { + if (!StringUtils.isNotBlank(str)) { throw new IllegalArgumentException(String.format(errorMessageTemplate, args)); } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java b/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java index c24663c..342e10a 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java @@ -17,11 +17,11 @@ package xyz.zhouxy.plusone.commons.util; import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableMap.Builder; - import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; /** @@ -32,7 +32,7 @@ public abstract class Enumeration> implements Comparabl protected final String name; protected Enumeration(final int id, final String name) { - Assert.hasText(name, "Name of enumeration must has text."); + Assert.isNotBlank(name, "Name of enumeration must has text."); this.id = id; this.name = name; } @@ -73,20 +73,20 @@ public abstract class Enumeration> implements Comparabl } protected static final class ValueSet> { - private final ImmutableMap valueMap; + private final Map valueMap; - private ValueSet(ImmutableMap valueMap) { + private ValueSet(Map valueMap) { this.valueMap = valueMap; } @SafeVarargs @StaticFactoryMethod(ValueSet.class) public static > ValueSet of(T... values) { - Builder builder = ImmutableMap.builder(); + Map temp = new HashMap<>(); for (T value : values) { - builder.put(value.getId(), value); + temp.put(value.getId(), value); } - return new ValueSet<>(builder.build()); + return new ValueSet<>(Collections.unmodifiableMap(temp)); } public T get(int id) { diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/MoreArrays.java b/src/main/java/xyz/zhouxy/plusone/commons/util/MoreArrays.java deleted file mode 100644 index 284244e..0000000 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/MoreArrays.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2022-2023 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package xyz.zhouxy.plusone.commons.util; - -import javax.annotation.Nullable; - -/** - * MoreArrays - * - *

- * 数组工具类 - * - * @author ZhouXY - * @since 0.1.0 - */ -public class MoreArrays { - - @SafeVarargs - public static T[] newArray(final T... values) { - return values; - } - - public static short[] newArray(final short... values) { - return values; - } - - public static int[] newArray(final int... values) { - return values; - } - - public static long[] newArray(final long... values) { - return values; - } - - public static byte[] newArray(final byte... values) { - return values; - } - - public static boolean[] newArray(final boolean... values) { - return values; - } - - public static char[] newArray(final char... values) { - return values; - } - - public static double[] newArray(final double... values) { - return values; - } - - public static float[] newArray(final float... values) { - return values; - } - - 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/MoreStrings.java b/src/main/java/xyz/zhouxy/plusone/commons/util/MoreStrings.java deleted file mode 100644 index a1bd9e7..0000000 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/MoreStrings.java +++ /dev/null @@ -1,23 +0,0 @@ -package xyz.zhouxy.plusone.commons.util; - -import javax.annotation.Nullable; - -public class MoreStrings { - public static boolean hasText(@Nullable String str) { - return (str != null && !str.isEmpty() && containsText(str)); - } - - private static boolean containsText(CharSequence str) { - int strLen = str.length(); - for (int i = 0; i < strLen; i++) { - if (!Character.isWhitespace(str.charAt(i))) { - return true; - } - } - return false; - } - - private MoreStrings() { - throw new IllegalStateException("Utility class"); - } -} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/PagingAndSortingQueryParams.java b/src/main/java/xyz/zhouxy/plusone/commons/util/PagingAndSortingQueryParams.java index 1c412c6..b34bea2 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/PagingAndSortingQueryParams.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/PagingAndSortingQueryParams.java @@ -16,15 +16,14 @@ package xyz.zhouxy.plusone.commons.util; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Set; import javax.annotation.Nullable; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; - import xyz.zhouxy.plusone.commons.annotation.Overridable; /** @@ -41,27 +40,28 @@ import xyz.zhouxy.plusone.commons.annotation.Overridable; public class PagingAndSortingQueryParams { private static final int DEFAULT_PAGE_SIZE = 15; - protected final List orderBy = Lists.newLinkedList(); + protected final List orderBy = new LinkedList<>(); protected int size; protected long pageNum; private final Set sortableColNames; public PagingAndSortingQueryParams() { - this.sortableColNames = ImmutableSet.of(); + this.sortableColNames = Collections.emptySet(); } public PagingAndSortingQueryParams(String... sortableColNames) { for (String colName : sortableColNames) { - Assert.hasText(colName, "Column name must has text."); + Assert.isNotBlank(colName, "Column name must has text."); } - this.sortableColNames = ImmutableSet.copyOf(sortableColNames); + Set sortableColNameSet = new HashSet<>(sortableColNames.length); + this.sortableColNames = Collections.unmodifiableSet(sortableColNameSet); } // Getters public final List getOrderBy() { - return ImmutableList.copyOf(this.orderBy); + return Collections.unmodifiableList(this.orderBy); } public final int getSize() { diff --git a/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java b/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java index b1d5154..dd04458 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java @@ -2,13 +2,13 @@ package xyz.zhouxy.plusone.commons; import static org.junit.jupiter.api.Assertions.assertSame; -import com.google.common.collect.Lists; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import xyz.zhouxy.plusone.commons.util.Enumeration; +import java.util.ArrayList; import java.util.Collection; class EnumerationTests { @@ -19,7 +19,7 @@ class EnumerationTests { void testEnumeration() { assertSame(EntityStatus.AVAILABLE, EntityStatus.of(0)); assertSame(Result.SUCCESSFUL, Result.of(1)); - Collection>> enums = Lists.newArrayList(); + Collection>> enums = new ArrayList<>(); enums.addAll(EntityStatus.constants()); enums.addAll(Result.constants()); for (Comparable> anEnum : enums) { diff --git a/src/test/java/xyz/zhouxy/plusone/commons/function/FunctionTests.java b/src/test/java/xyz/zhouxy/plusone/commons/function/FunctionTests.java index 1ec1353..00e5825 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/function/FunctionTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/function/FunctionTests.java @@ -13,7 +13,7 @@ class FunctionTests { void test() { String str = ""; Predicate predicate = Predicates.of(Objects::nonNull) - .and(StringUtils::isNotEmpty); + .and(StringUtils::isNotBlank); Assert.isFalse(predicate.test(str), "校验应是不通过"); } }