org.unlaxer.tinyexpression.evaluator.javacode.SideEffectStringExpressionBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tinyExpression Show documentation
Show all versions of tinyExpression Show documentation
TinyExpression implemented with Unlaxer
The newest version!
package org.unlaxer.tinyexpression.evaluator.javacode;
import java.util.Iterator;
import java.util.function.Function;
import java.util.function.Supplier;
import org.unlaxer.Token;
import org.unlaxer.parser.Parser;
import org.unlaxer.tinyexpression.evaluator.javacode.SimpleJavaCodeBuilder.Kind;
import org.unlaxer.tinyexpression.parser.BooleanExpression;
import org.unlaxer.tinyexpression.parser.NumberExpression;
import org.unlaxer.tinyexpression.parser.SideEffectStringExpressionParser;
import org.unlaxer.tinyexpression.parser.SideEffectStringExpressionParser.MethodAndParameters;
import org.unlaxer.tinyexpression.parser.StringExpression;
public class SideEffectStringExpressionBuilder implements TokenCodeBuilder {
public static SideEffectStringExpressionBuilder SINGLETON = new SideEffectStringExpressionBuilder();
@Override
public void build(SimpleJavaCodeBuilder builder, Token token ,
TinyExpressionTokens tinyExpressionTokens) {
MethodAndParameters methodAndParameters =
SideEffectStringExpressionParser.extract(token , tinyExpressionTokens);
String methodName = methodAndParameters.classNameAndIdentifier.getIdentifier();
String className = methodAndParameters.classNameAndIdentifier.getClassName();
builder
// java.util.Optional function1 = calculateContext.getObject(
// org.unlaxer.tinyexpression.evaluator.javacode.WhiteListSetter.class);
.setKind(Kind.Function)
.append("java.util.Optional<")
.append(className)
.append("> ")
.appendCurrentFunctionName()
.append(" = calculateContext.getObject(")
.n()
.incTab()
.append(className)
.append(".class);")
.decTab()
.n()
// function1.map(_function->_function.setWhiteList(calculateContext, 1.0f)).orElse(1.0f) :
.setKind(Kind.Calculation)
.appendCurrentFunctionName()
.append(".map(_function->_function.")
.append(methodName)
.append("(calculateContext , ");
ParametersBuilder.buildParameter(builder, methodAndParameters , tinyExpressionTokens);
builder
.append(")).orElse(");
// first parameter is default returning value
StringExpressionBuilder.SINGLETON.build(builder, methodAndParameters.parameterTokens.get(0) ,
tinyExpressionTokens);
builder
.append(")");
;
}
static Supplier sampleSupplier = ()->{System.out.println("");return 10.0f;};
static Function sampleFunction= (x)->{x++;return x;};
public static class ParametersBuilder {
public static ParametersBuilder SINGLETON = new ParametersBuilder();
public static void buildParameter(SimpleJavaCodeBuilder builder, MethodAndParameters methodAndParameters ,
TinyExpressionTokens tinyExpressionTokens) {
Iterator iterator = methodAndParameters.parameterTokens.iterator();
while(iterator.hasNext()) {
Token token = iterator.next();
Parser parser = token.parser;
if(parser instanceof NumberExpression) {
NumberExpressionBuilder.SINGLETON.build(builder, token , tinyExpressionTokens);
}else if(parser instanceof BooleanExpression) {
BooleanExpressionBuilder.SINGLETON.build(builder, token , tinyExpressionTokens);
}else if (parser instanceof StringExpression) {
ExpressionOrLiteral build = StringClauseBuilder.SINGLETON.build(token ,
tinyExpressionTokens);
build.populateTo(builder, Kind.Function);
builder.append(build.toString());
}else {
throw new IllegalArgumentException();
}
if(iterator.hasNext()) {
builder.append(" , ");
}
}
}
static Supplier sampleSupplier = ()->{System.out.println("");return 10.0f;};
static Function sampleFunction= (x)->{x++;return x;};
}
}