From 1cf0b19ad020b98ad95dc717214f162b38c551ed Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sat, 2 Mar 2024 14:49:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=81=E8=AE=B8=20message=20=E5=92=8C=20data?= =?UTF-8?q?=20=E5=8F=82=E6=95=B0=E4=BC=A0=20null=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/util/CustomResult.java | 6 ++-- .../plusone/commons/util/ErrorResult.java | 16 ++++++---- .../plusone/commons/util/SuccessResult.java | 6 ++-- .../plusone/commons/util/UnifiedResponse.java | 32 +++++++++++-------- 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/CustomResult.java b/src/main/java/xyz/zhouxy/plusone/commons/util/CustomResult.java index ee8bf19..e9aff0b 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/CustomResult.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/CustomResult.java @@ -1,5 +1,7 @@ package xyz.zhouxy.plusone.commons.util; +import javax.annotation.Nullable; + /** * 自定义结果 * @@ -7,11 +9,11 @@ package xyz.zhouxy.plusone.commons.util; */ final class CustomResult extends UnifiedResponse { - CustomResult(Object status, String message) { + CustomResult(Object status, @Nullable String message) { super(status, message); } - CustomResult(Object status, String message, Object data) { + CustomResult(Object status, @Nullable String message, @Nullable Object data) { super(status, message, data); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/ErrorResult.java b/src/main/java/xyz/zhouxy/plusone/commons/util/ErrorResult.java index 7ad1726..7c36cc1 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/ErrorResult.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/ErrorResult.java @@ -1,6 +1,8 @@ package xyz.zhouxy.plusone.commons.util; -import com.google.common.base.Strings; +import java.util.Objects; + +import javax.annotation.Nullable; import xyz.zhouxy.plusone.commons.base.IWithCode; @@ -12,28 +14,28 @@ import xyz.zhouxy.plusone.commons.base.IWithCode; final class ErrorResult extends UnifiedResponse { private static final String DEFAULT_ERR_STATUS = "9999999"; - ErrorResult(String message) { + ErrorResult(@Nullable String message) { super(DEFAULT_ERR_STATUS, message); } - ErrorResult(String message, Object data) { + ErrorResult(@Nullable String message, @Nullable Object data) { super(DEFAULT_ERR_STATUS, message, data); } - ErrorResult(Object status, String message) { + ErrorResult(Object status, @Nullable String message) { super(status, message); } - ErrorResult(Object status, String message, Object data) { + ErrorResult(Object status, @Nullable String message, @Nullable Object data) { super(status, message, data); } ErrorResult(Object status, Throwable e) { - super(status, Strings.nullToEmpty(e.getMessage())); + super(status, Objects.requireNonNull(e).getMessage()); } > ErrorResult(E e) { - super(e.getCode(), Strings.nullToEmpty(e.getMessage())); + super(Objects.requireNonNull(e).getCode(), Objects.requireNonNull(e).getMessage()); } private static final long serialVersionUID = -1680792957826923092L; diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/SuccessResult.java b/src/main/java/xyz/zhouxy/plusone/commons/util/SuccessResult.java index 660f82b..4f9e214 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/SuccessResult.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/SuccessResult.java @@ -1,5 +1,7 @@ package xyz.zhouxy.plusone.commons.util; +import javax.annotation.Nullable; + /** * 成功结果 * @@ -13,11 +15,11 @@ final class SuccessResult extends UnifiedResponse { super(SUCCESS_STATUS, DEFAULT_SUCCESS_MSG); } - SuccessResult(String message) { + SuccessResult(@Nullable String message) { super(SUCCESS_STATUS, message); } - SuccessResult(String message, Object data) { + SuccessResult(@Nullable String message, @Nullable Object data) { super(SUCCESS_STATUS, message, data); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/UnifiedResponse.java b/src/main/java/xyz/zhouxy/plusone/commons/util/UnifiedResponse.java index bf94227..f719aa8 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/UnifiedResponse.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/UnifiedResponse.java @@ -27,6 +27,8 @@ import java.util.function.BooleanSupplier; import java.util.function.Function; import java.util.function.Supplier; +import javax.annotation.Nullable; + import com.google.common.base.Preconditions; /** @@ -45,27 +47,27 @@ public abstract class UnifiedResponse extends HashMap { return new SuccessResult(); } - public static UnifiedResponse success(String message) { + public static UnifiedResponse success(@Nullable String message) { return new SuccessResult(message); } - public static UnifiedResponse success(String message, Object data) { + public static UnifiedResponse success(@Nullable String message, @Nullable Object data) { return new SuccessResult(message, data); } - public static UnifiedResponse error(String message) { + public static UnifiedResponse error(@Nullable String message) { return new ErrorResult(message); } - public static UnifiedResponse error(String message, Object data) { + public static UnifiedResponse error(@Nullable String message, @Nullable Object data) { return new ErrorResult(message, data); } - public static UnifiedResponse error(Object status, String message) { + public static UnifiedResponse error(Object status, @Nullable String message) { return new ErrorResult(status, message); } - public static UnifiedResponse error(Object status, String message, Object data) { + public static UnifiedResponse error(Object status, @Nullable String message, @Nullable Object data) { return new ErrorResult(status, message, data); } @@ -77,11 +79,11 @@ public abstract class UnifiedResponse extends HashMap { return new ErrorResult(e); } - public static UnifiedResponse of(Object status, String message) { + public static UnifiedResponse of(Object status, @Nullable String message) { return new CustomResult(status, message); } - public static UnifiedResponse of(Object status, String message, Object data) { + public static UnifiedResponse of(Object status, @Nullable String message, @Nullable Object data) { return new CustomResult(status, message, data); } @@ -100,12 +102,12 @@ public abstract class UnifiedResponse extends HashMap { return isSuccess.getAsBoolean() ? success.get() : error.get(); } - protected UnifiedResponse(Object status, String message) { + protected UnifiedResponse(Object status, @Nullable String message) { setStatus(status); setMessage(message); } - protected UnifiedResponse(Object status, String message, Object data) { + protected UnifiedResponse(Object status, @Nullable String message, @Nullable Object data) { setStatus(status); setMessage(message); setData(data); @@ -120,12 +122,14 @@ public abstract class UnifiedResponse extends HashMap { } } - private void setData(Object data) { - super.put(DATA_KEY, Objects.requireNonNull(data)); + private void setData(@Nullable Object data) { + if (data != null) { + super.put(DATA_KEY, data); + } } - private void setMessage(String message) { - super.put(MESSAGE_KEY, Objects.requireNonNull(message)); + private void setMessage(@Nullable String message) { + super.put(MESSAGE_KEY, message); } /**