From 488aaad452f5f955e1f0eadfd8e302546e88e1f6 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 16 Dec 2024 09:40:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20AssertTools=20=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=20Preconditions=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/model/dto/PageResult.java | 5 ++--- .../model/dto/PagingAndSortingQueryParams.java | 17 +++++++++-------- .../zhouxy/plusone/commons/util/EnumTools.java | 10 ++++------ .../plusone/commons/util/Enumeration.java | 6 ++---- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PageResult.java b/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PageResult.java index 3467182..7f913a8 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PageResult.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PageResult.java @@ -18,9 +18,8 @@ package xyz.zhouxy.plusone.commons.model.dto; import java.util.List; -import com.google.common.base.Preconditions; - import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; +import xyz.zhouxy.plusone.commons.util.AssertTools; /** * 返回分页查询的结果 @@ -37,7 +36,7 @@ public class PageResult { private final List content; private PageResult(List content, long total) { - Preconditions.checkNotNull(content, "Content must not be null."); + AssertTools.checkNotNull(content, "Content must not be null."); this.content = content; this.total = total; } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PagingAndSortingQueryParams.java b/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PagingAndSortingQueryParams.java index 64f8db2..9c04fd3 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PagingAndSortingQueryParams.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PagingAndSortingQueryParams.java @@ -21,12 +21,13 @@ import java.util.Map; import java.util.regex.Pattern; import java.util.stream.Collectors; +import javax.annotation.Nonnull; import javax.annotation.Nullable; -import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; import xyz.zhouxy.plusone.commons.annotation.Virtual; +import xyz.zhouxy.plusone.commons.util.AssertTools; import xyz.zhouxy.plusone.commons.util.StringTools; /** @@ -52,11 +53,11 @@ public class PagingAndSortingQueryParams { private final Map sortableProperties; - public PagingAndSortingQueryParams(Map sortableProperties) { - Preconditions.checkArgument(sortableProperties != null && !sortableProperties.isEmpty(), + public PagingAndSortingQueryParams(@Nonnull Map sortableProperties) { + AssertTools.checkArgument(sortableProperties != null && !sortableProperties.isEmpty(), "Sortable properties can not be empty."); sortableProperties.forEach((k, v) -> - Preconditions.checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v), + AssertTools.checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v), "Property name must not be blank.")); this.sortableProperties = ImmutableMap.copyOf(sortableProperties); } @@ -101,12 +102,12 @@ public class PagingAndSortingQueryParams { } private SortableProperty generateSortableProperty(String orderByStr) { - Preconditions.checkArgument(PagingAndSortingQueryParams.sortStrPattern.matcher(orderByStr).matches()); + AssertTools.checkArgument(PagingAndSortingQueryParams.sortStrPattern.matcher(orderByStr).matches()); String[] propertyNameAndOrderType = orderByStr.split("-"); - Preconditions.checkArgument(propertyNameAndOrderType.length == 2); + AssertTools.checkArgument(propertyNameAndOrderType.length == 2); String propertyName = propertyNameAndOrderType[0]; - Preconditions.checkArgument(sortableProperties.containsKey(propertyName), + AssertTools.checkArgument(sortableProperties.containsKey(propertyName), "The property name must be in the set of sortable properties."); String columnName = sortableProperties.get(propertyName); String orderType = propertyNameAndOrderType[1]; @@ -123,7 +124,7 @@ public class PagingAndSortingQueryParams { SortableProperty(String propertyName, String columnName, String orderType) { this.propertyName = propertyName; this.columnName = columnName; - Preconditions.checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType)); + AssertTools.checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType)); this.orderType = orderType.toUpperCase(); this.sqlSnippet = this.propertyName + " " + this.orderType; diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/EnumTools.java b/src/main/java/xyz/zhouxy/plusone/commons/util/EnumTools.java index 428b29c..8f2e4d6 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/EnumTools.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/EnumTools.java @@ -20,8 +20,6 @@ import java.util.function.Supplier; import javax.annotation.Nullable; -import com.google.common.base.Preconditions; - /** * 枚举工具类 * @@ -44,7 +42,7 @@ public final class EnumTools { */ @Deprecated public static > E valueOf(Class clazz, int ordinal) { // NOSONAR 该方法弃用,但不删掉 - Preconditions.checkNotNull(clazz, "Clazz must not be null."); + AssertTools.checkNotNull(clazz, "Clazz must not be null."); E[] values = clazz.getEnumConstants(); AssertTools.checkCondition((ordinal >= 0 && ordinal < values.length), () -> new EnumConstantNotPresentException(clazz, Integer.toString(ordinal))); @@ -102,7 +100,7 @@ public final class EnumTools { @Deprecated public static > E getValueOrDefault(Class clazz, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉 return getValueOrDefault(clazz, ordinal, () -> { - Preconditions.checkNotNull(clazz, "Clazz must not be null."); + AssertTools.checkNotNull(clazz, "Clazz must not be null."); E[] values = clazz.getEnumConstants(); return values[0]; }); @@ -123,8 +121,8 @@ public final class EnumTools { } public static > Integer checkOrdinal(Class clazz, Integer ordinal) { - Preconditions.checkNotNull(clazz, "Clazz must not be null."); - Preconditions.checkNotNull(ordinal, "Ordinal must not be null."); + AssertTools.checkNotNull(clazz, "Clazz must not be null."); + AssertTools.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 a6bfa8f..aee9e0b 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java @@ -24,8 +24,6 @@ import java.util.Objects; import java.util.function.Function; import java.util.stream.Collectors; -import com.google.common.base.Preconditions; - import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; /** @@ -43,7 +41,7 @@ public abstract class Enumeration> // NOSONAR 暂不移 protected final String name; protected Enumeration(final int id, final String name) { - Preconditions.checkArgument(StringTools.isNotBlank(name), "Name of enumeration must has text."); + AssertTools.checkArgument(StringTools.isNotBlank(name), "Name of enumeration must has text."); this.id = id; this.name = name; } @@ -98,7 +96,7 @@ public abstract class Enumeration> // NOSONAR 暂不移 } public T get(int id) { - Preconditions.checkArgument(this.valueMap.containsKey(id), "[%s] 对应的值不存在", id); + AssertTools.checkArgument(this.valueMap.containsKey(id), "[%s] 对应的值不存在", id); return this.valueMap.get(id); }