修改断言类,一般使用 Preconditions,需要时使用 PreconditionsExt 即可。

feature/net-util
ZhouXY108 2023-07-19 00:32:08 +08:00
parent 7c37d364d4
commit 3c4310e603
11 changed files with 89 additions and 363 deletions

View File

@ -18,9 +18,11 @@ package xyz.zhouxy.plusone.commons.domain;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import com.fasterxml.jackson.annotation.JsonValue; import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.base.Preconditions;
import xyz.zhouxy.plusone.commons.util.Assert;
import xyz.zhouxy.plusone.commons.util.RegexUtil; import xyz.zhouxy.plusone.commons.util.RegexUtil;
/** /**
@ -33,9 +35,9 @@ public abstract class ValidatableStringRecord {
private final String value; private final String value;
protected ValidatableStringRecord(String value, Pattern pattern) { protected ValidatableStringRecord(String value, Pattern pattern) {
Assert.notNull(pattern, "The pattern must not be null."); Preconditions.checkNotNull(pattern, "The pattern must not be null.");
Assert.isNotBlank(value, "The value must be has text."); Preconditions.checkArgument(StringUtils.isNotBlank(value), "The value must be has text.");
Assert.isTrue(RegexUtil.matches(value, pattern)); Preconditions.checkArgument(RegexUtil.matches(value, pattern));
this.value = value; this.value = value;
} }

View File

@ -29,21 +29,23 @@ import java.util.OptionalInt;
import java.util.OptionalLong; import java.util.OptionalLong;
import java.util.Set; import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import com.google.common.annotations.Beta; import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import xyz.zhouxy.plusone.commons.collection.AbstractMapWrapper; import xyz.zhouxy.plusone.commons.collection.AbstractMapWrapper;
import xyz.zhouxy.plusone.commons.util.Assert;
import xyz.zhouxy.plusone.commons.util.OptionalUtil; import xyz.zhouxy.plusone.commons.util.OptionalUtil;
@Beta @Beta
public class DbRecord extends AbstractMapWrapper<String, Object, DbRecord> { public class DbRecord extends AbstractMapWrapper<String, Object, DbRecord> {
public DbRecord() { public DbRecord() {
super(new HashMap<>(), k -> Assert.isNotBlank(k, "Key must has text."), null); super(new HashMap<>(), k -> Preconditions.checkArgument(StringUtils.isNotBlank(k), "Key must has text."), null);
} }
public DbRecord(Map<String, Object> map) { public DbRecord(Map<String, Object> map) {
super(map, k -> Assert.isNotBlank(k, "Key must has text."), null); super(map, k -> Preconditions.checkArgument(StringUtils.isNotBlank(k), "Key must has text."), null);
} }
public Optional<String> getValueAsString(String key) { public Optional<String> getValueAsString(String key) {

View File

@ -39,9 +39,9 @@ import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import com.google.common.annotations.Beta; import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import xyz.zhouxy.plusone.commons.util.Assert;
import xyz.zhouxy.plusone.commons.util.MoreArrays; import xyz.zhouxy.plusone.commons.util.MoreArrays;
import xyz.zhouxy.plusone.commons.util.MoreCollections; import xyz.zhouxy.plusone.commons.util.MoreCollections;
import xyz.zhouxy.plusone.commons.util.OptionalUtil; import xyz.zhouxy.plusone.commons.util.OptionalUtil;
@ -211,7 +211,7 @@ public class SimpleJdbcTemplate {
} }
public <T extends Exception> void tx(final IAtom<T> atom) throws SQLException, T { public <T extends Exception> void tx(final IAtom<T> atom) throws SQLException, T {
Assert.notNull(atom, "Atom can not be null."); Preconditions.checkNotNull(atom, "Atom can not be null.");
try { try {
this.conn.setAutoCommit(false); this.conn.setAutoCommit(false);
atom.execute(); atom.execute();
@ -257,8 +257,8 @@ public class SimpleJdbcTemplate {
} }
public static <T> List<Object[]> buildBatchParams(final Collection<T> c, final Function<T, Object[]> function) { public static <T> List<Object[]> buildBatchParams(final Collection<T> c, final Function<T, Object[]> function) {
Assert.notNull(c, "The collection can not be null."); Preconditions.checkNotNull(c, "The collection can not be null.");
Assert.notNull(function, "The function can not be null."); Preconditions.checkNotNull(function, "The function can not be null.");
if (MoreCollections.isEmpty(c)) { if (MoreCollections.isEmpty(c)) {
return Collections.emptyList(); return Collections.emptyList();
} }

View File

@ -1,326 +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 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
public static <E extends Throwable> void between(int value, int min, int max, Supplier<E> e) throws E {
Assert.isTrue(Numbers.between(value, min, max), e);
}
public static void between(int value, int min, int max, String errorMessage) {
Assert.isTrue(Numbers.between(value, min, max), errorMessage);
}
public static void between(int value, int min, int max, String errorMessageTemplate, Object... args) {
Assert.isTrue(Numbers.between(value, min, max), errorMessageTemplate, args);
}
// between - long
public static <E extends Throwable> void between(long value, long min, long max, Supplier<E> e) throws E {
Assert.isTrue(Numbers.between(value, min, max), e);
}
public static void between(long value, long min, long max, String errorMessage) {
Assert.isTrue(Numbers.between(value, min, max), errorMessage);
}
public static void between(long value, long min, long max, String errorMessageTemplate, Object... args) {
Assert.isTrue(Numbers.between(value, min, max), errorMessageTemplate, args);
}
// between - double
public static <E extends Throwable> void between(double value, double min, double max, Supplier<E> e) throws E {
Assert.isTrue(Numbers.between(value, min, max), e);
}
public static void between(double value, double min, double max, String errorMessage) {
Assert.isTrue(Numbers.between(value, min, max), errorMessage);
}
public static void between(double value, double min, double max, String errorMessageTemplate, Object... args) {
Assert.isTrue(Numbers.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");
}
}

View File

@ -11,8 +11,11 @@ import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.TimeZone; import java.util.TimeZone;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTimeZone; import org.joda.time.DateTimeZone;
import com.google.common.base.Preconditions;
import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap; import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap;
import xyz.zhouxy.plusone.commons.collection.MapWrapper; import xyz.zhouxy.plusone.commons.collection.MapWrapper;
@ -21,8 +24,8 @@ public class DateTimeUtil {
private static final MapWrapper<String, DateTimeFormatter> DATE_TIME_FORMATTER_CACHE = MapWrapper private static final MapWrapper<String, DateTimeFormatter> DATE_TIME_FORMATTER_CACHE = MapWrapper
.<String, DateTimeFormatter>wrap(new SafeConcurrentHashMap<>()) .<String, DateTimeFormatter>wrap(new SafeConcurrentHashMap<>())
.keyChecker(pattern -> Assert.isNotBlank(pattern, "The pattern could not be blank.")) .keyChecker(pattern -> Preconditions.checkArgument(StringUtils.isNotBlank(pattern), "The pattern could not be blank."))
.valueChecker(formatter -> Assert.notNull(formatter, "The formatter could not be null.")) .valueChecker(formatter -> Preconditions.checkNotNull(formatter, "The formatter could not be null."))
.build(); .build();
public static DateTimeFormatter getDateTimeFormatter(String pattern) { public static DateTimeFormatter getDateTimeFormatter(String pattern) {

View File

@ -20,6 +20,8 @@ import java.util.function.Supplier;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.google.common.base.Preconditions;
/** /**
* *
* *
@ -42,9 +44,9 @@ public final class EnumUtil {
*/ */
@Deprecated @Deprecated
public static <E extends Enum<?>> E valueOf(Class<E> clazz, int ordinal) { public static <E extends Enum<?>> E valueOf(Class<E> clazz, int ordinal) {
Assert.notNull(clazz, "Clazz must not be null."); Preconditions.checkNotNull(clazz, "Clazz must not be null.");
E[] values = clazz.getEnumConstants(); E[] values = clazz.getEnumConstants();
Assert.isTrue((ordinal >= 0 && ordinal < values.length), PreconditionsExt.isTrue((ordinal >= 0 && ordinal < values.length),
() -> new EnumConstantNotPresentException(clazz, Integer.toString(ordinal))); () -> new EnumConstantNotPresentException(clazz, Integer.toString(ordinal)));
return values[ordinal]; return values[ordinal];
} }
@ -100,7 +102,7 @@ public final class EnumUtil {
@Deprecated @Deprecated
public static <E extends Enum<?>> E getValueOrDefault(Class<E> clazz, @Nullable Integer ordinal) { public static <E extends Enum<?>> E getValueOrDefault(Class<E> clazz, @Nullable Integer ordinal) {
return getValueOrDefault(clazz, ordinal, () -> { return getValueOrDefault(clazz, ordinal, () -> {
Assert.notNull(clazz, "Clazz must not be null."); Preconditions.checkNotNull(clazz, "Clazz must not be null.");
E[] values = clazz.getEnumConstants(); E[] values = clazz.getEnumConstants();
return values[0]; return values[0];
}); });
@ -121,8 +123,8 @@ public final class EnumUtil {
} }
public static <E extends Enum<?>> Integer checkOrdinal(Class<E> clazz, Integer ordinal) { public static <E extends Enum<?>> Integer checkOrdinal(Class<E> clazz, Integer ordinal) {
Assert.notNull(clazz, "Clazz must not be null."); Preconditions.checkNotNull(clazz, "Clazz must not be null.");
Assert.notNull(ordinal, "Ordinal must not be null."); Preconditions.checkNotNull(ordinal, "Ordinal must not be null.");
E[] values = clazz.getEnumConstants(); E[] values = clazz.getEnumConstants();
if (ordinal >= 0 && ordinal < values.length) { if (ordinal >= 0 && ordinal < values.length) {
return ordinal; return ordinal;

View File

@ -22,6 +22,10 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Preconditions;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
/** /**
@ -32,7 +36,7 @@ public abstract class Enumeration<T extends Enumeration<T>> implements Comparabl
protected final String name; protected final String name;
protected Enumeration(final int id, final String name) { protected Enumeration(final int id, final String name) {
Assert.isNotBlank(name, "Name of enumeration must has text."); Preconditions.checkArgument(StringUtils.isNotBlank(name), "Name of enumeration must has text.");
this.id = id; this.id = id;
this.name = name; this.name = name;
} }
@ -90,7 +94,7 @@ public abstract class Enumeration<T extends Enumeration<T>> implements Comparabl
} }
public T get(int id) { public T get(int id) {
Assert.isTrue(this.valueMap.containsKey(id), "%s 对应的值不存在", id); Preconditions.checkArgument(this.valueMap.containsKey(id), "%s 对应的值不存在", id);
return this.valueMap.get(id); return this.valueMap.get(id);
} }

View File

@ -18,6 +18,8 @@ package xyz.zhouxy.plusone.commons.util;
import java.util.List; import java.util.List;
import com.google.common.base.Preconditions;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
/** /**
@ -35,7 +37,7 @@ public class PageDTO<T> {
private final List<T> content; private final List<T> content;
private PageDTO(List<T> content, long total) { private PageDTO(List<T> content, long total) {
Assert.notNull(content, "Content must not be null."); Preconditions.checkNotNull(content, "Content must not be null.");
this.content = content; this.content = content;
this.total = total; this.total = total;
} }

View File

@ -24,6 +24,10 @@ import java.util.Set;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Preconditions;
import xyz.zhouxy.plusone.commons.annotation.Overridable; import xyz.zhouxy.plusone.commons.annotation.Overridable;
/** /**
@ -53,7 +57,7 @@ public class PagingAndSortingQueryParams {
public PagingAndSortingQueryParams(String... sortableColNames) { public PagingAndSortingQueryParams(String... sortableColNames) {
Set<String> sortableColNameSet = new HashSet<>(sortableColNames.length); Set<String> sortableColNameSet = new HashSet<>(sortableColNames.length);
for (String colName : sortableColNames) { for (String colName : sortableColNames) {
Assert.isNotBlank(colName, "Column name must has text."); Preconditions.checkArgument(StringUtils.isNotBlank(colName), "Column name must has text.");
sortableColNameSet.add(colName); sortableColNameSet.add(colName);
} }
this.sortableColNames = Collections.unmodifiableSet(sortableColNameSet); this.sortableColNames = Collections.unmodifiableSet(sortableColNameSet);
@ -85,7 +89,7 @@ public class PagingAndSortingQueryParams {
this.orderBy.clear(); this.orderBy.clear();
if (orderBy != null && !orderBy.isEmpty()) { if (orderBy != null && !orderBy.isEmpty()) {
for (String colName : orderBy) { for (String colName : orderBy) {
Assert.isTrue(this.sortableColNames.contains(colName), Preconditions.checkArgument(this.sortableColNames.contains(colName),
"The column name must be in the set of sortable columns."); "The column name must be in the set of sortable columns.");
} }
this.orderBy.addAll(orderBy); this.orderBy.addAll(orderBy);

View File

@ -0,0 +1,32 @@
/*
* 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.function.Supplier;
public class PreconditionsExt {
public static <E extends Throwable> void isTrue(boolean condition, Supplier<E> e) throws E {
if (!condition) {
throw e.get();
}
}
private PreconditionsExt() {
throw new IllegalStateException("Utility class");
}
}

View File

@ -22,6 +22,7 @@ import java.util.function.Supplier;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.google.common.base.Preconditions;
/** /**
* *
@ -48,12 +49,12 @@ public class RestfulResult {
} }
public static RestfulResult success(final String message) { public static RestfulResult success(final String message) {
Assert.notNull(message, "Message must not be null."); Preconditions.checkNotNull(message, "Message must not be null.");
return new RestfulResult(SUCCESS_STATUS, message); return new RestfulResult(SUCCESS_STATUS, message);
} }
public static RestfulResult success(final String message, @Nullable final Object data) { public static RestfulResult success(final String message, @Nullable final Object data) {
Assert.notNull(message, "Message must not be null."); Preconditions.checkNotNull(message, "Message must not be null.");
return new RestfulResult(SUCCESS_STATUS, message, data); return new RestfulResult(SUCCESS_STATUS, message, data);
} }
@ -62,20 +63,20 @@ public class RestfulResult {
} }
public static RestfulResult error(final Object status, final String message) { public static RestfulResult error(final Object status, final String message) {
Assert.notNull(status, "Status must not be null."); Preconditions.checkNotNull(status, "Status must not be null.");
Assert.notNull(message, "Message must not be null."); Preconditions.checkNotNull(message, "Message must not be null.");
return new RestfulResult(status, message); return new RestfulResult(status, message);
} }
public static RestfulResult error(final Object status, final String message, @Nullable final Object data) { public static RestfulResult error(final Object status, final String message, @Nullable final Object data) {
Assert.notNull(status, "Status must not be null."); Preconditions.checkNotNull(status, "Status must not be null.");
Assert.notNull(message, "Message must not be null."); Preconditions.checkNotNull(message, "Message must not be null.");
return new RestfulResult(status, message, data); return new RestfulResult(status, message, data);
} }
public static RestfulResult error(final Object status, final Throwable e) { public static RestfulResult error(final Object status, final Throwable e) {
Assert.notNull(status, "Status must not be null."); Preconditions.checkNotNull(status, "Status must not be null.");
Assert.notNull(e, "Exception must not be null."); Preconditions.checkNotNull(e, "Exception must not be null.");
String msg = e.getMessage(); String msg = e.getMessage();
if (msg == null) { if (msg == null) {
msg = ""; msg = "";
@ -85,16 +86,16 @@ public class RestfulResult {
public static RestfulResult of(final boolean isSuccess, public static RestfulResult of(final boolean isSuccess,
final Supplier<RestfulResult> success, final Supplier<RestfulResult> error) { final Supplier<RestfulResult> success, final Supplier<RestfulResult> error) {
Assert.notNull(success, "Success supplier must not be null."); Preconditions.checkNotNull(success, "Success supplier must not be null.");
Assert.notNull(error, "Error supplier must not be null."); Preconditions.checkNotNull(error, "Error supplier must not be null.");
return isSuccess ? success.get() : error.get(); return isSuccess ? success.get() : error.get();
} }
public static RestfulResult of(final BooleanSupplier isSuccess, public static RestfulResult of(final BooleanSupplier isSuccess,
final Supplier<RestfulResult> success, final Supplier<RestfulResult> error) { final Supplier<RestfulResult> success, final Supplier<RestfulResult> error) {
Assert.notNull(isSuccess, "Conditions for success must not be null."); Preconditions.checkNotNull(isSuccess, "Conditions for success must not be null.");
Assert.notNull(success, "Success supplier must not be null."); Preconditions.checkNotNull(success, "Success supplier must not be null.");
Assert.notNull(error, "Error supplier must not be null."); Preconditions.checkNotNull(error, "Error supplier must not be null.");
return isSuccess.getAsBoolean() ? success.get() : error.get(); return isSuccess.getAsBoolean() ? success.get() : error.get();
} }