T - Data Typepublic class R<T> extends I18nMessage implements DataResult<T>, ErrorResult, I18nAware
## Conventions 1. ErrorResult with errors - the service is abnormal and incomplete. 1.1. success - must be false. 1.2. if has i18n-message - should be the 1st error message. 1.3. if has code - the err-code to clarify the detailed error. 2. DataResult without errors - the service is normal and complete. 2.1. success - can be true or false. 2.2. if has i18n-message - give more information to the user. 2.3. if has code - the biz-code to clarify the detailed data. 2.4. if has data - the biz-data to the user, regardless of success. ## Basic result container. - success - whether the result succeeded, default false. - code - biz/err code. CodeEnum auto set the code. - data - business data in the result. - errors - errors thrown, success must be false. - message - default i18n message or template - i18nCode - i18n template code - i18nArgs - i18n template args ## serialize and @Transient The following are `@Transient`, should ignore for `hashCode`, `equals` and `json` - `cause` - Internal error for tracking. Such as exceptions, strings, enum, etc. `@Transient` and `transient` are difference, - kryo - ignore transient field - jackson - ignore transient field and @Transient method - fastjson - ignore transient field NOTE-1: should NOT use `@Transient` on any setter-like method, or jackson will ignore properties by name, e.g. setXxx or getXxx will ignore the xxx property, the param list are not considered, check POJOPropertiesCollector#_removeUnwantedProperties and POJOPropertyBuilder#anyIgnorals NOTE-2: When using the`cast*` method, be careful to avoid the ClassCastException.
| Modifier and Type | Class and Description |
|---|---|
static class |
R.Immutable<T>
throw UnsupportedOperationException if modify
|
I18nAware.I18nSource| Modifier and Type | Field and Description |
|---|---|
protected Object |
cause |
protected String |
code |
protected Object |
data |
protected List<I18nNotice> |
errors |
protected boolean |
success |
i18nArgs, i18nCode, message| Constructor and Description |
|---|
R() |
R(boolean success) |
R(boolean success,
T data) |
R(boolean success,
T data,
CodeEnum code) |
R(boolean success,
T data,
I18nAware message) |
R(boolean success,
T data,
String code) |
R(boolean success,
T data,
String code,
I18nAware message) |
R(boolean success,
T data,
String code,
String message) |
R(boolean success,
T data,
String code,
String message,
String i18nCode) |
R(boolean success,
T data,
String code,
String message,
String i18nCode,
Object... i18nArgs) |
R(I18nNotice err) |
R(List<I18nNotice> errs) |
| Modifier and Type | Method and Description |
|---|---|
void |
applyLocale(Locale locale,
@NotNull I18nAware.I18nSource source)
apply the internal locale and updates the i18n state based on the provided source.
|
<S extends R<D>,D> |
castData(D data)
replace the data and force to cast to subclass
|
<S extends R<D>,D> |
castData(Function<T,D> fun)
replace the data and force to cast to subclass
|
boolean |
equals(Object o) |
@Nullable Object |
getCause() |
<E> E |
getCause(Class<E> type) |
String |
getCode() |
T |
getData()
the biz-data to caller
|
@Nullable List<I18nNotice> |
getErrors()
errors cause success to be false,
should be null if empty.
|
int |
hashCode() |
boolean |
isSuccess()
whether success, default false.
|
static <T> R<T> |
ng() |
static <T> R<T> |
NG() |
static <T> R<T> |
ng(T data) |
static <T> R<T> |
ng(T data,
CodeEnum code) |
static <T> R<T> |
ng(T data,
I18nAware code) |
static <T> R<T> |
ng(T data,
String code) |
static <T> R<T> |
ng(T data,
String code,
I18nAware message) |
static <T> R<T> |
ng(T data,
String code,
String message) |
static <T> R<T> |
ngCause(Throwable t) |
static <T> R<T> |
ngCause(Throwable t,
CodeEnum code) |
static <T> R<T> |
ngCause(Throwable t,
I18nAware message) |
static <T> R<T> |
ngCause(Throwable t,
String code) |
static <T> R<T> |
ngCause(Throwable t,
String code,
I18nAware message) |
static <T> R<T> |
ngCause(Throwable t,
String code,
String message) |
static <T> R<T> |
ngCode(CodeEnum code) |
static <T> R<T> |
ngCode(String code) |
static <T> R<T> |
ngCodeMessage(CodeEnum code) |
static <T> R<T> |
ngError(I18nNotice error) |
static <T> R<T> |
ngError(I18nNotice error,
String code) |
static <T> R<T> |
ngError(List<I18nNotice> errors) |
static <T> R<T> |
ngError(List<I18nNotice> errors,
String code) |
static <T> R<T> |
ngError(Throwable t,
String type) |
static <T> R<T> |
ngMessage(I18nAware message) |
static <T> R<T> |
ngMessage(String message) |
static <T> R<T> |
of(boolean success) |
static <T> R<T> |
of(boolean success,
T data) |
static <T> R<T> |
of(boolean success,
T data,
CodeEnum code) |
static <T> R<T> |
of(boolean success,
T data,
I18nAware message) |
static <T> R<T> |
of(boolean success,
T data,
String code) |
static <T> R<T> |
of(boolean success,
T data,
String code,
I18nAware message) |
static <T> R<T> |
of(boolean success,
T data,
String code,
String message) |
static <T> R<T> |
ofCode(boolean success,
CodeEnum code) |
static <T> R<T> |
ofCode(boolean success,
String code) |
static <T> R<T> |
ofCodeMessage(boolean success,
CodeEnum code) |
static <T> R<T> |
ofMessage(boolean success,
I18nAware message) |
static <T> R<T> |
ofMessage(boolean success,
String message) |
static <T> R<T> |
ok() |
static <T> R<T> |
OK() |
static <T> R<T> |
ok(T data) |
static <T> R<T> |
ok(T data,
CodeEnum code) |
static <T> R<T> |
ok(T data,
I18nAware code) |
static <T> R<T> |
ok(T data,
String code) |
static <T> R<T> |
ok(T data,
String code,
I18nAware message) |
static <T> R<T> |
ok(T data,
String code,
String message) |
static <T> R<T> |
okCode(CodeEnum code) |
static <T> R<T> |
okCode(String code) |
static <T> R<T> |
okCodeMessage(CodeEnum code) |
static <T> R<T> |
okMessage(I18nAware message) |
static <T> R<T> |
okMessage(String message) |
static <T> R<T> |
orCode(boolean success,
CodeEnum okCode,
CodeEnum ngCode) |
static <T> R<T> |
orCode(boolean success,
String okCode,
String ngCode) |
static <T> R<T> |
orCodeMessage(boolean success,
CodeEnum okCode,
CodeEnum ngCode) |
static <T> R<T> |
orData(boolean success,
T okData,
T ngData) |
static <T> R<T> |
orMessage(boolean success,
String okMessage,
String ngMessage) |
R<T> |
setCause(Object cause) |
R<T> |
setCode(String code) |
R<T> |
setCodeIfNg(String code) |
R<T> |
setCodeIfNg(@NotNull Supplier<String> code) |
R<T> |
setCodeIfOk(String code) |
R<T> |
setCodeIfOk(@NotNull Supplier<String> code) |
R<T> |
setCodeMessage(CodeEnum code) |
R<T> |
setCodeMessage(String code,
String message) |
R<T> |
setCodeMessageIfNg(CodeEnum code) |
R<T> |
setCodeMessageIfNg(@NotNull Supplier<CodeEnum> code) |
R<T> |
setCodeMessageIfOk(CodeEnum code) |
R<T> |
setCodeMessageIfOk(@NotNull Supplier<CodeEnum> code) |
R<T> |
setData(T data) |
R<T> |
setDataIfNg(@NotNull Supplier<T> data) |
R<T> |
setDataIfNg(T data) |
R<T> |
setDataIfOk(@NotNull Supplier<T> data) |
R<T> |
setDataIfOk(T data) |
R<T> |
setErrors(List<I18nNotice> errors) |
R<T> |
setMessageIfNg(String message) |
R<T> |
setMessageIfNg(@NotNull Supplier<String> message) |
R<T> |
setMessageIfOk(String message) |
R<T> |
setMessageIfOk(@NotNull Supplier<String> message) |
R<T> |
setSuccess(boolean success) |
String |
toString() |
getI18nArgs, getI18nCode, getI18nHint, getMessage, of, setI18nArgs, setI18nCode, setMessage, setMessageBy, setMessageBy, setMessageBy, setMessageByclone, finalize, getClass, notify, notifyAll, wait, wait, waitgetErrorsIf, getErrorsOrgetI18nArgs, getI18nArgsIf, getI18nArgsOr, getI18nCode, getI18nCodeIf, getI18nCodeOr, getI18nHint, getI18nHintIf, getI18nHintOr, toI18nString, toI18nString, toI18nString, toString, toString, toStringcastprotected boolean success
protected Object data
protected String code
protected List<I18nNotice> errors
protected Object cause
public R()
public R(boolean success)
public R(boolean success,
T data)
public R(boolean success,
T data,
String code,
String message,
String i18nCode,
Object... i18nArgs)
public R(I18nNotice err)
public R(List<I18nNotice> errs)
public boolean isSuccess()
DoneAware@Nullable public @Nullable List<I18nNotice> getErrors()
ErrorResultgetErrors in interface ErrorResult@Contract(value="_->this") public R<T> setErrors(List<I18nNotice> errors)
public void applyLocale(Locale locale, @NotNull @NotNull I18nAware.I18nSource source)
I18nAwareapplyLocale in interface I18nAwareapplyLocale in class I18nMessagelocale - the locale to be appliedsource - the i18n source providing the translations@Nullable public @Nullable Object getCause()
@Nullable public <E> E getCause(Class<E> type)
@Contract(value="_->this") public R<T> setCodeIfOk(@NotNull @NotNull Supplier<String> code)
@Contract(value="_->this") public R<T> setCodeIfNg(@NotNull @NotNull Supplier<String> code)
@Contract(value="_->this") public R<T> setMessageIfOk(@NotNull @NotNull Supplier<String> message)
@Contract(value="_->this") public R<T> setMessageIfNg(@NotNull @NotNull Supplier<String> message)
@Contract(value="_->this") public R<T> setCodeMessageIfOk(@NotNull @NotNull Supplier<CodeEnum> code)
@Contract(value="_->this") public R<T> setCodeMessageIfNg(@NotNull @NotNull Supplier<CodeEnum> code)
@Contract(value="_->this") public <S extends R<D>,D> S castData(D data)
S - subclass typeD - data typedata - new dataClassCastException - if type not match@Contract(value="_->this") public <S extends R<D>,D> S castData(Function<T,D> fun)
S - subclass typeD - new typefun - type convertorClassCastException - if type not matchpublic boolean equals(Object o)
equals in class I18nMessagepublic int hashCode()
hashCode in class I18nMessagepublic String toString()
toString in class I18nMessagepublic static <T> R<T> orData(boolean success, T okData, T ngData)
public static <T> R<T> orCodeMessage(boolean success, CodeEnum okCode, CodeEnum ngCode)
public static <T> R<T> of(boolean success)
public static <T> R<T> of(boolean success, T data)
public static <T> R<T> OK()
public static <T> R<T> ok()
public static <T> R<T> ok(T data)
public static <T> R<T> NG()
public static <T> R<T> ng()
public static <T> R<T> ng(T data)
public static <T> R<T> ngError(I18nNotice error)
public static <T> R<T> ngError(I18nNotice error, String code)
public static <T> R<T> ngError(List<I18nNotice> errors)
public static <T> R<T> ngError(List<I18nNotice> errors, String code)
Copyright © 2025. All rights reserved.