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

com.g2forge.alexandria.expression.numeric.ConditionalNumericExpression Maven / Gradle / Ivy

Go to download

A library for expressions and their evaluation. Includes a basic implementation for math.

There is a newer version: 0.0.18
Show newest version
package com.g2forge.alexandria.expression.numeric;

import com.g2forge.alexandria.expression.eval.IEvaluator;
import com.g2forge.alexandria.java.close.ICloseable;
import com.g2forge.alexandria.java.core.helpers.HObject;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
public class ConditionalNumericExpression extends AActiveNumericExpression {
	protected final INumericExpression condition;

	protected final INumericExpression valueNonZero;

	protected final INumericExpression valueZero;

	@Override
	protected INumericExpression applyInternal(IEvaluator evaluator, NumericEnvironment environment) {
		final INumericExpression condition = evaluator.apply("Condition", getCondition(), environment);
		final INumericExpression valueNonZero = evaluator.apply("NonZero", getValueNonZero(), environment);
		final INumericExpression valueZero = evaluator.apply("Zero", getValueZero(), environment);
		return new ConditionalNumericExpression(condition, valueNonZero, valueZero);
	}

	@Override
	protected INumericExpression reduceInternal(IEvaluator evaluator) {
		final INumericExpression condition = evaluator.reduce("Condition", getCondition()), retVal;
		if (condition instanceof NumericLiteral) {
			final boolean conditionAsBoolean = ((NumericLiteral) condition).getValue() != 0;
			retVal = conditionAsBoolean ? evaluator.reduce("NonZero", getValueNonZero()) : evaluator.reduce("Zero", getValueZero());
		} else {
			final INumericExpression nonZero = evaluator.reduce("NonZero", getValueNonZero());
			final INumericExpression zero = evaluator.reduce("Zero", getValueZero());
			retVal = new ConditionalNumericExpression(condition, nonZero, zero);
		}
		return retVal;
	}

	@Override
	public String toString() {
		try (final ICloseable debug = EVAL.get().debug()) {
			return HObject.toString(this, ConditionalNumericExpression::getCondition, ConditionalNumericExpression::getValueNonZero, ConditionalNumericExpression::getValueZero);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy