Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.zodiac.script.expression.aviator.AviatorExpressionBuilder Maven / Gradle / Ivy
package org.zodiac.script.expression.aviator;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.zodiac.script.expression.api.Expression;
import org.zodiac.script.expression.api.ExpressionBuilder;
import org.zodiac.script.expression.api.ExpressionOperator;
import org.zodiac.script.expression.api.SimpleExpression;
import org.zodiac.sdk.toolkit.util.AssertUtil;
public class AviatorExpressionBuilder implements ExpressionBuilder {
private AviatorExpressionBuilder() {
}
@Override
public Expression buildExpression(ExpressionOperator operator, Expression... expressions) {
AssertUtil.notNull(operator, "operator");
AssertUtil.notNull(expressions, "expressions");
assertObjectsNotNull(expressions);
return newExpression(operator.eval(expressions));
}
@Override
public String buildExpression(ExpressionOperator operator, String... expressions) {
AssertUtil.notNull(operator, "operator");
AssertUtil.notNull(expressions, "expressions");
assertObjectsNotBlank(expressions);
return operator.eval(expressions);
}
@Override
public Expression and(Expression left, Expression right) {
assertObjectsNotNull(left, right);
return buildExpression(AviatorExpressionOperator.AND, left, right);
}
@Override
public String and(String left, String right) {
assertObjectsNotBlank(left, right);
return buildExpression(AviatorExpressionOperator.AND, left, right);
}
@Override
public Expression or(Expression left, Expression right) {
assertObjectsNotNull(left, right);
return buildExpression(AviatorExpressionOperator.OR, left, right);
}
@Override
public String or(String left, String right) {
assertObjectsNotBlank(left, right);
return buildExpression(AviatorExpressionOperator.OR, left, right);
}
@Override
public Expression not(Expression expr) {
assertObjectsNotNull(expr);
return buildExpression(AviatorExpressionOperator.NOT, expr);
}
@Override
public String not(String expr) {
assertObjectsNotBlank(expr);
return buildExpression(AviatorExpressionOperator.NOT, expr);
}
@Override
public Expression equalTo(Expression left, Expression right) {
assertObjectsNotNull(left, right);
return buildExpression(AviatorExpressionOperator.EQ, left, right);
}
@Override
public String equalTo(String left, String right) {
assertObjectsNotBlank(left, right);
return buildExpression(AviatorExpressionOperator.EQ, left, right);
}
@Override
public Expression notEqualTo(Expression left, Expression right) {
assertObjectsNotNull(left, right);
return buildExpression(AviatorExpressionOperator.NEQ, left, right);
}
@Override
public String notEqualTo(String left, String right) {
assertObjectsNotBlank(left, right);
return buildExpression(AviatorExpressionOperator.NEQ, left, right);
}
@Override
public Expression greaterThan(Expression left, Expression right) {
assertObjectsNotNull(left, right);
return buildExpression(AviatorExpressionOperator.GT, left, right);
}
@Override
public String greaterThan(String left, String right) {
assertObjectsNotBlank(left, right);
return buildExpression(AviatorExpressionOperator.GT, left, right);
}
@Override
public Expression lessThan(Expression left, Expression right) {
assertObjectsNotNull(left, right);
return buildExpression(AviatorExpressionOperator.LT, left, right);
}
@Override
public String lessThan(String left, String right) {
assertObjectsNotBlank(left, right);
return buildExpression(AviatorExpressionOperator.LT, left, right);
}
@Override
public Expression greaterThanOrEqualTo(Expression left, Expression right) {
assertObjectsNotNull(left, right);
return buildExpression(AviatorExpressionOperator.GE, left, right);
}
@Override
public String greaterThanOrEqualTo(String left, String right) {
assertObjectsNotBlank(left, right);
return buildExpression(AviatorExpressionOperator.GE, left, right);
}
@Override
public Expression lessThanOrEqualTo(Expression left, Expression right) {
assertObjectsNotNull(left, right);
return buildExpression(AviatorExpressionOperator.LE, left, right);
}
@Override
public String lessThanOrEqualTo(String left, String right) {
assertObjectsNotBlank(left, right);
return buildExpression(AviatorExpressionOperator.LE, left, right);
}
@Override
public Expression in(Expression... expressions) {
AssertUtil.assertTrue(AssertUtil.notNull(expressions, "expressions").length >= 2);
assertObjectsNotNull(expressions);
return buildExpression(AviatorExpressionOperator.IN, expressions[0], expressions[1]);
}
@Override
public String in(String... expressions) {
AssertUtil.assertTrue(AssertUtil.notNull(expressions, "expressions").length >= 2);
assertObjectsNotBlank(expressions);
return buildExpression(AviatorExpressionOperator.IN, expressions[0], expressions[1]);
}
@Override
public Expression between(Expression... expressions) {
AssertUtil.assertTrue(AssertUtil.notNull(expressions, "expressions").length >= 3);
assertObjectsNotNull(expressions);
return buildExpression(AviatorExpressionOperator.IN, expressions[0], expressions[1], expressions[2]);
}
@Override
public String between(String... expressions) {
AssertUtil.assertTrue(AssertUtil.notNull(expressions, "expressions").length >= 3);
assertObjectsNotBlank(expressions);
return buildExpression(AviatorExpressionOperator.IN, expressions[0], expressions[1], expressions[2]);
}
@Override
public Expression listMust(Expression... expressions) {
AssertUtil.notEmptyArray(expressions, "expressions");
String joinedExpr = Arrays.stream(expressions).map(e -> String.format("(%s)", e.getExpr())).collect(Collectors.joining(String.format(" %s ", AviatorExpressionOperator.AND.getNativeOperator())));
return new SimpleExpression(joinedExpr);
}
@Override
public String arrayMust(String... expressions) {
AssertUtil.notEmptyArray(expressions, "expressions");
String joinedExpr = Arrays.stream(expressions).map(e -> String.format("(%s)", e)).collect(Collectors.joining(String.format(" %s ", AviatorExpressionOperator.AND.getNativeOperator())));
return joinedExpr;
}
@Override
public Expression listShould(Expression... expressions) {
AssertUtil.notEmptyArray(expressions, "expressions");
String joinedExpr = Arrays.stream(expressions).map(e -> String.format("(%s)", e.getExpr())).collect(Collectors.joining(String.format(" %s ", AviatorExpressionOperator.OR.getNativeOperator())));
return new SimpleExpression(joinedExpr);
}
@Override
public String arrayShould(String... expressions) {
AssertUtil.notEmptyArray(expressions, "expressions");
String joinedExpr = Arrays.stream(expressions).map(e -> String.format("(%s)", e)).collect(Collectors.joining(String.format(" %s ", AviatorExpressionOperator.OR.getNativeOperator())));
return joinedExpr;
}
@Override
public Expression enclose(Expression expr) {
assertObjectsNotNull(expr);
return newExpression(encloseExpression(expr.getExpr()));
}
@Override
public String enclose(String expr) {
assertObjectsNotBlank(expr);
return encloseExpr(expr);
}
private Expression newExpression(Object obj) {
return new SimpleExpression(obj);
}
private Expression encloseExpression(String expr) {
return newExpression(encloseExpr(expr));
}
private String encloseExpr(String expr) {
return String.format("(%s)", expr);
}
private void assertObjectsNotNull(Expression... expressions) {
for (int i = 0; i < expressions.length; i++) {
AssertUtil.notNull(expressions[i], String.format("expressions[%s]", i));
}
}
private void assertObjectsNotBlank(String... expressions) {
for (int i = 0; i < expressions.length; i++) {
AssertUtil.notBlank(expressions[i], String.format("expressions[%s]", i));
}
}
public static AviatorExpressionBuilder builder() {
return new AviatorExpressionBuilder();
}
}