diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/Arrays2.java b/src/main/java/xyz/zhouxy/plusone/commons/util/Arrays2.java index 95eb458..d7dd4ee 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/Arrays2.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Arrays2.java @@ -16,42 +16,64 @@ package xyz.zhouxy.plusone.commons.util; +import java.util.Objects; + +import javax.annotation.Nonnull; + public abstract class Arrays2 { @SafeVarargs + @Nonnull public static T[] of(final T... values) { + Objects.requireNonNull(values); return values; } + @Nonnull public static short[] of(final short... values) { + Objects.requireNonNull(values); return values; } + @Nonnull public static int[] of(final int... values) { + Objects.requireNonNull(values); return values; } + @Nonnull public static long[] of(final long... values) { + Objects.requireNonNull(values); return values; } + @Nonnull public static byte[] of(final byte... values) { + Objects.requireNonNull(values); return values; } + @Nonnull public static boolean[] of(final boolean... values) { + Objects.requireNonNull(values); return values; } + @Nonnull public static char[] of(final char... values) { + Objects.requireNonNull(values); return values; } + @Nonnull public static double[] of(final double... values) { + Objects.requireNonNull(values); return values; } + @Nonnull public static float[] of(final float... values) { + Objects.requireNonNull(values); return values; } 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 7135319..30ad4ab 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/EnumUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/EnumUtil.java @@ -18,6 +18,9 @@ package xyz.zhouxy.plusone.commons.util; import java.util.Objects; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + /** * 枚举工具类 * @@ -37,7 +40,7 @@ public final class EnumUtil { * @param ordinal 数据库中对应的数值 * @return 枚举对象 */ - public static > E valueOf(Class clazz, int ordinal) { + public static > E valueOf(@Nonnull Class clazz, int ordinal) { E[] values = clazz.getEnumConstants(); try { return values[ordinal]; @@ -54,11 +57,12 @@ public final class EnumUtil { * @param ordinal 数据库中对应的数值 * @return 枚举对象 */ - public static > E getValueOrDefault(Class clazz, Integer ordinal) { + 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)); } } @@ -71,19 +75,17 @@ public final class EnumUtil { * @param ordinal 数据库中对应的数值 * @return 枚举对象 */ - public static > E getValueNullable(Class clazz, Integer ordinal) { + 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 > Integer checkOrdinal(Class clazz, Integer ordinal) { - if (ordinal == null) { - throw new IllegalArgumentException("ordinal 不能为空"); - } + public static > Integer checkOrdinal(@Nonnull Class clazz, @Nonnull Integer ordinal) { E[] values = clazz.getEnumConstants(); if (ordinal >= 0 && ordinal < values.length) { return ordinal; @@ -91,15 +93,15 @@ public final class EnumUtil { throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal)); } - public static > Integer checkOrdinalNullable(Class clazz, Integer ordinal) { - if (Objects.isNull(ordinal)) { + public static > Integer checkOrdinalNullable(@Nonnull Class clazz, @Nullable Integer ordinal) { + if (ordinal == null) { return null; } return checkOrdinal(clazz, ordinal); } - public static > Integer checkOrdinalOrDefault(Class clazz, Integer ordinal) { - if (Objects.isNull(ordinal)) { + public static > Integer checkOrdinalOrDefault(@Nonnull Class clazz, @Nullable Integer ordinal) { + if (ordinal == null) { return 0; } return checkOrdinal(clazz, ordinal); 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 b1255d3..397f888 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java @@ -20,14 +20,17 @@ import java.util.Map; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; +import javax.annotation.Nonnull; + /** * 枚举类 */ public abstract class Enumeration> { protected final int value; + @Nonnull protected final String name; - protected Enumeration(int value, String name) { + protected Enumeration(int value, @Nonnull String name) { this.value = value; this.name = name; } @@ -36,6 +39,7 @@ public abstract class Enumeration> { return value; } + @Nonnull public String getName() { return name; } @@ -68,16 +72,20 @@ public abstract class Enumeration> { @SafeVarargs public EnumerationValuesHolder(T... values) { for (T value : values) { + Objects.requireNonNull(value); put(value); } } - private void put(T constant) { + private void put(@Nonnull T constant) { this.constants.put(constant.getValue(), constant); } - + + @Nonnull public T get(int value) { - return this.constants.get(value); + T val = this.constants.get(value); + Objects.requireNonNull(val); + return val; } } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/NumberUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/NumberUtil.java index 088e005..1395050 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/NumberUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/NumberUtil.java @@ -27,7 +27,7 @@ public class NumberUtil { throw new IllegalStateException("Utility class"); } - public static int sum(short... numbers) { + public static int sum(final short... numbers) { int result = 0; for (short number : numbers) { result += number; @@ -35,7 +35,7 @@ public class NumberUtil { return result; } - public static long sum(int... numbers) { + public static long sum(final int... numbers) { int result = 0; for (int number : numbers) { result += number; @@ -43,7 +43,7 @@ public class NumberUtil { return result; } - public static long sum(long... numbers) { + public static long sum(final long... numbers) { long result = 0; for (long number : numbers) { result += number; @@ -51,7 +51,7 @@ public class NumberUtil { return result; } - public static double sum(float... numbers) { + public static double sum(final float... numbers) { double result = 0; for (double number : numbers) { result += number; @@ -59,7 +59,7 @@ public class NumberUtil { return result; } - public static double sum(double... numbers) { + public static double sum(final double... numbers) { double result = 0; for (double number : numbers) { result += number; 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 049658e..e737013 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/PageDTO.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/PageDTO.java @@ -18,6 +18,8 @@ package xyz.zhouxy.plusone.commons.util; import java.util.List; +import javax.annotation.Nonnull; + /** * 返回分页查询的结果 * @@ -28,38 +30,31 @@ import java.util.List; */ public class PageDTO { - private Long total; - private List content; + @Nonnull + private final Long total; + @Nonnull + private final List content; - private PageDTO(List content, Long total) { + private PageDTO(@Nonnull List content, @Nonnull Long total) { this.content = content; this.total = total; } - public static PageDTO of(List content, Long total) { + @Nonnull + public static PageDTO of(@Nonnull List content, @Nonnull Long total) { return new PageDTO<>(content, total); } + @Nonnull public Long getTotal() { return total; } + @Nonnull public List getContent() { return content; } - // Setters - - public void setTotal(Long total) { - this.total = total; - } - - public void setContent(List content) { - this.content = content; - } - - // Setters end - @Override public String toString() { return "PageDTO [total=" + total + ", content=" + content + "]"; diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/PagingAndSortingQueryParams.java b/src/main/java/xyz/zhouxy/plusone/commons/util/PagingAndSortingQueryParams.java index 5959189..cdfe472 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/PagingAndSortingQueryParams.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/PagingAndSortingQueryParams.java @@ -47,6 +47,8 @@ public class PagingAndSortingQueryParams { this.sortableColNames = Arrays.asList(sortableColNames); } + // Getters + public String getOrderBy() { return orderBy != null && sortableColNames.contains(orderBy) ? orderBy : null; } @@ -63,6 +65,8 @@ public class PagingAndSortingQueryParams { return (getPageNum() - 1) * getSize(); } + // Getters end + // Setters public void setOrderBy(String orderBy) { diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/RestfulResult.java b/src/main/java/xyz/zhouxy/plusone/commons/util/RestfulResult.java index 7a8b5f1..e967a21 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/RestfulResult.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/RestfulResult.java @@ -19,6 +19,9 @@ package xyz.zhouxy.plusone.commons.util; import java.util.function.BooleanSupplier; import java.util.function.Supplier; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + import com.fasterxml.jackson.annotation.JsonInclude; /** @@ -32,59 +35,83 @@ public class RestfulResult { public static final int SUCCESS_STATUS = 2000000; public static final int DEFAULT_ERROR_STATUS = 9999999; + @Nonnull private final Object status; + @Nonnull private final String message; + @Nullable private final Object data; - private RestfulResult(final Object status, final String message) { + private RestfulResult(@Nonnull final Object status, @Nonnull final String message) { this(status, message, null); } + @Nonnull public static RestfulResult success() { return new RestfulResult(SUCCESS_STATUS, "操作成功"); } - public static RestfulResult success(final String message) { + @Nonnull + public static RestfulResult success(@Nonnull final String message) { return new RestfulResult(SUCCESS_STATUS, message); } - public static RestfulResult success(final String message, final Object data) { + @Nonnull + public static RestfulResult success( + @Nonnull final String message, + @Nullable final Object data) { return new RestfulResult(SUCCESS_STATUS, message, data); } + @Nonnull public static RestfulResult error() { return new RestfulResult(DEFAULT_ERROR_STATUS, "未知错误"); } - public static RestfulResult error(final Object status, final String message) { + @Nonnull + public static RestfulResult error( + @Nonnull final Object status, + @Nonnull final String message) { return new RestfulResult(status, message); } - public static RestfulResult error(final Object status, final String message, final Object data) { + @Nonnull + public static RestfulResult error( + @Nonnull final Object status, + @Nonnull final String message, + @Nullable final Object data) { return new RestfulResult(status, message, data); } - public static RestfulResult error(final Object status, final Throwable e) { - return new RestfulResult(status, e.getMessage()); + @Nonnull + public static RestfulResult error(@Nonnull final Object status, @Nonnull final Throwable e) { + String msg = e.getMessage(); + if (msg == null) { + msg = ""; + } + return new RestfulResult(status, msg); } public static RestfulResult of( final boolean isSuccess, - final Supplier success, - final Supplier error) { + @Nonnull final Supplier success, + @Nonnull final Supplier error) { return isSuccess ? success.get() : error.get(); } public static RestfulResult of( - final BooleanSupplier isSuccess, - final Supplier success, - final Supplier error) { + @Nonnull final BooleanSupplier isSuccess, + @Nonnull final Supplier success, + @Nonnull final Supplier error) { return isSuccess.getAsBoolean() ? success.get() : error.get(); } // Constructors - private RestfulResult(Object status, String message, Object data) { + private RestfulResult( + @Nonnull final Object status, + @Nonnull final String message, + @Nullable final Object data) { this.status = status; this.message = message; this.data = data; @@ -94,14 +121,17 @@ public class RestfulResult { // Getters + @Nonnull public Object getStatus() { return status; } + @Nonnull public String getMessage() { return message; } + @Nullable public Object getData() { return data; } @@ -112,59 +142,4 @@ public class RestfulResult { public String toString() { return "RestfulResult [status=" + status + ", message=" + message + ", data=" + data + "]"; } - - // Builder - public static Builder successIf(final boolean condition) { - return successIf(() -> condition); - } - - public static Builder successIf(final BooleanSupplier booleanSupplier) { - return new Builder(booleanSupplier, () -> success()); - } - - public static Builder successIf(final boolean condition, final String msg) { - return new Builder(() -> condition, () -> success(msg)); - } - - public static Builder successIf(final BooleanSupplier booleanSupplier, final String msg) { - return new Builder(booleanSupplier, () -> success(msg)); - } - - public static Builder successIf(final boolean condition, final String msg, final Object data) { - return new Builder(() -> condition, () -> success(msg, data)); - } - - public static Builder successIf(final BooleanSupplier booleanSupplier, final String msg, final Object data) { - return new Builder(booleanSupplier, () -> success(msg, data)); - } - - public static class Builder { - private final BooleanSupplier booleanSupplier; - private final Supplier success; - - public Builder(final BooleanSupplier booleanSupplier, final Supplier success) { - this.booleanSupplier = booleanSupplier; - this.success = success; - } - - public RestfulResult orError() { - return this.booleanSupplier.getAsBoolean() - ? this.success.get() - : RestfulResult.error(); - } - - public RestfulResult orError(final Object status, final String msg) { - return orError(status, msg, null); - } - - public RestfulResult orError(final Object status, final Throwable e) { - return orError(status, e.getMessage()); - } - - public RestfulResult orError(final Object status, final String msg, final Object data) { - return this.booleanSupplier.getAsBoolean() - ? this.success.get() - : RestfulResult.error(status, msg, data); - } - } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java b/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java index 6e950a7..0ab1334 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java @@ -2,6 +2,8 @@ package xyz.zhouxy.plusone.commons; import static org.junit.jupiter.api.Assertions.assertEquals; +import javax.annotation.Nonnull; + import org.junit.jupiter.api.Test; import xyz.zhouxy.plusone.commons.util.Enumeration; @@ -16,7 +18,7 @@ class EnumerationTests { final class EntityStatus extends Enumeration { - private EntityStatus(int value, String name) { + private EntityStatus(int value, @Nonnull String name) { super(value, name); } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/RestfulResultTest.java b/src/test/java/xyz/zhouxy/plusone/commons/util/RestfulResultTest.java deleted file mode 100644 index 177d05a..0000000 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/RestfulResultTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package xyz.zhouxy.plusone.commons.util; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -class RestfulResultTest { - - @Test - void testSuccessIf() { - String str = null; - RestfulResult result = RestfulResult.successIf(str != null, "成功") - .orError(); - System.out.println(result); - assertEquals(RestfulResult.DEFAULT_ERROR_STATUS, result.getStatus()); - - result = RestfulResult.successIf(str != null, "成功") - .orError(2333, "失败"); - System.out.println(result); - assertEquals(2333, result.getStatus()); - assertEquals("失败", result.getMessage()); - - str = ""; - result = RestfulResult.successIf(str != null, "成功") - .orError(); - System.out.println(result); - assertEquals(RestfulResult.SUCCESS_STATUS, result.getStatus()); - - result = RestfulResult.successIf(str != null, "成功", str) - .orError(2333, "失败"); - System.out.println(result); - assertEquals("成功", result.getMessage()); - } -}