org.zodiac.script.engine.ExecuteResult Maven / Gradle / Ivy
package org.zodiac.script.engine;
import javax.script.ScriptException;
import java.io.Serializable;
import java.util.function.Supplier;
public class ExecuteResult implements Serializable {
private static final long serialVersionUID = -1091229967577001409L;
private boolean success;
private Object result;
private String message;
private transient Exception exception;
private long useTime;
public ExecuteResult() {
super();
}
public boolean isSuccess() {
return success;
}
public ExecuteResult setSuccess(boolean success) {
this.success = success;
return this;
}
/**
* Use {@link ExecuteResult#get()} or {@link ExecuteResult#getIfSuccess()}.
*
* @return result
*/
@Deprecated
public Object getResult() {
return result;
}
public ExecuteResult setResult(Object result) {
this.result = result;
return this;
}
public String getMessage() {
if (message == null && exception != null) {
message = exception.getMessage();
}
return message;
}
public ExecuteResult setMessage(String message) {
this.message = message;
return this;
}
public Exception getException() {
return exception;
}
public ExecuteResult setException(Exception exception) {
this.exception = exception;
return this;
}
public long getUseTime() {
return useTime;
}
public ExecuteResult setUseTime(long useTime) {
this.useTime = useTime;
return this;
}
@Override
public String toString() {
return String.valueOf(this.get());
}
public Object get() {
return result;
}
public Object getIfSuccess() throws Exception {
if (!success) {
if (exception != null) {
throw exception;
} else
throw new ScriptException(message);
}
return result;
}
public Object getIfSuccess(Object defaultValue) {
if (!success) {
return defaultValue;
}
return result;
}
public Object getIfSuccess(Supplier> supplier) {
if (!success) {
return supplier.get();
}
return result;
}
}