perf: 优化 JSR305 注解的使用

This commit is contained in:
zhouxy108 2025-04-01 17:51:35 +08:00
parent c0be49dd36
commit 769feaf72e
7 changed files with 33 additions and 15 deletions

View File

@ -21,6 +21,8 @@ import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.regex.Matcher;
import javax.annotation.Nullable;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.Immutable;
@ -207,7 +209,7 @@ public class Chinese2ndGenIDCardNumber
}
@Override
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
return super.equals(obj);
}
}

View File

@ -22,6 +22,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import xyz.zhouxy.plusone.commons.annotation.ReaderMethod;
import xyz.zhouxy.plusone.commons.util.AssertTools;
@ -40,17 +41,16 @@ public abstract class ValidatableStringRecord<T extends ValidatableStringRecord<
private final Matcher matcher;
protected ValidatableStringRecord(@Nonnull String value, @Nonnull Pattern pattern) {
protected ValidatableStringRecord(String value, Pattern pattern) {
this(value, pattern, "Invalid value");
}
protected ValidatableStringRecord(@Nonnull String value, @Nonnull Pattern pattern,
@Nonnull Supplier<String> errorMessageSupplier) {
protected ValidatableStringRecord(String value, Pattern pattern,
Supplier<String> errorMessageSupplier) {
this(value, pattern, errorMessageSupplier.get());
}
protected ValidatableStringRecord(@Nonnull String value, @Nonnull Pattern pattern,
@Nonnull String errorMessage) {
protected ValidatableStringRecord(String value, Pattern pattern, String errorMessage) {
AssertTools.checkArgument(Objects.nonNull(value), "The value cannot be null.");
AssertTools.checkArgument(Objects.nonNull(pattern), "The pattern cannot be null.");
this.matcher = pattern.matcher(value);
@ -69,7 +69,7 @@ public abstract class ValidatableStringRecord<T extends ValidatableStringRecord<
}
@Override
public int compareTo(T o) {
public int compareTo(@SuppressWarnings("null") T o) {
return this.value.compareTo(o.value());
}
@ -79,7 +79,7 @@ public abstract class ValidatableStringRecord<T extends ValidatableStringRecord<
}
@Override
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
if (this == obj)
return true;
if (obj == null)

View File

@ -19,6 +19,8 @@ package xyz.zhouxy.plusone.commons.model.dto;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
@ -36,13 +38,13 @@ public class PageResult<T> {
private final List<T> content;
private PageResult(List<T> content, long total) {
private PageResult(@Nullable final List<T> content, final long total) {
this.content = CollectionTools.nullToEmptyList(content);
this.total = total;
}
@StaticFactoryMethod(PageResult.class)
public static <T> PageResult<T> of(List<T> content, long total) {
public static <T> PageResult<T> of(@Nullable final List<T> content, final long total) {
return new PageResult<>(content, total);
}

View File

@ -21,7 +21,6 @@ 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.collect.ImmutableMap;
@ -55,7 +54,7 @@ public class PagingAndSortingQueryParams {
private final Map<String, String> sortableProperties;
public PagingAndSortingQueryParams(@Nonnull Map<String, String> sortableProperties) {
public PagingAndSortingQueryParams(Map<String, String> sortableProperties) {
AssertTools.checkArgument(CollectionTools.isNotEmpty(sortableProperties),
"Sortable properties can not be empty.");
sortableProperties.forEach((k, v) ->
@ -66,7 +65,12 @@ public class PagingAndSortingQueryParams {
// Setters
public final void setOrderBy(@Nullable List<String> orderBy) {
/**
* 设置排序规则
*
* @param orderBy 排序规则不能为空
*/
public final void setOrderBy(List<String> orderBy) {
this.orderBy = orderBy;
}
@ -83,7 +87,10 @@ public class PagingAndSortingQueryParams {
public final PagingParams buildPagingParams() {
final int sizeValue = this.size != null ? this.size : defaultSizeInternal();
final long pageNumValue = this.pageNum != null ? this.pageNum : 1L;
final List<SortableProperty> propertiesToSort = this.orderBy.stream().map(this::generateSortableProperty)
AssertTools.checkArgument(CollectionTools.isNotEmpty(this.orderBy),
"The 'orderBy' cannot be empty");
final List<SortableProperty> propertiesToSort = this.orderBy.stream()
.map(this::generateSortableProperty)
.collect(Collectors.toList());
return new PagingParams(sizeValue, pageNumValue, propertiesToSort);
}
@ -104,6 +111,7 @@ public class PagingAndSortingQueryParams {
}
private SortableProperty generateSortableProperty(String orderByStr) {
AssertTools.checkArgument(StringTools.isNotBlank(orderByStr));
AssertTools.checkArgument(RegexTools.matches(orderByStr, SORT_STR_PATTERN));
String[] propertyNameAndOrderType = orderByStr.split("-");
AssertTools.checkArgument(propertyNameAndOrderType.length == 2);

View File

@ -76,7 +76,7 @@ public class UnifiedResponse<T> {
this.code, this.message, transValue(this.data));
}
private static String transValue(Object value) {
private static String transValue(@Nullable Object value) {
if (value == null) {
return null;
}

View File

@ -64,4 +64,7 @@
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
@ParametersAreNonnullByDefault
package xyz.zhouxy.plusone.commons.model.dto;
import javax.annotation.ParametersAreNonnullByDefault;

View File

@ -22,4 +22,7 @@
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
@ParametersAreNonnullByDefault
package xyz.zhouxy.plusone.commons.model;
import javax.annotation.ParametersAreNonnullByDefault;