使用 AssertTools 替换 Preconditions。

pull/1/head
ZhouXY108 2024-12-16 09:40:18 +08:00
parent 1e4306005e
commit 488aaad452
4 changed files with 17 additions and 21 deletions

View File

@ -18,9 +18,8 @@ package xyz.zhouxy.plusone.commons.model.dto;
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;
import xyz.zhouxy.plusone.commons.util.AssertTools;
/** /**
* *
@ -37,7 +36,7 @@ public class PageResult<T> {
private final List<T> content; private final List<T> content;
private PageResult(List<T> content, long total) { private PageResult(List<T> content, long total) {
Preconditions.checkNotNull(content, "Content must not be null."); AssertTools.checkNotNull(content, "Content must not be null.");
this.content = content; this.content = content;
this.total = total; this.total = total;
} }

View File

@ -21,12 +21,13 @@ import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import xyz.zhouxy.plusone.commons.annotation.Virtual; import xyz.zhouxy.plusone.commons.annotation.Virtual;
import xyz.zhouxy.plusone.commons.util.AssertTools;
import xyz.zhouxy.plusone.commons.util.StringTools; import xyz.zhouxy.plusone.commons.util.StringTools;
/** /**
@ -52,11 +53,11 @@ public class PagingAndSortingQueryParams {
private final Map<String, String> sortableProperties; private final Map<String, String> sortableProperties;
public PagingAndSortingQueryParams(Map<String, String> sortableProperties) { public PagingAndSortingQueryParams(@Nonnull Map<String, String> sortableProperties) {
Preconditions.checkArgument(sortableProperties != null && !sortableProperties.isEmpty(), AssertTools.checkArgument(sortableProperties != null && !sortableProperties.isEmpty(),
"Sortable properties can not be empty."); "Sortable properties can not be empty.");
sortableProperties.forEach((k, v) -> 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.")); "Property name must not be blank."));
this.sortableProperties = ImmutableMap.copyOf(sortableProperties); this.sortableProperties = ImmutableMap.copyOf(sortableProperties);
} }
@ -101,12 +102,12 @@ public class PagingAndSortingQueryParams {
} }
private SortableProperty generateSortableProperty(String orderByStr) { private SortableProperty generateSortableProperty(String orderByStr) {
Preconditions.checkArgument(PagingAndSortingQueryParams.sortStrPattern.matcher(orderByStr).matches()); AssertTools.checkArgument(PagingAndSortingQueryParams.sortStrPattern.matcher(orderByStr).matches());
String[] propertyNameAndOrderType = orderByStr.split("-"); String[] propertyNameAndOrderType = orderByStr.split("-");
Preconditions.checkArgument(propertyNameAndOrderType.length == 2); AssertTools.checkArgument(propertyNameAndOrderType.length == 2);
String propertyName = propertyNameAndOrderType[0]; 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."); "The property name must be in the set of sortable properties.");
String columnName = sortableProperties.get(propertyName); String columnName = sortableProperties.get(propertyName);
String orderType = propertyNameAndOrderType[1]; String orderType = propertyNameAndOrderType[1];
@ -123,7 +124,7 @@ public class PagingAndSortingQueryParams {
SortableProperty(String propertyName, String columnName, String orderType) { SortableProperty(String propertyName, String columnName, String orderType) {
this.propertyName = propertyName; this.propertyName = propertyName;
this.columnName = columnName; this.columnName = columnName;
Preconditions.checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType)); AssertTools.checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType));
this.orderType = orderType.toUpperCase(); this.orderType = orderType.toUpperCase();
this.sqlSnippet = this.propertyName + " " + this.orderType; this.sqlSnippet = this.propertyName + " " + this.orderType;

View File

@ -20,8 +20,6 @@ import java.util.function.Supplier;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.google.common.base.Preconditions;
/** /**
* *
* *
@ -44,7 +42,7 @@ public final class EnumTools {
*/ */
@Deprecated @Deprecated
public static <E extends Enum<?>> E valueOf(Class<E> clazz, int ordinal) { // NOSONAR 该方法弃用,但不删掉 public static <E extends Enum<?>> E valueOf(Class<E> clazz, int ordinal) { // NOSONAR 该方法弃用,但不删掉
Preconditions.checkNotNull(clazz, "Clazz must not be null."); AssertTools.checkNotNull(clazz, "Clazz must not be null.");
E[] values = clazz.getEnumConstants(); E[] values = clazz.getEnumConstants();
AssertTools.checkCondition((ordinal >= 0 && ordinal < values.length), AssertTools.checkCondition((ordinal >= 0 && ordinal < values.length),
() -> new EnumConstantNotPresentException(clazz, Integer.toString(ordinal))); () -> new EnumConstantNotPresentException(clazz, Integer.toString(ordinal)));
@ -102,7 +100,7 @@ public final class EnumTools {
@Deprecated @Deprecated
public static <E extends Enum<?>> E getValueOrDefault(Class<E> clazz, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉 public static <E extends Enum<?>> E getValueOrDefault(Class<E> clazz, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉
return getValueOrDefault(clazz, ordinal, () -> { return getValueOrDefault(clazz, ordinal, () -> {
Preconditions.checkNotNull(clazz, "Clazz must not be null."); AssertTools.checkNotNull(clazz, "Clazz must not be null.");
E[] values = clazz.getEnumConstants(); E[] values = clazz.getEnumConstants();
return values[0]; return values[0];
}); });
@ -123,8 +121,8 @@ public final class EnumTools {
} }
public static <E extends Enum<?>> Integer checkOrdinal(Class<E> clazz, Integer ordinal) { public static <E extends Enum<?>> Integer checkOrdinal(Class<E> clazz, Integer ordinal) {
Preconditions.checkNotNull(clazz, "Clazz must not be null."); AssertTools.checkNotNull(clazz, "Clazz must not be null.");
Preconditions.checkNotNull(ordinal, "Ordinal must not be null."); AssertTools.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

@ -24,8 +24,6 @@ import java.util.Objects;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.google.common.base.Preconditions;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
/** /**
@ -43,7 +41,7 @@ public abstract class Enumeration<T extends Enumeration<T>> // NOSONAR 暂不移
protected final String name; protected final String name;
protected Enumeration(final int id, 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.id = id;
this.name = name; this.name = name;
} }
@ -98,7 +96,7 @@ public abstract class Enumeration<T extends Enumeration<T>> // NOSONAR 暂不移
} }
public T get(int id) { 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); return this.valueMap.get(id);
} }