diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/EnumUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/EnumUtil.java index 30ad4ab..6e44dcb 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/EnumUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/EnumUtil.java @@ -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 valueOf(@Nonnull Class clazz, int ordinal) { + public static > E valueOf(Class 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 枚举的类型 + * @param clazz 枚举的类型信息 + * @param ordinal 数据库中对应的数值 + * @param defaultValue 默认值 + * @return 枚举对象 + */ + public static > E valueOf(Class clazz, @Nullable Integer ordinal, E defaultValue) { + if (null == ordinal) { + return defaultValue; } + return valueOf(clazz, ordinal); + } + + /** + * 通过 ordinal 获取枚举实例 + * + * @param 枚举的类型 + * @param clazz 枚举的类型信息 + * @param ordinal 数据库中对应的数值 + * @param defaultValue 默认值 + * @return 枚举对象 + */ + public static > E getValueOrDefault( + Class clazz, + @Nullable Integer ordinal, + Supplier 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 getValueOrDefault(@Nonnull Class 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 getValueOrDefault(Class 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 getValueNullable(@Nonnull Class 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 getValueNullable(Class clazz, @Nullable Integer ordinal) { + return valueOf(clazz, ordinal, null); } - public static > Integer checkOrdinal(@Nonnull Class clazz, @Nonnull Integer ordinal) { + public static > Integer checkOrdinal(Class 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 > Integer checkOrdinalNullable(@Nonnull Class clazz, @Nullable Integer ordinal) { - if (ordinal == null) { - return null; - } - return checkOrdinal(clazz, ordinal); + public static > Integer checkOrdinalNullable(Class clazz, @Nullable Integer ordinal) { + return checkOrdinalOrDefault(clazz, ordinal, null); } - public static > Integer checkOrdinalOrDefault(@Nonnull Class clazz, @Nullable Integer ordinal) { - if (ordinal == null) { - return 0; + public static > Integer checkOrdinalOrDefault(Class clazz, @Nullable Integer ordinal) { + return checkOrdinalOrDefault(clazz, ordinal, 0); + } + + @Nullable + public static > Integer checkOrdinalOrDefault( + Class clazz, + @Nullable Integer ordinal, + @Nullable Integer defaultValue) { + if (ordinal != null) { + return checkOrdinal(clazz, ordinal); } - return checkOrdinal(clazz, ordinal); + return defaultValue; } } 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 f2dca80..90edf0a 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java @@ -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> implements Comparable { 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> implements Comparabl } protected static final class ValueSet> { - private final Map values = new ConcurrentHashMap<>(); - + private final Map 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 getValues() { + public Collection<@Nonnull T> getValues() { return this.values.values(); } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/PageDTO.java b/src/main/java/xyz/zhouxy/plusone/commons/util/PageDTO.java index e737013..963e028 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/PageDTO.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/PageDTO.java @@ -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 { - @Nonnull - private final Long total; - @Nonnull + private final long total; + private final List content; - private PageDTO(@Nonnull List content, @Nonnull Long total) { + private PageDTO(List content, long total) { + Assert.isNotEmpty(content, "Content must not be null."); this.content = content; this.total = total; } - @Nonnull - public static PageDTO of(@Nonnull List content, @Nonnull Long total) { + @StaticFactoryMethod(PageDTO.class) + public static PageDTO of(List content, long total) { return new PageDTO<>(content, total); } - @Nonnull - public Long getTotal() { + public long getTotal() { return total; } - @Nonnull public List getContent() { return content; } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java index 491ee86..66446fd 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java @@ -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 PATTERN_CACHE = new ConcurrentHashMap<>(); + private static final Map 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; } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java b/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java index 8fa1b53..619c4d8 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java @@ -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 { - 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 { } final class Result extends Enumeration { - private Result(int id, @Nonnull String name) { + private Result(int id, String name) { super(id, name); }