调整代码,避免滥用 Nonnull 注解。
parent
c6d3cbeaf5
commit
15a8b23ae3
|
@ -16,9 +16,8 @@
|
|||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
|
@ -40,13 +39,47 @@ public final class EnumUtil {
|
|||
* @param ordinal 数据库中对应的数值
|
||||
* @return 枚举对象
|
||||
*/
|
||||
public static <E extends Enum<?>> E valueOf(@Nonnull 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.");
|
||||
E[] values = clazz.getEnumConstants();
|
||||
try {
|
||||
return values[ordinal];
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
|
||||
Assert.isTrue((ordinal >= 0 && ordinal < values.length),
|
||||
() -> new EnumConstantNotPresentException(clazz, Integer.toString(ordinal)));
|
||||
return values[ordinal];
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 ordinal 获取枚举实例
|
||||
*
|
||||
* @param <E> 枚举的类型
|
||||
* @param clazz 枚举的类型信息
|
||||
* @param ordinal 数据库中对应的数值
|
||||
* @param defaultValue 默认值
|
||||
* @return 枚举对象
|
||||
*/
|
||||
public static <E extends Enum<?>> E valueOf(Class<E> clazz, @Nullable Integer ordinal, E defaultValue) {
|
||||
if (null == ordinal) {
|
||||
return defaultValue;
|
||||
}
|
||||
return valueOf(clazz, ordinal);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 ordinal 获取枚举实例
|
||||
*
|
||||
* @param <E> 枚举的类型
|
||||
* @param clazz 枚举的类型信息
|
||||
* @param ordinal 数据库中对应的数值
|
||||
* @param defaultValue 默认值
|
||||
* @return 枚举对象
|
||||
*/
|
||||
public static <E extends Enum<?>> E getValueOrDefault(
|
||||
Class<E> clazz,
|
||||
@Nullable Integer ordinal,
|
||||
Supplier<E> defaultValue) {
|
||||
if (null == ordinal) {
|
||||
return defaultValue.get();
|
||||
}
|
||||
return valueOf(clazz, ordinal);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -57,14 +90,12 @@ public final class EnumUtil {
|
|||
* @param ordinal 数据库中对应的数值
|
||||
* @return 枚举对象
|
||||
*/
|
||||
public static <E extends Enum<?>> E getValueOrDefault(@Nonnull Class<E> clazz, @Nullable Integer ordinal) {
|
||||
E[] values = clazz.getEnumConstants();
|
||||
try {
|
||||
return Objects.nonNull(ordinal) ? values[ordinal] : values[0];
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
Objects.requireNonNull(ordinal);
|
||||
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
|
||||
}
|
||||
public static <E extends Enum<?>> E getValueOrDefault(Class<E> clazz, @Nullable Integer ordinal) {
|
||||
return getValueOrDefault(clazz, ordinal, () -> {
|
||||
Assert.notNull(clazz, "Clazz must not be null.");
|
||||
E[] values = clazz.getEnumConstants();
|
||||
return values[0];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -75,17 +106,13 @@ public final class EnumUtil {
|
|||
* @param ordinal 数据库中对应的数值
|
||||
* @return 枚举对象
|
||||
*/
|
||||
public static <E extends Enum<?>> E getValueNullable(@Nonnull Class<E> clazz, @Nullable Integer ordinal) {
|
||||
E[] values = clazz.getEnumConstants();
|
||||
try {
|
||||
return Objects.nonNull(ordinal) ? values[ordinal] : null;
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
Objects.requireNonNull(ordinal);
|
||||
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
|
||||
}
|
||||
public static <E extends Enum<?>> E getValueNullable(Class<E> clazz, @Nullable Integer ordinal) {
|
||||
return valueOf(clazz, ordinal, null);
|
||||
}
|
||||
|
||||
public static <E extends Enum<?>> Integer checkOrdinal(@Nonnull Class<E> clazz, @Nonnull Integer ordinal) {
|
||||
public static <E extends Enum<?>> Integer checkOrdinal(Class<E> clazz, Integer ordinal) {
|
||||
Assert.notNull(clazz, "Clazz must not be null.");
|
||||
Assert.notNull(ordinal, "Ordinal must not be null.");
|
||||
E[] values = clazz.getEnumConstants();
|
||||
if (ordinal >= 0 && ordinal < values.length) {
|
||||
return ordinal;
|
||||
|
@ -93,17 +120,22 @@ public final class EnumUtil {
|
|||
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
|
||||
}
|
||||
|
||||
public static <E extends Enum<?>> Integer checkOrdinalNullable(@Nonnull Class<E> clazz, @Nullable Integer ordinal) {
|
||||
if (ordinal == null) {
|
||||
return null;
|
||||
}
|
||||
return checkOrdinal(clazz, ordinal);
|
||||
public static <E extends Enum<?>> Integer checkOrdinalNullable(Class<E> clazz, @Nullable Integer ordinal) {
|
||||
return checkOrdinalOrDefault(clazz, ordinal, null);
|
||||
}
|
||||
|
||||
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(@Nonnull Class<E> clazz, @Nullable Integer ordinal) {
|
||||
if (ordinal == null) {
|
||||
return 0;
|
||||
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(Class<E> clazz, @Nullable Integer ordinal) {
|
||||
return checkOrdinalOrDefault(clazz, ordinal, 0);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(
|
||||
Class<E> clazz,
|
||||
@Nullable Integer ordinal,
|
||||
@Nullable Integer defaultValue) {
|
||||
if (ordinal != null) {
|
||||
return checkOrdinal(clazz, ordinal);
|
||||
}
|
||||
return checkOrdinal(clazz, ordinal);
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ import java.util.Map;
|
|||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import javax.annotation.CheckForNull;
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
|
@ -29,12 +28,11 @@ import javax.annotation.Nonnull;
|
|||
*/
|
||||
public abstract class Enumeration<T extends Enumeration<T>> implements Comparable<T> {
|
||||
protected final int id;
|
||||
@Nonnull
|
||||
protected final String name;
|
||||
|
||||
protected Enumeration(final int id, @Nonnull final String name) {
|
||||
protected Enumeration(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.name = Objects.requireNonNull(name);
|
||||
}
|
||||
|
||||
public final int getId() {
|
||||
|
@ -73,25 +71,25 @@ public abstract class Enumeration<T extends Enumeration<T>> implements Comparabl
|
|||
}
|
||||
|
||||
protected static final class ValueSet<T extends Enumeration<T>> {
|
||||
private final Map<Integer, T> values = new ConcurrentHashMap<>();
|
||||
|
||||
private final Map<Integer, @Nonnull T> values = new ConcurrentHashMap<>();
|
||||
|
||||
@SafeVarargs
|
||||
public ValueSet(T... values) {
|
||||
for (T value : values) {
|
||||
put(Objects.requireNonNull(value));
|
||||
put(value);
|
||||
}
|
||||
}
|
||||
|
||||
private void put(@Nonnull final T value) {
|
||||
this.values.put(value.getId(), value);
|
||||
private void put(final T value) {
|
||||
this.values.put(value.getId(), Objects.requireNonNull(value, "Value must not be null."));
|
||||
}
|
||||
|
||||
@CheckForNull
|
||||
public T get(final int id) {
|
||||
public T get(int id) {
|
||||
Assert.isTrue(this.values.containsKey(id), "%s 对应的值不存在", id);
|
||||
return this.values.get(id);
|
||||
}
|
||||
|
||||
public Collection<T> getValues() {
|
||||
public Collection<@Nonnull T> getValues() {
|
||||
return this.values.values();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ package xyz.zhouxy.plusone.commons.util;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
|
||||
/**
|
||||
* 返回分页查询的结果
|
||||
|
@ -30,27 +30,25 @@ import javax.annotation.Nonnull;
|
|||
*/
|
||||
public class PageDTO<T> {
|
||||
|
||||
@Nonnull
|
||||
private final Long total;
|
||||
@Nonnull
|
||||
private final long total;
|
||||
|
||||
private final List<T> content;
|
||||
|
||||
private PageDTO(@Nonnull List<T> content, @Nonnull Long total) {
|
||||
private PageDTO(List<T> content, long total) {
|
||||
Assert.isNotEmpty(content, "Content must not be null.");
|
||||
this.content = content;
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static <T> PageDTO<T> of(@Nonnull List<T> content, @Nonnull Long total) {
|
||||
@StaticFactoryMethod(PageDTO.class)
|
||||
public static <T> PageDTO<T> of(List<T> content, long total) {
|
||||
return new PageDTO<>(content, total);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Long getTotal() {
|
||||
public long getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public List<T> getContent() {
|
||||
return content;
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
@ -24,31 +26,34 @@ import java.util.regex.Pattern;
|
|||
|
||||
public class RegexUtil {
|
||||
|
||||
private static final Map<String, Pattern> PATTERN_CACHE = new ConcurrentHashMap<>();
|
||||
private static final Map<String, @Nonnull Pattern> PATTERN_CACHE = new ConcurrentHashMap<>();
|
||||
|
||||
@Nonnull
|
||||
public static Pattern getPattern(final String regex) {
|
||||
Objects.requireNonNull(regex);
|
||||
Pattern pattern = PATTERN_CACHE.get(regex);
|
||||
if (pattern == null) {
|
||||
pattern = Pattern.compile(regex);
|
||||
PATTERN_CACHE.put(regex, pattern);
|
||||
if (PATTERN_CACHE.containsKey(regex)) {
|
||||
return PATTERN_CACHE.get(regex);
|
||||
}
|
||||
return Objects.requireNonNull(pattern);
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
if (pattern == null) {
|
||||
throw new IllegalArgumentException("Regex must not be null.");
|
||||
}
|
||||
PATTERN_CACHE.put(regex, pattern);
|
||||
return pattern;
|
||||
}
|
||||
|
||||
public static boolean matches(CharSequence input, @Nonnull String regex) {
|
||||
public static boolean matches(@Nullable CharSequence input, String regex) {
|
||||
return matches(input, getPattern(regex));
|
||||
}
|
||||
|
||||
public static boolean matches(CharSequence input, @Nonnull Pattern regex) {
|
||||
return regex.matcher(input).matches();
|
||||
public static boolean matches(@Nullable CharSequence input, Pattern pattern) {
|
||||
Assert.notNull(pattern, "Pattern must not be null.");
|
||||
return pattern.matcher(input).matches();
|
||||
}
|
||||
|
||||
public static boolean matchesOr(CharSequence input, String... regexes) {
|
||||
public static boolean matchesOr(@Nullable CharSequence input, String... regexes) {
|
||||
boolean isMatched;
|
||||
for (String regex : regexes) {
|
||||
isMatched = matches(input, Objects.requireNonNull(regex));
|
||||
isMatched = matches(input, regex);
|
||||
if (isMatched) {
|
||||
return true;
|
||||
}
|
||||
|
@ -56,10 +61,10 @@ public class RegexUtil {
|
|||
return false;
|
||||
}
|
||||
|
||||
public static boolean matchesOr(CharSequence input, Pattern... patterns) {
|
||||
public static boolean matchesOr(@Nullable CharSequence input, Pattern... patterns) {
|
||||
boolean isMatched;
|
||||
for (Pattern pattern : patterns) {
|
||||
isMatched = matches(input, Objects.requireNonNull(pattern));
|
||||
isMatched = matches(input, pattern);
|
||||
if (isMatched) {
|
||||
return true;
|
||||
}
|
||||
|
@ -67,10 +72,10 @@ public class RegexUtil {
|
|||
return false;
|
||||
}
|
||||
|
||||
public static boolean matchesAnd(CharSequence input, String... regexes) {
|
||||
public static boolean matchesAnd(@Nullable CharSequence input, String... regexes) {
|
||||
boolean isMatched;
|
||||
for (String regex : regexes) {
|
||||
isMatched = matches(input, Objects.requireNonNull(regex));
|
||||
isMatched = matches(input, regex);
|
||||
if (!isMatched) {
|
||||
return false;
|
||||
}
|
||||
|
@ -78,10 +83,10 @@ public class RegexUtil {
|
|||
return true;
|
||||
}
|
||||
|
||||
public static boolean matchesAnd(CharSequence input, Pattern... patterns) {
|
||||
public static boolean matchesAnd(@Nullable CharSequence input, Pattern... patterns) {
|
||||
boolean isMatched;
|
||||
for (Pattern pattern : patterns) {
|
||||
isMatched = matches(input, Objects.requireNonNull(pattern));
|
||||
isMatched = matches(input, pattern);
|
||||
if (!isMatched) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ import org.slf4j.LoggerFactory;
|
|||
|
||||
import xyz.zhouxy.plusone.commons.util.Enumeration;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Collection;
|
||||
|
||||
class EnumerationTests {
|
||||
|
@ -31,7 +30,7 @@ class EnumerationTests {
|
|||
|
||||
final class EntityStatus extends Enumeration<EntityStatus> {
|
||||
|
||||
private EntityStatus(int value, @Nonnull String name) {
|
||||
private EntityStatus(int value, String name) {
|
||||
super(value, name);
|
||||
}
|
||||
|
||||
|
@ -52,7 +51,7 @@ final class EntityStatus extends Enumeration<EntityStatus> {
|
|||
}
|
||||
|
||||
final class Result extends Enumeration<Result> {
|
||||
private Result(int id, @Nonnull String name) {
|
||||
private Result(int id, String name) {
|
||||
super(id, name);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue