允许 message 和 data 参数传 null。

dev
ZhouXY108 2024-03-02 14:49:59 +08:00
parent 5d0af2dad5
commit 1cf0b19ad0
4 changed files with 35 additions and 25 deletions

View File

@ -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);
}

View File

@ -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());
}
<E extends Throwable & IWithCode<?>> 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;

View File

@ -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);
}

View File

@ -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<String, Object> {
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<String, Object> {
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<String, Object> {
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<String, Object> {
}
}
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);
}
/**