org.unlaxer.tinyexpression.loader.FormulaInfoBlocksParser 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
package org.unlaxer.tinyexpression.loader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.unlaxer.TypedToken;
import org.unlaxer.parser.Parser;
import org.unlaxer.parser.combinator.LazyOneOrMore;
import org.unlaxer.tinyexpression.Calculator;
import org.unlaxer.tinyexpression.loader.model.FormulaInfo;
import org.unlaxer.tinyexpression.loader.model.FormulaInfoList;
import org.unlaxer.util.annotation.TokenExtractor;
public class FormulaInfoBlocksParser extends LazyOneOrMore{
@Override
public Supplier getLazyParser() {
return FormulaInfoBlockParser::new;
}
@Override
public Optional getLazyTerminatorParser() {
return Optional.empty();
}
@TokenExtractor
public FormulaInfoList extract(TypedToken typedToken ,
FormulaInfoAdditionalFields additionalFields, ClassLoader classLoader) {
List> childrenWithParserAsListTyped =
typedToken.getChildrenWithParserAsListTyped(FormulaInfoBlockParser.class);
Map calculatorByName = new HashMap<>();
List collect = childrenWithParserAsListTyped.stream()
.filter(_typeToken->{
boolean hasElement = _typeToken.flatten().stream()
.anyMatch(token->token.parser.getClass() == FormulaInfoElementParser.class);
return hasElement;
})
.map(_typeToken->{
return FormulaInfoParser.extractFormulaInfo(_typeToken , additionalFields , classLoader);
})
.peek(formulaInfo->{
String name = formulaInfo.calculatorName;
Calculator calculator = formulaInfo.calculator();
calculatorByName.put(name,calculator);
})
.collect(Collectors.toList());
collect.forEach(formulaInfo->{
Calculator calculator = formulaInfo.calculator();
String[] split = formulaInfo.dependsOn.split(",");
for (String dependsOnCalculatorName : split) {
if(dependsOnCalculatorName.isBlank()) {
continue;
}
Calculator dependsOncalculator = calculatorByName.get(dependsOnCalculatorName);
calculator.addDependsOn(dependsOncalculator);
}
});
return new FormulaInfoList(collect);
}
}