net.paoding.rose.jade.statement.jexl.analysis.impl.IfElseAnalysis Maven / Gradle / Ivy
package net.paoding.rose.jade.statement.jexl.analysis.impl;
import net.paoding.rose.jade.statement.jexl.Jexl3Execute;
import net.paoding.rose.jade.statement.jexl.analysis.Analysis;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Objects;
/**
* if 表达式解析
* @author fusheng.zhang
* @date 2022-03-05 16:24:15
*/
public class IfElseAnalysis implements Analysis {
/**
* if条件语句 ()
*/
private String ifCondition;
private String ifExecute;
private List ifAnalyses;
private String elseExecute;
private List elseAnalyses;
@Override
public String toString() {
StringBuilder str = new StringBuilder();
if (Objects.nonNull(ifCondition)) {
str.append("if(").append(ifCondition).append(")").append("{");
if (Objects.nonNull(ifExecute)) {
str.append(ifExecute);
}
if (Objects.nonNull(ifAnalyses)) {
ifAnalyses.forEach(analysis -> str.append(analysis.toString()));
}
str.append("}");
if (Objects.nonNull(elseExecute)) {
str.append(" else {").append(elseExecute).append("}");
}
if (Objects.nonNull(elseAnalyses)) {
str.append(" else {");
elseAnalyses.forEach(analysis -> str.append(analysis.toString()));
str.append("}");
}
}
return str.toString();
}
@Override
public void execute(Jexl3Execute jexl3Execute) {
Object o = jexl3Execute.execute(jexl3Execute.getParams(), jexl3Execute.getConstantParams(), toStr(ifCondition));
if (Objects.nonNull(o) && Boolean.parseBoolean(o.toString())) {
if (StringUtils.isNotBlank(ifExecute)) jexl3Execute.setStr(ifExecute);
if (!CollectionUtils.isEmpty(ifAnalyses)) {
ifAnalyses.forEach(analysis -> analysis.execute(jexl3Execute));
}
} else {
if (StringUtils.isNotBlank(elseExecute)) jexl3Execute.setStr(elseExecute);
if (!CollectionUtils.isEmpty(elseAnalyses)) {
elseAnalyses.forEach(analysis -> analysis.execute(jexl3Execute));
}
}
}
public String getIfCondition() {
return ifCondition;
}
public void setIfCondition(String ifCondition) {
this.ifCondition = ifCondition;
}
public String getIfExecute() {
return ifExecute;
}
public void setIfExecute(String ifExecute) {
this.ifExecute = ifExecute;
}
public List getIfAnalyses() {
return ifAnalyses;
}
public void setIfAnalyses(List ifAnalyses) {
this.ifAnalyses = ifAnalyses;
}
public String getElseExecute() {
return elseExecute;
}
public void setElseExecute(String elseExecute) {
this.elseExecute = elseExecute;
}
public List getElseAnalyses() {
return elseAnalyses;
}
public void setElseAnalyses(List elseAnalyses) {
this.elseAnalyses = elseAnalyses;
}
}