forked from plusone/plusone-commons
整理代码。
parent
f1300e9a5c
commit
1959d1b2cb
|
@ -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> 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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 extends Enum<?>> E valueOf(Class<E> clazz, int ordinal) {
|
||||
public static <E extends Enum<?>> E valueOf(@Nonnull Class<E> 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 extends Enum<?>> E getValueOrDefault(Class<E> clazz, Integer ordinal) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -71,19 +75,17 @@ public final class EnumUtil {
|
|||
* @param ordinal 数据库中对应的数值
|
||||
* @return 枚举对象
|
||||
*/
|
||||
public static <E extends Enum<?>> E getValueNullable(Class<E> clazz, Integer ordinal) {
|
||||
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<?>> Integer checkOrdinal(Class<E> clazz, Integer ordinal) {
|
||||
if (ordinal == null) {
|
||||
throw new IllegalArgumentException("ordinal 不能为空");
|
||||
}
|
||||
public static <E extends Enum<?>> Integer checkOrdinal(@Nonnull Class<E> 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 <E extends Enum<?>> Integer checkOrdinalNullable(Class<E> clazz, Integer ordinal) {
|
||||
if (Objects.isNull(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 checkOrdinalOrDefault(Class<E> clazz, Integer ordinal) {
|
||||
if (Objects.isNull(ordinal)) {
|
||||
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(@Nonnull Class<E> clazz, @Nullable Integer ordinal) {
|
||||
if (ordinal == null) {
|
||||
return 0;
|
||||
}
|
||||
return checkOrdinal(clazz, ordinal);
|
||||
|
|
|
@ -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<T extends Enumeration<T>> {
|
||||
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<T extends Enumeration<T>> {
|
|||
return value;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
@ -68,16 +72,20 @@ public abstract class Enumeration<T extends Enumeration<T>> {
|
|||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<T> {
|
||||
|
||||
private Long total;
|
||||
private List<T> content;
|
||||
@Nonnull
|
||||
private final Long total;
|
||||
@Nonnull
|
||||
private final List<T> content;
|
||||
|
||||
private PageDTO(List<T> content, Long total) {
|
||||
private PageDTO(@Nonnull List<T> content, @Nonnull Long total) {
|
||||
this.content = content;
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public static <T> PageDTO<T> of(List<T> content, Long total) {
|
||||
@Nonnull
|
||||
public static <T> PageDTO<T> of(@Nonnull List<T> content, @Nonnull Long total) {
|
||||
return new PageDTO<>(content, total);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Long getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public List<T> getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
// Setters
|
||||
|
||||
public void setTotal(Long total) {
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public void setContent(List<T> content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
// Setters end
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PageDTO [total=" + total + ", content=" + content + "]";
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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<RestfulResult> success,
|
||||
final Supplier<RestfulResult> error) {
|
||||
@Nonnull final Supplier<RestfulResult> success,
|
||||
@Nonnull final Supplier<RestfulResult> error) {
|
||||
return isSuccess ? success.get() : error.get();
|
||||
}
|
||||
|
||||
public static RestfulResult of(
|
||||
final BooleanSupplier isSuccess,
|
||||
final Supplier<RestfulResult> success,
|
||||
final Supplier<RestfulResult> error) {
|
||||
@Nonnull final BooleanSupplier isSuccess,
|
||||
@Nonnull final Supplier<RestfulResult> success,
|
||||
@Nonnull final Supplier<RestfulResult> 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<RestfulResult> success;
|
||||
|
||||
public Builder(final BooleanSupplier booleanSupplier, final Supplier<RestfulResult> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<EntityStatus> {
|
||||
|
||||
private EntityStatus(int value, String name) {
|
||||
private EntityStatus(int value, @Nonnull String name) {
|
||||
super(value, name);
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue