Files
plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java
2023-05-13 12:53:57 +08:00

336 lines
12 KiB
Java

/*
* 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 java.util.Collection;
import java.util.Objects;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
public class Assert {
// isTrue
public static <E extends Throwable> void isTrue(@Nullable Boolean condition, Supplier<E> e) throws E {
if (!Boolean.TRUE.equals(condition)) {
throw e.get();
}
}
public static void isTrue(@Nullable Boolean condition) {
if (!Boolean.TRUE.equals(condition)) {
throw new IllegalArgumentException();
}
}
public static void isTrue(@Nullable Boolean condition, String errorMessage) {
if (!Boolean.TRUE.equals(condition)) {
throw new IllegalArgumentException(errorMessage);
}
}
public static void isTrue(@Nullable Boolean condition, String errorMessageTemplate, Object... args) {
if (!Boolean.TRUE.equals(condition)) {
throw new IllegalArgumentException(String.format(errorMessageTemplate, args));
}
}
// isFalse
public static <E extends Throwable> void isFalse(@Nullable Boolean condition, Supplier<E> e) throws E {
if (!Boolean.FALSE.equals(condition)) {
throw e.get();
}
}
public static void isFalse(@Nullable Boolean condition) {
if (!Boolean.FALSE.equals(condition)) {
throw new IllegalArgumentException();
}
}
public static void isFalse(@Nullable Boolean condition, String errorMessage) {
if (!Boolean.FALSE.equals(condition)) {
throw new IllegalArgumentException(errorMessage);
}
}
public static void isFalse(@Nullable Boolean condition, String errorMessageTemplate, Object... args) {
if (!Boolean.FALSE.equals(condition)) {
throw new IllegalArgumentException(String.format(errorMessageTemplate, args));
}
}
// between - int
private static boolean between(int value, int min, int max) {
return value >= min && value < max;
}
public static <E extends Throwable> void between(int value, int min, int max, Supplier<E> e) throws E {
Assert.isTrue(between(value, min, max), e);
}
public static void between(int value, int min, int max, String errorMessage) {
Assert.isTrue(between(value, min, max), errorMessage);
}
public static void between(int value, int min, int max, String errorMessageTemplate, Object... args) {
Assert.isTrue(between(value, min, max), errorMessageTemplate, args);
}
// between - long
private static boolean between(long value, long min, long max) {
return value >= min && value < max;
}
public static <E extends Throwable> void between(long value, long min, long max, Supplier<E> e) throws E {
Assert.isTrue(between(value, min, max), e);
}
public static void between(long value, long min, long max, String errorMessage) {
Assert.isTrue(between(value, min, max), errorMessage);
}
public static void between(long value, long min, long max, String errorMessageTemplate, Object... args) {
Assert.isTrue(between(value, min, max), errorMessageTemplate, args);
}
// between - double
private static boolean between(double value, double min, double max) {
return value >= min && value < max;
}
public static <E extends Throwable> void between(double value, double min, double max, Supplier<E> e) throws E {
Assert.isTrue(between(value, min, max), e);
}
public static void between(double value, double min, double max, String errorMessage) {
Assert.isTrue(between(value, min, max), errorMessage);
}
public static void between(double value, double min, double max, String errorMessageTemplate, Object... args) {
Assert.isTrue(between(value, min, max), errorMessageTemplate, args);
}
// 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);
}
// isEmpty - Collection
public static <E extends Throwable> void isEmpty(@Nullable Collection<?> collection, Supplier<E> e) throws E {
Assert.isTrue(MoreCollections.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 errorMessageTemplate, Object... args) {
Assert.isTrue(MoreCollections.isEmpty(collection), errorMessageTemplate, args);
}
// isNotEmpty - Collection
public static <E extends Throwable> void isNotEmpty(@Nullable Collection<?> collection, Supplier<E> e) throws E {
Assert.isTrue(MoreCollections.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 errorMessageTemplate, Object... args) {
Assert.isTrue(MoreCollections.isNotEmpty(collection), errorMessageTemplate, args);
}
// isEmpty - Array
public static <T, E extends Throwable> void isEmpty(@Nullable T[] arr, Supplier<E> e) throws E {
Assert.isTrue(ArrayUtils.isEmpty(arr), e);
}
public static <T> void isEmpty(@Nullable T[] arr, String errorMessage) {
Assert.isTrue(ArrayUtils.isEmpty(arr), errorMessage);
}
public static <T> void isEmpty(@Nullable T[] arr, String errorMessageTemplate, Object... 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(ArrayUtils.isEmpty(arr), e);
}
public static void isEmpty(@Nullable int[] arr, String errorMessage) {
Assert.isTrue(ArrayUtils.isEmpty(arr), errorMessage);
}
public static void isEmpty(@Nullable int[] arr, String errorMessageTemplate, Object... 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(ArrayUtils.isEmpty(arr), e);
}
public static void isEmpty(@Nullable long[] arr, String errorMessage) {
Assert.isTrue(ArrayUtils.isEmpty(arr), errorMessage);
}
public static void isEmpty(@Nullable long[] arr, String errorMessageTemplate, Object... 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(ArrayUtils.isEmpty(arr), e);
}
public static void isEmpty(@Nullable double[] arr, String errorMessage) {
Assert.isTrue(ArrayUtils.isEmpty(arr), errorMessage);
}
public static void isEmpty(@Nullable double[] arr, String errorMessageTemplate, Object... 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(ArrayUtils.isNotEmpty(arr), e);
}
public static <T> void isNotEmpty(@Nullable T[] arr, String errorMessage) {
Assert.isTrue(ArrayUtils.isNotEmpty(arr), errorMessage);
}
public static <T> void isNotEmpty(@Nullable T[] arr, String errorMessageTemplate, Object... 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(ArrayUtils.isNotEmpty(arr), e);
}
public static void isNotEmpty(@Nullable int[] arr, String errorMessage) {
Assert.isTrue(ArrayUtils.isNotEmpty(arr), errorMessage);
}
public static void isNotEmpty(@Nullable int[] arr, String errorMessageTemplate, Object... 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(ArrayUtils.isNotEmpty(arr), e);
}
public static void isNotEmpty(@Nullable long[] arr, String errorMessage) {
Assert.isTrue(ArrayUtils.isNotEmpty(arr), errorMessage);
}
public static void isNotEmpty(@Nullable long[] arr, String errorMessageTemplate, Object... 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(ArrayUtils.isNotEmpty(arr), e);
}
public static void isNotEmpty(@Nullable double[] arr, String errorMessage) {
Assert.isTrue(ArrayUtils.isNotEmpty(arr), errorMessage);
}
public static void isNotEmpty(@Nullable double[] arr, String errorMessageTemplate, Object... 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 {
if (!StringUtils.isEmpty(str)) {
throw e.get();
}
}
public static void isEmpty(@Nullable String str, String errorMessage) {
if (!StringUtils.isEmpty(str)) {
throw new IllegalArgumentException(errorMessage);
}
}
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));
}
}
// private constructor
private Assert() {
throw new IllegalStateException("Utility class");
}
}