Files
plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java

295 lines
11 KiB
Java
Raw Normal View History

2023-04-21 01:46:46 +08:00
package xyz.zhouxy.plusone.commons.util;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Supplier;
import javax.annotation.Nullable;
2023-04-29 15:35:34 +08:00
import com.google.common.base.Strings;
2023-04-21 01:46:46 +08:00
public class Assert {
// isTrue
public static <E extends Throwable> void isTrue(@Nullable Boolean conditions, Supplier<E> e) throws E {
if (!Boolean.TRUE.equals(conditions)) {
2023-04-21 01:46:46 +08:00
throw e.get();
}
}
public static void isTrue(@Nullable Boolean conditions, String errorMessage) {
2023-04-29 15:54:00 +08:00
if (!Boolean.TRUE.equals(conditions)) {
throw new IllegalArgumentException(errorMessage);
}
2023-04-21 01:46:46 +08:00
}
public static void isTrue(@Nullable Boolean conditions, String errorMessageTemplate, Object... args) {
2023-04-29 15:54:00 +08:00
if (!Boolean.TRUE.equals(conditions)) {
throw new IllegalArgumentException(String.format(errorMessageTemplate, args));
}
2023-04-21 01:46:46 +08:00
}
// isFalse
public static <E extends Throwable> void isFalse(@Nullable Boolean conditions, Supplier<E> e) throws E {
2023-04-29 15:54:00 +08:00
if (!Boolean.FALSE.equals(conditions)) {
throw e.get();
}
2023-04-21 01:46:46 +08:00
}
public static void isFalse(@Nullable Boolean conditions, String errorMessage) {
2023-04-29 15:54:00 +08:00
if (!Boolean.FALSE.equals(conditions)) {
throw new IllegalArgumentException(errorMessage);
}
2023-04-21 01:46:46 +08:00
}
public static void isFalse(@Nullable Boolean conditions, String errorMessageTemplate, Object... args) {
2023-04-29 15:54:00 +08:00
if (!Boolean.FALSE.equals(conditions)) {
throw new IllegalArgumentException(String.format(errorMessageTemplate, args));
}
2023-04-21 01:46:46 +08:00
}
// between - int
2023-04-29 15:54:00 +08:00
private static boolean between(int value, int min, int max) {
return value >= min && value < max;
}
2023-04-21 01:46:46 +08:00
public static <E extends Throwable> void between(int value, int min, int max, Supplier<E> e) throws E {
2023-04-29 15:54:00 +08:00
Assert.isTrue(between(value, min, max), e);
2023-04-21 01:46:46 +08:00
}
public static void between(int value, int min, int max, String errorMessage) {
2023-04-29 15:54:00 +08:00
Assert.isTrue(between(value, min, max), errorMessage);
2023-04-21 01:46:46 +08:00
}
public static void between(int value, int min, int max, String errorMessageTemplate, Object... args) {
2023-04-29 15:54:00 +08:00
Assert.isTrue(between(value, min, max), errorMessageTemplate, args);
2023-04-21 01:46:46 +08:00
}
// between - long
2023-04-29 15:54:00 +08:00
private static boolean between(long value, long min, long max) {
return value >= min && value < max;
}
2023-04-21 01:46:46 +08:00
public static <E extends Throwable> void between(long value, long min, long max, Supplier<E> e) throws E {
2023-04-29 15:54:00 +08:00
Assert.isTrue(between(value, min, max), e);
2023-04-21 01:46:46 +08:00
}
public static void between(long value, long min, long max, String errorMessage) {
2023-04-29 15:54:00 +08:00
Assert.isTrue(between(value, min, max), errorMessage);
2023-04-21 01:46:46 +08:00
}
public static void between(long value, long min, long max, String errorMessageTemplate, Object... args) {
2023-04-29 15:54:00 +08:00
Assert.isTrue(between(value, min, max), errorMessageTemplate, args);
2023-04-21 01:46:46 +08:00
}
// between - double
2023-04-29 15:54:00 +08:00
private static boolean between(double value, double min, double max) {
return value >= min && value < max;
}
2023-04-21 01:46:46 +08:00
public static <E extends Throwable> void between(double value, double min, double max, Supplier<E> e) throws E {
2023-04-29 15:54:00 +08:00
Assert.isTrue(between(value, min, max), e);
2023-04-21 01:46:46 +08:00
}
public static void between(double value, double min, double max, String errorMessage) {
2023-04-29 15:54:00 +08:00
Assert.isTrue(between(value, min, max), errorMessage);
2023-04-21 01:46:46 +08:00
}
public static void between(double value, double min, double max, String errorMessageTemplate, Object... args) {
2023-04-29 15:54:00 +08:00
Assert.isTrue(between(value, min, max), errorMessageTemplate, args);
2023-04-21 01:46:46 +08:00
}
// notNull
public static <E extends Throwable> void notNull(Object value, Supplier<E> e) throws E {
Assert.isTrue(Objects.nonNull(value), e);
}
public static <E extends Throwable> void notNull(Object value, String errorMessage) throws E {
Assert.isTrue(Objects.nonNull(value), errorMessage);
}
public static void notNull(Object value, String errorMessageTemplate, Object... args) {
Assert.isTrue(Objects.nonNull(value), errorMessageTemplate, args);
}
2023-04-21 03:21:21 +08:00
// isEmpty - Collection
2023-04-29 18:38:14 +08:00
public static <E extends Throwable> void isEmpty(@Nullable Collection<?> collection, Supplier<E> e) throws E {
Assert.isTrue(MoreCollections.isEmpty(collection), e);
2023-04-29 15:54:00 +08:00
}
2023-04-29 18:38:14 +08:00
public static void isEmpty(@Nullable Collection<?> collection, String errorMessage) {
Assert.isTrue(MoreCollections.isEmpty(collection), errorMessage);
2023-04-21 01:46:46 +08:00
}
2023-04-29 18:38:14 +08:00
public static void isEmpty(@Nullable Collection<?> collection, String errorMessageTemplate, Object... args) {
Assert.isTrue(MoreCollections.isEmpty(collection), errorMessageTemplate, args);
2023-04-21 01:46:46 +08:00
}
2023-04-21 03:21:21 +08:00
// isNotEmpty - Collection
2023-04-29 18:38:14 +08:00
public static <E extends Throwable> void isNotEmpty(@Nullable Collection<?> collection, Supplier<E> e) throws E {
Assert.isTrue(MoreCollections.isNotEmpty(collection), e);
2023-04-21 01:46:46 +08:00
}
2023-04-29 18:38:14 +08:00
public static void isNotEmpty(@Nullable Collection<?> collection, String errorMessage) {
Assert.isTrue(MoreCollections.isNotEmpty(collection), errorMessage);
2023-04-21 01:46:46 +08:00
}
2023-04-29 18:38:14 +08:00
public static void isNotEmpty(@Nullable Collection<?> collection, String errorMessageTemplate, Object... args) {
Assert.isTrue(MoreCollections.isNotEmpty(collection), errorMessageTemplate, args);
2023-04-21 01:46:46 +08:00
}
2023-04-21 03:21:21 +08:00
// isEmpty - Array
public static <T, E extends Throwable> void isEmpty(@Nullable T[] arr, Supplier<E> e) throws E {
2023-04-29 18:38:14 +08:00
Assert.isTrue(MoreArrays.isEmpty(arr), e);
2023-04-21 03:21:21 +08:00
}
public static <T> void isEmpty(@Nullable T[] arr, String errorMessage) {
2023-04-29 18:38:14 +08:00
Assert.isTrue(MoreArrays.isEmpty(arr), errorMessage);
2023-04-21 03:21:21 +08:00
}
public static <T> void isEmpty(@Nullable T[] arr, String errorMessageTemplate, Object... args) {
2023-04-29 18:38:14 +08:00
Assert.isTrue(MoreArrays.isEmpty(arr), errorMessageTemplate, args);
2023-04-21 03:21:21 +08:00
}
// isEmpty - int[]
public static <E extends Throwable> void isEmpty(@Nullable int[] arr, Supplier<E> e) throws E {
Assert.isTrue(MoreArrays.isEmpty(arr), e);
}
public static void isEmpty(@Nullable int[] arr, String errorMessage) {
Assert.isTrue(MoreArrays.isEmpty(arr), errorMessage);
}
public static void isEmpty(@Nullable int[] arr, String errorMessageTemplate, Object... args) {
Assert.isTrue(MoreArrays.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);
}
public static void isEmpty(@Nullable long[] arr, String errorMessage) {
Assert.isTrue(MoreArrays.isEmpty(arr), errorMessage);
}
public static void isEmpty(@Nullable long[] arr, String errorMessageTemplate, Object... args) {
Assert.isTrue(MoreArrays.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);
}
public static void isEmpty(@Nullable double[] arr, String errorMessage) {
Assert.isTrue(MoreArrays.isEmpty(arr), errorMessage);
}
public static void isEmpty(@Nullable double[] arr, String errorMessageTemplate, Object... args) {
Assert.isTrue(MoreArrays.isEmpty(arr), errorMessageTemplate, args);
}
2023-04-29 15:35:34 +08:00
// isNotEmpty - Array
2023-04-21 03:21:21 +08:00
public static <T, E extends Throwable> void isNotEmpty(@Nullable T[] arr, Supplier<E> e) throws E {
2023-04-29 18:38:14 +08:00
Assert.isTrue(MoreArrays.isNotEmpty(arr), e);
2023-04-21 03:21:21 +08:00
}
public static <T> void isNotEmpty(@Nullable T[] arr, String errorMessage) {
2023-04-29 18:38:14 +08:00
Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessage);
2023-04-21 03:21:21 +08:00
}
public static <T> void isNotEmpty(@Nullable T[] arr, String errorMessageTemplate, Object... args) {
2023-04-29 18:38:14 +08:00
Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessageTemplate, args);
2023-04-21 03:21:21 +08:00
}
// isNotEmpty - int[]
public static <E extends Throwable> void isNotEmpty(@Nullable int[] arr, Supplier<E> e) throws E {
Assert.isTrue(MoreArrays.isNotEmpty(arr), e);
}
public static void isNotEmpty(@Nullable int[] arr, String errorMessage) {
Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessage);
}
public static void isNotEmpty(@Nullable int[] arr, String errorMessageTemplate, Object... args) {
Assert.isTrue(MoreArrays.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);
}
public static void isNotEmpty(@Nullable long[] arr, String errorMessage) {
Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessage);
}
public static void isNotEmpty(@Nullable long[] arr, String errorMessageTemplate, Object... args) {
Assert.isTrue(MoreArrays.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);
}
public static void isNotEmpty(@Nullable double[] arr, String errorMessage) {
Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessage);
}
public static void isNotEmpty(@Nullable double[] arr, String errorMessageTemplate, Object... args) {
Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessageTemplate, args);
}
2023-04-29 15:35:34 +08:00
// isEmpty - String
public static <E extends Throwable> void isEmpty(@Nullable String str, Supplier<E> e) throws E {
2023-04-29 15:54:00 +08:00
Assert.isTrue(Strings.isNullOrEmpty(str), e);
2023-04-21 03:21:21 +08:00
}
2023-04-29 15:35:34 +08:00
public static void isEmpty(@Nullable String str, String errorMessage) {
2023-04-29 15:54:00 +08:00
Assert.isTrue(Strings.isNullOrEmpty(str), errorMessage);
2023-04-21 03:21:21 +08:00
}
2023-04-29 15:35:34 +08:00
public static void isEmpty(@Nullable String str, String errorMessageTemplate, Object... args) {
2023-04-29 15:54:00 +08:00
Assert.isTrue(Strings.isNullOrEmpty(str), errorMessageTemplate, args);
2023-04-21 03:21:21 +08:00
}
2023-04-29 15:35:34 +08:00
// isNotEmpty - String
public static <E extends Throwable> void isNotEmpty(@Nullable String str, Supplier<E> e) throws E {
2023-04-29 15:54:00 +08:00
Assert.isTrue(!Strings.isNullOrEmpty(str), e);
2023-04-21 03:21:21 +08:00
}
2023-04-29 15:35:34 +08:00
public static void isNotEmpty(@Nullable String str, String errorMessage) {
2023-04-29 15:54:00 +08:00
Assert.isTrue(!Strings.isNullOrEmpty(str), errorMessage);
2023-04-21 03:21:21 +08:00
}
2023-04-29 15:35:34 +08:00
public static void isNotEmpty(@Nullable String str, String errorMessageTemplate, Object... args) {
2023-04-29 15:54:00 +08:00
Assert.isTrue(!Strings.isNullOrEmpty(str), errorMessageTemplate, args);
2023-04-21 03:21:21 +08:00
}
2023-04-29 17:45:20 +08:00
// hasText - String
public static <E extends Throwable> void hasText(@Nullable String str, Supplier<E> e) throws E {
2023-04-30 11:11:44 +08:00
if (!MoreStrings.hasText(str)) {
throw e.get();
}
2023-04-29 17:45:20 +08:00
}
public static void hasText(@Nullable String str, String errorMessage) {
2023-04-30 11:11:44 +08:00
if (!MoreStrings.hasText(str)) {
throw new IllegalArgumentException(errorMessage);
}
2023-04-29 17:45:20 +08:00
}
public static void hasText(@Nullable String str, String errorMessageTemplate, Object... args) {
2023-04-30 11:11:44 +08:00
if (!MoreStrings.hasText(str)) {
throw new IllegalArgumentException(String.format(errorMessageTemplate, args));
}
2023-04-29 17:45:20 +08:00
}
2023-04-25 09:06:00 +08:00
// private constructor
2023-04-21 01:46:46 +08:00
private Assert() {
throw new IllegalStateException("Utility class");
}
}