依赖 commons-lang3。

feature/net-util
ZhouXY108 2023-05-08 21:56:45 +08:00
parent be3caf14ec
commit f54b2d3cc6
9 changed files with 100 additions and 213 deletions

17
pom.xml
View File

@ -13,15 +13,11 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jackson.version>2.13.4</jackson.version>
<guava.version>31.1-jre</guava.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<google-jsr305.version>3.0.2</google-jsr305.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
@ -31,8 +27,13 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
<scope>test</scope>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>${google-jsr305.version}</version>
</dependency>
<dependency>

View File

@ -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;
}

View File

@ -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 <T, E extends Throwable> void isEmpty(@Nullable T[] arr, Supplier<E> e) throws E {
Assert.isTrue(MoreArrays.isEmpty(arr), e);
Assert.isTrue(ArrayUtils.isEmpty(arr), e);
}
public static <T> void isEmpty(@Nullable T[] arr, String errorMessage) {
Assert.isTrue(MoreArrays.isEmpty(arr), errorMessage);
Assert.isTrue(ArrayUtils.isEmpty(arr), errorMessage);
}
public static <T> 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 <E extends Throwable> void isEmpty(@Nullable int[] arr, Supplier<E> 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 <E extends Throwable> void isEmpty(@Nullable long[] arr, Supplier<E> 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 <E extends Throwable> void isEmpty(@Nullable double[] arr, Supplier<E> 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 <T, E extends Throwable> void isNotEmpty(@Nullable T[] arr, Supplier<E> e) throws E {
Assert.isTrue(MoreArrays.isNotEmpty(arr), e);
Assert.isTrue(ArrayUtils.isNotEmpty(arr), e);
}
public static <T> void isNotEmpty(@Nullable T[] arr, String errorMessage) {
Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessage);
Assert.isTrue(ArrayUtils.isNotEmpty(arr), errorMessage);
}
public static <T> 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 <E extends Throwable> void isNotEmpty(@Nullable int[] arr, Supplier<E> 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 <E extends Throwable> void isNotEmpty(@Nullable long[] arr, Supplier<E> 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 <E extends Throwable> void isNotEmpty(@Nullable double[] arr, Supplier<E> 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 <E extends Throwable> void isEmpty(@Nullable String str, Supplier<E> 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 <E extends Throwable> void isNotEmpty(@Nullable String str, Supplier<E> 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 <E extends Throwable> void hasText(@Nullable String str, Supplier<E> 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 <E extends Throwable> void isNotEmpty(@Nullable String str, Supplier<E> 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 <E extends Throwable> void isNotBlank(@Nullable String str, Supplier<E> 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));
}
}

View File

@ -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<T extends Enumeration<T>> 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<T extends Enumeration<T>> implements Comparabl
}
protected static final class ValueSet<T extends Enumeration<T>> {
private final ImmutableMap<Integer, T> valueMap;
private final Map<Integer, T> valueMap;
private ValueSet(ImmutableMap<Integer, T> valueMap) {
private ValueSet(Map<Integer, T> valueMap) {
this.valueMap = valueMap;
}
@SafeVarargs
@StaticFactoryMethod(ValueSet.class)
public static <T extends Enumeration<T>> ValueSet<T> of(T... values) {
Builder<Integer, T> builder = ImmutableMap.builder();
Map<Integer, T> 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) {

View File

@ -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
*
* <p>
*
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
* @since 0.1.0
*/
public class MoreArrays {
@SafeVarargs
public static <T> 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 <T> boolean isEmpty(@Nullable T[] arr) {
return arr == null || arr.length == 0;
}
public static <T> boolean isNotEmpty(@Nullable T[] arr) {
return arr != null && arr.length > 0;
}
private MoreArrays() {
throw new IllegalStateException("Utility class");
}
}

View File

@ -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");
}
}

View File

@ -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<String> orderBy = Lists.newLinkedList();
protected final List<String> orderBy = new LinkedList<>();
protected int size;
protected long pageNum;
private final Set<String> 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<String> sortableColNameSet = new HashSet<>(sortableColNames.length);
this.sortableColNames = Collections.unmodifiableSet(sortableColNameSet);
}
// Getters
public final List<String> getOrderBy() {
return ImmutableList.copyOf(this.orderBy);
return Collections.unmodifiableList(this.orderBy);
}
public final int getSize() {

View File

@ -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<Comparable<? extends Enumeration<?>>> enums = Lists.newArrayList();
Collection<Comparable<? extends Enumeration<?>>> enums = new ArrayList<>();
enums.addAll(EntityStatus.constants());
enums.addAll(Result.constants());
for (Comparable<? extends Enumeration<?>> anEnum : enums) {

View File

@ -13,7 +13,7 @@ class FunctionTests {
void test() {
String str = "";
Predicate<String> predicate = Predicates.<String>of(Objects::nonNull)
.and(StringUtils::isNotEmpty);
.and(StringUtils::isNotBlank);
Assert.isFalse(predicate.test(str), "校验应是不通过");
}
}