调整代码,避免滥用 Nonnull 注解。

feature/net-util
ZhouXY108 2023-04-21 01:47:18 +08:00
parent c6d3cbeaf5
commit 15a8b23ae3
5 changed files with 108 additions and 76 deletions

View File

@ -16,9 +16,8 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import java.util.Objects; import java.util.function.Supplier;
import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
/** /**
@ -40,13 +39,47 @@ public final class EnumUtil {
* @param ordinal * @param ordinal
* @return * @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(); E[] values = clazz.getEnumConstants();
try { Assert.isTrue((ordinal >= 0 && ordinal < values.length),
return values[ordinal]; () -> new EnumConstantNotPresentException(clazz, Integer.toString(ordinal)));
} catch (IndexOutOfBoundsException e) { return values[ordinal];
throw new EnumConstantNotPresentException(clazz, Integer.toString(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 * @param ordinal
* @return * @return
*/ */
public static <E extends Enum<?>> E getValueOrDefault(@Nonnull Class<E> clazz, @Nullable Integer ordinal) { public static <E extends Enum<?>> E getValueOrDefault(Class<E> clazz, @Nullable Integer ordinal) {
E[] values = clazz.getEnumConstants(); return getValueOrDefault(clazz, ordinal, () -> {
try { Assert.notNull(clazz, "Clazz must not be null.");
return Objects.nonNull(ordinal) ? values[ordinal] : values[0]; E[] values = clazz.getEnumConstants();
} catch (IndexOutOfBoundsException e) { return values[0];
Objects.requireNonNull(ordinal); });
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
}
} }
/** /**
@ -75,17 +106,13 @@ public final class EnumUtil {
* @param ordinal * @param ordinal
* @return * @return
*/ */
public static <E extends Enum<?>> E getValueNullable(@Nonnull Class<E> clazz, @Nullable Integer ordinal) { public static <E extends Enum<?>> E getValueNullable(Class<E> clazz, @Nullable Integer ordinal) {
E[] values = clazz.getEnumConstants(); return valueOf(clazz, ordinal, null);
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<?>> 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(); E[] values = clazz.getEnumConstants();
if (ordinal >= 0 && ordinal < values.length) { if (ordinal >= 0 && ordinal < values.length) {
return ordinal; return ordinal;
@ -93,17 +120,22 @@ public final class EnumUtil {
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal)); throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
} }
public static <E extends Enum<?>> Integer checkOrdinalNullable(@Nonnull Class<E> clazz, @Nullable Integer ordinal) { public static <E extends Enum<?>> Integer checkOrdinalNullable(Class<E> clazz, @Nullable Integer ordinal) {
if (ordinal == null) { return checkOrdinalOrDefault(clazz, ordinal, null);
return null;
}
return checkOrdinal(clazz, ordinal);
} }
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(@Nonnull Class<E> clazz, @Nullable Integer ordinal) { public static <E extends Enum<?>> Integer checkOrdinalOrDefault(Class<E> clazz, @Nullable Integer ordinal) {
if (ordinal == null) { return checkOrdinalOrDefault(clazz, ordinal, 0);
return 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;
} }
} }

View File

@ -21,7 +21,6 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
/** /**
@ -29,12 +28,11 @@ import javax.annotation.Nonnull;
*/ */
public abstract class Enumeration<T extends Enumeration<T>> implements Comparable<T> { public abstract class Enumeration<T extends Enumeration<T>> implements Comparable<T> {
protected final int id; protected final int id;
@Nonnull
protected final String name; 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.id = id;
this.name = name; this.name = Objects.requireNonNull(name);
} }
public final int getId() { 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>> { 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 @SafeVarargs
public ValueSet(T... values) { public ValueSet(T... values) {
for (T value : values) { for (T value : values) {
put(Objects.requireNonNull(value)); put(value);
} }
} }
private void put(@Nonnull final T value) { private void put(final T value) {
this.values.put(value.getId(), value); this.values.put(value.getId(), Objects.requireNonNull(value, "Value must not be null."));
} }
@CheckForNull public T get(int id) {
public T get(final int id) { Assert.isTrue(this.values.containsKey(id), "%s 对应的值不存在", id);
return this.values.get(id); return this.values.get(id);
} }
public Collection<T> getValues() { public Collection<@Nonnull T> getValues() {
return this.values.values(); return this.values.values();
} }
} }

View File

@ -18,7 +18,7 @@ package xyz.zhouxy.plusone.commons.util;
import java.util.List; 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> { public class PageDTO<T> {
@Nonnull private final long total;
private final Long total;
@Nonnull
private final List<T> content; 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.content = content;
this.total = total; this.total = total;
} }
@Nonnull @StaticFactoryMethod(PageDTO.class)
public static <T> PageDTO<T> of(@Nonnull List<T> content, @Nonnull Long total) { public static <T> PageDTO<T> of(List<T> content, long total) {
return new PageDTO<>(content, total); return new PageDTO<>(content, total);
} }
@Nonnull public long getTotal() {
public Long getTotal() {
return total; return total;
} }
@Nonnull
public List<T> getContent() { public List<T> getContent() {
return content; return content;
} }

View File

@ -17,6 +17,8 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -24,31 +26,34 @@ import java.util.regex.Pattern;
public class RegexUtil { 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) { public static Pattern getPattern(final String regex) {
Objects.requireNonNull(regex); Objects.requireNonNull(regex);
Pattern pattern = PATTERN_CACHE.get(regex); if (PATTERN_CACHE.containsKey(regex)) {
if (pattern == null) { return PATTERN_CACHE.get(regex);
pattern = Pattern.compile(regex);
PATTERN_CACHE.put(regex, pattern);
} }
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)); return matches(input, getPattern(regex));
} }
public static boolean matches(CharSequence input, @Nonnull Pattern regex) { public static boolean matches(@Nullable CharSequence input, Pattern pattern) {
return regex.matcher(input).matches(); 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; boolean isMatched;
for (String regex : regexes) { for (String regex : regexes) {
isMatched = matches(input, Objects.requireNonNull(regex)); isMatched = matches(input, regex);
if (isMatched) { if (isMatched) {
return true; return true;
} }
@ -56,10 +61,10 @@ public class RegexUtil {
return false; return false;
} }
public static boolean matchesOr(CharSequence input, Pattern... patterns) { public static boolean matchesOr(@Nullable CharSequence input, Pattern... patterns) {
boolean isMatched; boolean isMatched;
for (Pattern pattern : patterns) { for (Pattern pattern : patterns) {
isMatched = matches(input, Objects.requireNonNull(pattern)); isMatched = matches(input, pattern);
if (isMatched) { if (isMatched) {
return true; return true;
} }
@ -67,10 +72,10 @@ public class RegexUtil {
return false; return false;
} }
public static boolean matchesAnd(CharSequence input, String... regexes) { public static boolean matchesAnd(@Nullable CharSequence input, String... regexes) {
boolean isMatched; boolean isMatched;
for (String regex : regexes) { for (String regex : regexes) {
isMatched = matches(input, Objects.requireNonNull(regex)); isMatched = matches(input, regex);
if (!isMatched) { if (!isMatched) {
return false; return false;
} }
@ -78,10 +83,10 @@ public class RegexUtil {
return true; return true;
} }
public static boolean matchesAnd(CharSequence input, Pattern... patterns) { public static boolean matchesAnd(@Nullable CharSequence input, Pattern... patterns) {
boolean isMatched; boolean isMatched;
for (Pattern pattern : patterns) { for (Pattern pattern : patterns) {
isMatched = matches(input, Objects.requireNonNull(pattern)); isMatched = matches(input, pattern);
if (!isMatched) { if (!isMatched) {
return false; return false;
} }

View File

@ -9,7 +9,6 @@ import org.slf4j.LoggerFactory;
import xyz.zhouxy.plusone.commons.util.Enumeration; import xyz.zhouxy.plusone.commons.util.Enumeration;
import javax.annotation.Nonnull;
import java.util.Collection; import java.util.Collection;
class EnumerationTests { class EnumerationTests {
@ -31,7 +30,7 @@ class EnumerationTests {
final class EntityStatus extends Enumeration<EntityStatus> { final class EntityStatus extends Enumeration<EntityStatus> {
private EntityStatus(int value, @Nonnull String name) { private EntityStatus(int value, String name) {
super(value, name); super(value, name);
} }
@ -52,7 +51,7 @@ final class EntityStatus extends Enumeration<EntityStatus> {
} }
final class Result extends Enumeration<Result> { final class Result extends Enumeration<Result> {
private Result(int id, @Nonnull String name) { private Result(int id, String name) {
super(id, name); super(id, name);
} }