com.g2forge.alexandria.expression.eval.SimpleEvaluator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ax-expression Show documentation
Show all versions of ax-expression Show documentation
A library for expressions and their evaluation. Includes a basic implementation for math.
package com.g2forge.alexandria.expression.eval;
import java.util.function.Supplier;
import com.g2forge.alexandria.expression.ExpressionNotEvaluableException;
import com.g2forge.alexandria.expression.IEnvironment;
import com.g2forge.alexandria.expression.IExpression;
import com.g2forge.alexandria.expression.IVariable;
import com.g2forge.alexandria.java.close.ICloseable;
import com.g2forge.alexandria.java.function.IFunction2;
public class SimpleEvaluator, N extends IEnvironment, E extends IExpression> implements IEvaluator {
@Override
public E apply(E expression, N environment, Supplier supplier) {
return supplier.get();
}
@SuppressWarnings("unchecked")
@Override
public E apply(String description, E expression, N environment) {
if (expression == null) { throw new NullPointerException(); }
return (E) expression.apply(environment);
}
@Override
public ICloseable debug() {
return () -> {};
}
@Override
public T eval(E expression, Class type, Supplier supplier) throws ExpressionNotEvaluableException {
return supplier.get();
}
@Override
public T eval(String description, E expression, Class type, IFunction2 super E, ? super Class, ? extends T> function) throws ExpressionNotEvaluableException {
return function.apply(expression, type);
}
@Override
public E reduce(E expression, Supplier supplier) {
return supplier.get();
}
@SuppressWarnings("unchecked")
@Override
public E reduce(String description, E expression) {
return (E) expression.reduce();
}
}