com.ly.mybatis.mapperservice.model.Result Maven / Gradle / Ivy
package com.ly.mybatis.mapperservice.model;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.util.Collection;
import java.util.Map;
import java.util.function.Supplier;
public class Result {
private boolean success = true;
private String optype;
private T result;
private String message;
public Result() {
}
public Result(boolean success, String optype, T result, String message) {
this.success = success;
this.optype = optype;
this.result = result;
this.message = message;
}
public boolean isEmpty() {
return isEmpty(result);
}
public boolean isSuccess() {
return success;
}
public Result setSuccess(boolean success) {
this.success = success;
return this;
}
public String getOptype() {
return optype;
}
public Result setOptype(String optype) {
this.optype = optype;
return this;
}
public T get() {
return result;
}
public T getOrDefault(T defaultValue) {
if (isSuccess()) {
return result;
}
return defaultValue;
}
public T getOrDefault(Supplier defaultValueSupplier) {
if (isSuccess()) {
return result;
}
return defaultValueSupplier.get();
}
public T getOrException(Supplier extends RuntimeException> exceptionSupplier) {
if (isSuccess()) {
return result;
}
throw exceptionSupplier.get();
}
public Result set(T result) {
this.result = result;
return this;
}
public String getMessage() {
return message;
}
public Result setMessage(String message) {
this.message = message;
return this;
}
private boolean isEmpty(Object object) {
if (object == null) {
return true;
}
if (object instanceof String) {
return "".equals(object);
}
if (object instanceof Collection) {
return ((Collection>) object).isEmpty();
}
if (object instanceof Map) {
return ((Map, ?>) object).isEmpty();
}
if (object instanceof IPage) {
return isEmpty(((IPage>) object).getRecords());
}
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy