All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.unlaxer.tinyexpression.instances.TinyExpressionsExecutor Maven / Gradle / Ivy

There is a newer version: 1.4.4
Show newest version
package org.unlaxer.tinyexpression.instances;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.unlaxer.compiler.CompileError;
import org.unlaxer.tinyexpression.CalculationContext;
import org.unlaxer.tinyexpression.Calculator;
import org.unlaxer.tinyexpression.loader.model.FormulaInfo;

public class TinyExpressionsExecutor {
  
  final Consumer> mapConsumer;
  
  
  public TinyExpressionsExecutor() {
    super();
    mapConsumer = x->{};
  }

  public TinyExpressionsExecutor(Consumer> mapConsumer) {
    super();
    this.mapConsumer = mapConsumer;
  }

  public List execute(
      TenantID tenantID,
      CalculationContext calculationContext,
      ResultConsumer resultConsumer , 
      TinyExpressionInstancesCache tinyExpressionInstancesCache ,
      Comparator comparator,
      Predicate passFilter,
      ClassLoader classLoader) {
    
    List list = null ;
    try {
      
      list = tinyExpressionInstancesCache.get(tenantID , comparator , passFilter , classLoader);
      
      return list.stream().map(calculator->{
        
        Object result;
        Throwable throwable;
        try {
          
          calculator.before(calculationContext);
          result = calculator.apply(calculationContext);
          calculator.after(calculationContext);

          FormulaInfo formulaInfo = FormulaInfo.get(calculator);
          
          if(result instanceof Number) {
            resultConsumer.accept(calculationContext,calculator, formulaInfo, (Number) result);
          }else if(result instanceof String) {
            resultConsumer.accept(calculationContext,calculator, formulaInfo, (String) result);
          }else if(result instanceof Boolean) {
            resultConsumer.accept(calculationContext,calculator, formulaInfo, (Boolean) result);
          }else if(result instanceof Object) {
            resultConsumer.accept(calculationContext,calculator, formulaInfo, result);
          }else {
            throw new IllegalArgumentException("no match:" + result.getClass());
          }
          return new CalculationResult(calculator,result,null);
        }catch(Throwable e) {
          throwable = e;
          return new CalculationResult(calculator,null,throwable);
        }
      }).collect(Collectors.toList());
    }catch (Exception e) {
      List _list = list;
      throw new CompileError(e,map->{
        map.put(CompileError.ComileErrorContextKey.calculatorList.name(), _list);
      });
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy