99 Star 776 Fork 252

GVPcanonical-entropy/nop-entropy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
FilterBeanEvaluator.java 3.47 KB
一键复制 编辑 原始数据 按行查看 历史
canonical-entropy 提交于 2024-03-10 09:29 . 更新版权声明
/**
* Copyright (c) 2017-2024 Nop Platform. All rights reserved.
* Author: canonical_entropy@163.com
* Blog: https://www.zhihu.com/people/canonical-entropy
* Gitee: https://gitee.com/canonical-entropy/nop-entropy
* Github: https://github.com/entropy-cloud/nop-entropy
*/
package io.nop.core.model.query;
import io.nop.api.core.beans.ITreeBean;
import io.nop.api.core.util.IVariableScope;
import java.util.List;
import static io.nop.api.core.beans.FilterBeanConstants.FILTER_ATTR_EXCLUDE_MAX;
import static io.nop.api.core.beans.FilterBeanConstants.FILTER_ATTR_EXCLUDE_MIN;
import static io.nop.api.core.convert.ConvertHelper.defaults;
import static io.nop.api.core.convert.ConvertHelper.toBoolean;
/**
* 执行复杂过滤条件
*/
public class FilterBeanEvaluator extends FilterBeanVisitor<Boolean> {
public static final FilterBeanEvaluator INSTANCE = new FilterBeanEvaluator();
@Override
protected Boolean visitCompareOp(FilterOp filterOp, ITreeBean filter, IVariableScope scope) {
String name = getName(filter);
Object leftValue = getValue(scope, name);
String valueName = getValueName(filter);
Object rightValue;
if (valueName != null) {
rightValue = getValue(scope, valueName);
} else {
rightValue = getValue(filter);
}
return filterOp.getBiPredicate().test(leftValue, rightValue);
}
protected Object getValue(IVariableScope scope, String name){
return scope.getValueByPropPath(name);
}
@Override
protected Boolean visitAssertOp(FilterOp filterOp, ITreeBean filter, IVariableScope scope) {
String name = getName(filter);
Object value = getValue(scope, name);
return filterOp.getPredicate().test(value);
}
protected Boolean visitBetweenOp(FilterOp filterOp, ITreeBean filter, IVariableScope scope) {
String name = getName(filter);
Object value = getValue(scope, name);
String minName = getMinName(filter);
String maxName = getMaxName(filter);
Object min;
if (minName != null) {
min = getValue(scope, minName);
} else {
min = getMin(filter);
}
Object max;
if (maxName != null) {
max = getValue(scope, maxName);
} else {
max = getMax(filter);
}
boolean excludeMin = defaults(toBoolean(filter.getAttr(FILTER_ATTR_EXCLUDE_MIN)), false);
boolean excludeMax = defaults(toBoolean(filter.getAttr(FILTER_ATTR_EXCLUDE_MAX)), false);
return filterOp.getBetweenOperator().test(value, min, max, excludeMin, excludeMax);
}
@Override
public Boolean visitAnd(ITreeBean filter, IVariableScope scope) {
List<? extends ITreeBean> children = filter.getChildren();
if (children == null || children.isEmpty())
return true;
for (ITreeBean child : children) {
if (!visit(child, scope))
return false;
}
return true;
}
@Override
public Boolean visitOr(ITreeBean filter, IVariableScope scope) {
List<? extends ITreeBean> children = filter.getChildren();
if (children == null || children.isEmpty())
return true;
for (ITreeBean child : children) {
if (visit(child, scope))
return true;
}
return false;
}
@Override
public Boolean visitNot(ITreeBean filter, IVariableScope scope) {
return !visitAnd(filter, scope);
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/canonical-entropy/nop-entropy.git
git@gitee.com:canonical-entropy/nop-entropy.git
canonical-entropy
nop-entropy
nop-entropy
master

搜索帮助

Cb406eda 1850385 E526c682 1850385