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 df041c4..0ff178b 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java @@ -18,9 +18,11 @@ package xyz.zhouxy.plusone.commons.domain; 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; /** @@ -33,9 +35,9 @@ public abstract class ValidatableStringRecord { private final String value; protected ValidatableStringRecord(String value, Pattern pattern) { - Assert.notNull(pattern, "The pattern must not be null."); - Assert.isNotBlank(value, "The value must be has text."); - Assert.isTrue(RegexUtil.matches(value, pattern)); + Preconditions.checkNotNull(pattern, "The pattern must not be null."); + Preconditions.checkArgument(StringUtils.isNotBlank(value), "The value must be has text."); + Preconditions.checkArgument(RegexUtil.matches(value, pattern)); this.value = value; } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/DbRecord.java b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/DbRecord.java index 472c00d..2c946b8 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/DbRecord.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/DbRecord.java @@ -29,21 +29,23 @@ import java.util.OptionalInt; import java.util.OptionalLong; import java.util.Set; +import org.apache.commons.lang3.StringUtils; + import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; import xyz.zhouxy.plusone.commons.collection.AbstractMapWrapper; -import xyz.zhouxy.plusone.commons.util.Assert; import xyz.zhouxy.plusone.commons.util.OptionalUtil; @Beta public class DbRecord extends AbstractMapWrapper { 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 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 getValueAsString(String key) { diff --git a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SimpleJdbcTemplate.java b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SimpleJdbcTemplate.java index 0600529..5cb3cfb 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SimpleJdbcTemplate.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SimpleJdbcTemplate.java @@ -39,9 +39,9 @@ import java.util.stream.Collectors; import org.apache.commons.lang3.ArrayUtils; import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; 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.MoreCollections; import xyz.zhouxy.plusone.commons.util.OptionalUtil; @@ -211,7 +211,7 @@ public class SimpleJdbcTemplate { } public void tx(final IAtom atom) throws SQLException, T { - Assert.notNull(atom, "Atom can not be null."); + Preconditions.checkNotNull(atom, "Atom can not be null."); try { this.conn.setAutoCommit(false); atom.execute(); @@ -257,8 +257,8 @@ public class SimpleJdbcTemplate { } public static List buildBatchParams(final Collection c, final Function function) { - Assert.notNull(c, "The collection can not be null."); - Assert.notNull(function, "The function can not be null."); + Preconditions.checkNotNull(c, "The collection can not be null."); + Preconditions.checkNotNull(function, "The function can not be null."); if (MoreCollections.isEmpty(c)) { return Collections.emptyList(); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java b/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java deleted file mode 100644 index fbfc17d..0000000 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/Assert.java +++ /dev/null @@ -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 void isTrue(@Nullable Boolean condition, Supplier 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 void isFalse(@Nullable Boolean condition, Supplier 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 void between(int value, int min, int max, Supplier 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 void between(long value, long min, long max, Supplier 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 void between(double value, double min, double max, Supplier 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 void notNull(Object value, Supplier e) throws E { - Assert.isTrue(Objects.nonNull(value), e); - } - - public static 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 void isEmpty(@Nullable Collection collection, Supplier 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 void isNotEmpty(@Nullable Collection collection, Supplier 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 void isEmpty(@Nullable T[] arr, Supplier e) throws E { - Assert.isTrue(ArrayUtils.isEmpty(arr), e); - } - - public static void isEmpty(@Nullable T[] arr, String errorMessage) { - Assert.isTrue(ArrayUtils.isEmpty(arr), errorMessage); - } - - public static void isEmpty(@Nullable T[] arr, String errorMessageTemplate, Object... args) { - Assert.isTrue(ArrayUtils.isEmpty(arr), errorMessageTemplate, args); - } - - // isEmpty - int[] - public static void isEmpty(@Nullable int[] arr, Supplier 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 void isEmpty(@Nullable long[] arr, Supplier 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 void isEmpty(@Nullable double[] arr, Supplier 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 void isNotEmpty(@Nullable T[] arr, Supplier e) throws E { - Assert.isTrue(ArrayUtils.isNotEmpty(arr), e); - } - - public static void isNotEmpty(@Nullable T[] arr, String errorMessage) { - Assert.isTrue(ArrayUtils.isNotEmpty(arr), errorMessage); - } - - public static void isNotEmpty(@Nullable T[] arr, String errorMessageTemplate, Object... args) { - Assert.isTrue(ArrayUtils.isNotEmpty(arr), errorMessageTemplate, args); - } - - // isNotEmpty - int[] - public static void isNotEmpty(@Nullable int[] arr, Supplier 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 void isNotEmpty(@Nullable long[] arr, Supplier 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 void isNotEmpty(@Nullable double[] arr, Supplier 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 void isEmpty(@Nullable String str, Supplier 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 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)); - } - } - - // private constructor - private Assert() { - throw new IllegalStateException("Utility class"); - } -} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java index f808353..1810eeb 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java @@ -11,8 +11,11 @@ import java.util.Calendar; import java.util.Date; import java.util.TimeZone; +import org.apache.commons.lang3.StringUtils; import org.joda.time.DateTimeZone; +import com.google.common.base.Preconditions; + import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap; import xyz.zhouxy.plusone.commons.collection.MapWrapper; @@ -21,8 +24,8 @@ public class DateTimeUtil { private static final MapWrapper DATE_TIME_FORMATTER_CACHE = MapWrapper .wrap(new SafeConcurrentHashMap<>()) - .keyChecker(pattern -> Assert.isNotBlank(pattern, "The pattern could not be blank.")) - .valueChecker(formatter -> Assert.notNull(formatter, "The formatter could not be null.")) + .keyChecker(pattern -> Preconditions.checkArgument(StringUtils.isNotBlank(pattern), "The pattern could not be blank.")) + .valueChecker(formatter -> Preconditions.checkNotNull(formatter, "The formatter could not be null.")) .build(); public static DateTimeFormatter getDateTimeFormatter(String pattern) { diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/EnumUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/EnumUtil.java index 3b92e83..1d3091f 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/EnumUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/EnumUtil.java @@ -20,6 +20,8 @@ import java.util.function.Supplier; import javax.annotation.Nullable; +import com.google.common.base.Preconditions; + /** * 枚举工具类 * @@ -42,9 +44,9 @@ public final class EnumUtil { */ @Deprecated public static > E valueOf(Class clazz, int ordinal) { - Assert.notNull(clazz, "Clazz must not be null."); + Preconditions.checkNotNull(clazz, "Clazz must not be null."); E[] values = clazz.getEnumConstants(); - Assert.isTrue((ordinal >= 0 && ordinal < values.length), + PreconditionsExt.isTrue((ordinal >= 0 && ordinal < values.length), () -> new EnumConstantNotPresentException(clazz, Integer.toString(ordinal))); return values[ordinal]; } @@ -100,7 +102,7 @@ public final class EnumUtil { @Deprecated public static > E getValueOrDefault(Class clazz, @Nullable Integer 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(); return values[0]; }); @@ -121,8 +123,8 @@ public final class EnumUtil { } public static > Integer checkOrdinal(Class clazz, Integer ordinal) { - Assert.notNull(clazz, "Clazz must not be null."); - Assert.notNull(ordinal, "Ordinal must not be null."); + Preconditions.checkNotNull(clazz, "Clazz must not be null."); + Preconditions.checkNotNull(ordinal, "Ordinal must not be null."); E[] values = clazz.getEnumConstants(); if (ordinal >= 0 && ordinal < values.length) { return ordinal; 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 342e10a..b22081f 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java @@ -22,6 +22,10 @@ import java.util.HashMap; import java.util.Map; import java.util.Objects; +import org.apache.commons.lang3.StringUtils; + +import com.google.common.base.Preconditions; + import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; /** @@ -32,7 +36,7 @@ public abstract class Enumeration> implements Comparabl protected 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.name = name; } @@ -90,7 +94,7 @@ public abstract class Enumeration> implements Comparabl } 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); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/PageDTO.java b/src/main/java/xyz/zhouxy/plusone/commons/util/PageDTO.java index 6cb51ad..d15e0c1 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/PageDTO.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/PageDTO.java @@ -18,6 +18,8 @@ package xyz.zhouxy.plusone.commons.util; import java.util.List; +import com.google.common.base.Preconditions; + import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; /** @@ -35,7 +37,7 @@ public class PageDTO { private final List content; private PageDTO(List content, long total) { - Assert.notNull(content, "Content must not be null."); + Preconditions.checkNotNull(content, "Content must not be null."); this.content = content; this.total = total; } 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 3f4a88b..f0eb1e3 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/PagingAndSortingQueryParams.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/PagingAndSortingQueryParams.java @@ -24,6 +24,10 @@ import java.util.Set; import javax.annotation.Nullable; +import org.apache.commons.lang3.StringUtils; + +import com.google.common.base.Preconditions; + import xyz.zhouxy.plusone.commons.annotation.Overridable; /** @@ -53,7 +57,7 @@ public class PagingAndSortingQueryParams { public PagingAndSortingQueryParams(String... sortableColNames) { Set sortableColNameSet = new HashSet<>(sortableColNames.length); 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); } this.sortableColNames = Collections.unmodifiableSet(sortableColNameSet); @@ -85,7 +89,7 @@ public class PagingAndSortingQueryParams { this.orderBy.clear(); if (orderBy != null && !orderBy.isEmpty()) { 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."); } this.orderBy.addAll(orderBy); diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/PreconditionsExt.java b/src/main/java/xyz/zhouxy/plusone/commons/util/PreconditionsExt.java new file mode 100644 index 0000000..6726233 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/PreconditionsExt.java @@ -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 void isTrue(boolean condition, Supplier e) throws E { + if (!condition) { + throw e.get(); + } + } + + private PreconditionsExt() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/RestfulResult.java b/src/main/java/xyz/zhouxy/plusone/commons/util/RestfulResult.java index efd3a16..5a52811 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/RestfulResult.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/RestfulResult.java @@ -22,6 +22,7 @@ import java.util.function.Supplier; import javax.annotation.Nullable; 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) { - Assert.notNull(message, "Message must not be null."); + Preconditions.checkNotNull(message, "Message must not be null."); return new RestfulResult(SUCCESS_STATUS, message); } 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); } @@ -62,20 +63,20 @@ public class RestfulResult { } public static RestfulResult error(final Object status, final String message) { - Assert.notNull(status, "Status must not be null."); - Assert.notNull(message, "Message must not be null."); + Preconditions.checkNotNull(status, "Status must not be null."); + Preconditions.checkNotNull(message, "Message must not be null."); return new RestfulResult(status, message); } public static RestfulResult error(final Object status, final String message, @Nullable final Object data) { - Assert.notNull(status, "Status must not be null."); - Assert.notNull(message, "Message must not be null."); + Preconditions.checkNotNull(status, "Status must not be null."); + Preconditions.checkNotNull(message, "Message must not be null."); return new RestfulResult(status, message, data); } public static RestfulResult error(final Object status, final Throwable e) { - Assert.notNull(status, "Status must not be null."); - Assert.notNull(e, "Exception must not be null."); + Preconditions.checkNotNull(status, "Status must not be null."); + Preconditions.checkNotNull(e, "Exception must not be null."); String msg = e.getMessage(); if (msg == null) { msg = ""; @@ -85,16 +86,16 @@ public class RestfulResult { public static RestfulResult of(final boolean isSuccess, final Supplier success, final Supplier error) { - Assert.notNull(success, "Success supplier must not be null."); - Assert.notNull(error, "Error supplier must not be null."); + Preconditions.checkNotNull(success, "Success supplier must not be null."); + Preconditions.checkNotNull(error, "Error supplier must not be null."); return isSuccess ? success.get() : error.get(); } public static RestfulResult of(final BooleanSupplier isSuccess, final Supplier success, final Supplier error) { - Assert.notNull(isSuccess, "Conditions for success must not be null."); - Assert.notNull(success, "Success supplier must not be null."); - Assert.notNull(error, "Error supplier must not be null."); + Preconditions.checkNotNull(isSuccess, "Conditions for success must not be null."); + Preconditions.checkNotNull(success, "Success supplier must not be null."); + Preconditions.checkNotNull(error, "Error supplier must not be null."); return isSuccess.getAsBoolean() ? success.get() : error.get(); }