com.g2forge.alexandria.expression.eval.EagerEvaluator 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.ILazyExpression;
import com.g2forge.alexandria.expression.IVariable;
import com.g2forge.alexandria.java.close.ICloseable;
import com.g2forge.alexandria.java.function.IFunction2;
import com.g2forge.alexandria.java.nestedstate.FlagState;
import com.g2forge.alexandria.java.nestedstate.INestedState;
import lombok.Data;
@Data
public class EagerEvaluator, N extends IEnvironment, E extends IExpression> implements IEvaluator {
protected final INestedState lazy = new FlagState();
protected final IEvaluator nested;
@Override
public E apply(E expression, N environment, Supplier supplier) {
final E applied = getNested().apply(expression, environment, supplier);
return reduceAfterApply(applied);
}
@Override
public E apply(String description, E expression, N environment) {
final E applied = getNested().apply(description, expression, environment);
return reduceAfterApply(applied);
}
@Override
public ICloseable debug() {
return getNested().debug();
}
@Override
public T eval(E expression, Class type, Supplier supplier) throws ExpressionNotEvaluableException {
return getNested().eval(expression, type, supplier);
}
@Override
public T eval(String description, E expression, Class type, IFunction2 super E, ? super Class, ? extends T> function) throws ExpressionNotEvaluableException {
return getNested().eval(description, expression, type, function);
}
@Override
public E reduce(E expression, Supplier supplier) {
if (lazy.get() && (expression instanceof ILazyExpression)) return expression;
return getNested().reduce(expression, supplier);
}
@Override
public E reduce(String description, E expression) {
if (lazy.get() && (expression instanceof ILazyExpression)) return expression;
return getNested().reduce(description, expression);
}
protected E reduceAfterApply(final E applied) {
if (applied instanceof ILazyExpression) return applied;
return getNested().reduce(applied, () -> {
try (final ICloseable lazy = this.lazy.open(true)) {
@SuppressWarnings("unchecked")
final E cast = (E) applied.reduce();
return cast;
}
});
}
}