减少 Nonnull 注解的滥用。

feature/net-util
ZhouXY108 2023-04-21 03:21:44 +08:00
parent f4da6b6ca1
commit 0e2c52b717
1 changed files with 29 additions and 35 deletions

View File

@ -19,7 +19,6 @@ package xyz.zhouxy.plusone.commons.util;
import java.util.function.BooleanSupplier; import java.util.function.BooleanSupplier;
import java.util.function.Supplier; import java.util.function.Supplier;
import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
@ -35,14 +34,12 @@ public class RestfulResult {
public static final int SUCCESS_STATUS = 2000000; public static final int SUCCESS_STATUS = 2000000;
public static final int DEFAULT_ERROR_STATUS = 9999999; public static final int DEFAULT_ERROR_STATUS = 9999999;
@Nonnull
private final Object status; private final Object status;
@Nonnull
private final String message; private final String message;
@Nullable
private final Object data;
private RestfulResult(@Nonnull final Object status, @Nonnull final String message) { private final @Nullable Object data;
private RestfulResult(final Object status, final String message) {
this(status, message, null); this(status, message, null);
} }
@ -50,34 +47,35 @@ public class RestfulResult {
return new RestfulResult(SUCCESS_STATUS, "操作成功"); return new RestfulResult(SUCCESS_STATUS, "操作成功");
} }
public static RestfulResult success(@Nonnull final String message) { public static RestfulResult success(final String message) {
Assert.notNull(message, "Message must not be null.");
return new RestfulResult(SUCCESS_STATUS, message); return new RestfulResult(SUCCESS_STATUS, message);
} }
public static RestfulResult success( public static RestfulResult success(final String message, @Nullable final Object data) {
@Nonnull final String message, Assert.notNull(message, "Message must not be null.");
@Nullable final Object data) {
return new RestfulResult(SUCCESS_STATUS, message, data); return new RestfulResult(SUCCESS_STATUS, message, data);
} }
public static RestfulResult error() { public static RestfulResult error() {
return new RestfulResult(DEFAULT_ERROR_STATUS, "未知错误"); return new RestfulResult(DEFAULT_ERROR_STATUS, "未知错误");
} }
public static RestfulResult error( public static RestfulResult error(final Object status, final String message) {
@Nonnull final Object status, Assert.notNull(status, "Status must not be null.");
@Nonnull final String message) { Assert.notNull(message, "Message must not be null.");
return new RestfulResult(status, message); return new RestfulResult(status, message);
} }
public static RestfulResult error( public static RestfulResult error(final Object status, final String message, @Nullable final Object data) {
@Nonnull final Object status, Assert.notNull(status, "Status must not be null.");
@Nonnull final String message, Assert.notNull(message, "Message must not be null.");
@Nullable final Object data) {
return new RestfulResult(status, message, data); return new RestfulResult(status, message, data);
} }
public static RestfulResult error(@Nonnull final Object status, @Nonnull final Throwable e) { public static RestfulResult error(final Object status, final Throwable e) {
Assert.notNull(status, "Status must not be null.");
Assert.notNull(e, "Exception must not be null.");
String msg = e.getMessage(); String msg = e.getMessage();
if (msg == null) { if (msg == null) {
msg = ""; msg = "";
@ -85,26 +83,24 @@ public class RestfulResult {
return new RestfulResult(status, msg); return new RestfulResult(status, msg);
} }
public static RestfulResult of( public static RestfulResult of(final boolean isSuccess,
final boolean isSuccess, final Supplier<RestfulResult> success, final Supplier<RestfulResult> error) {
@Nonnull final Supplier<RestfulResult> success, Assert.notNull(success, "Success supplier must not be null.");
@Nonnull final Supplier<RestfulResult> error) { Assert.notNull(error, "Error supplier must not be null.");
return isSuccess ? success.get() : error.get(); return isSuccess ? success.get() : error.get();
} }
public static RestfulResult of( public static RestfulResult of(final BooleanSupplier isSuccess,
@Nonnull final BooleanSupplier isSuccess, final Supplier<RestfulResult> success, final Supplier<RestfulResult> error) {
@Nonnull final Supplier<RestfulResult> success, Assert.notNull(isSuccess, "Conditions for success must not be null.");
@Nonnull final Supplier<RestfulResult> error) { Assert.notNull(success, "Success supplier must not be null.");
Assert.notNull(error, "Error supplier must not be null.");
return isSuccess.getAsBoolean() ? success.get() : error.get(); return isSuccess.getAsBoolean() ? success.get() : error.get();
} }
// Constructors // Constructors
private RestfulResult( private RestfulResult(final Object status, final String message, @Nullable final Object data) {
@Nonnull final Object status,
@Nonnull final String message,
@Nullable final Object data) {
this.status = status; this.status = status;
this.message = message; this.message = message;
this.data = data; this.data = data;
@ -114,12 +110,10 @@ public class RestfulResult {
// Getters // Getters
@Nonnull
public Object getStatus() { public Object getStatus() {
return status; return status;
} }
@Nonnull
public String getMessage() { public String getMessage() {
return message; return message;
} }