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

com.g2forge.alexandria.expression.numeric.NumericFunction 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 java.util.Arrays;
import java.util.List;

import com.g2forge.alexandria.expression.IFunction;

import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;

@Data
@RequiredArgsConstructor
@Accessors(chain = true)
public class NumericFunction implements IFunction, INumericExpression {
	protected INumericExpression expression;

	protected final List parameters;

	public NumericFunction(NumericVariable... parameters) {
		this(Arrays.asList(parameters));
	}

	@Override
	public INumericExpression apply(NumericEnvironment environment) {
		return new LazyNumericExpression(this::getExpressionNonNull, () -> environment.restrict(getParameters()::contains, null), INumericExpression::apply);
	}

	protected INumericExpression getExpressionNonNull() {
		final INumericExpression expression = getExpression();
		if (expression == null) throw new NullPointerException("Expression was not initialized");
		return expression;
	}

	@Override
	public INumericExpression reduce() {
		final INumericExpression expression = getExpressionNonNull(), reduced = expression.reduce();
		if (expression != reduced) return new NumericFunction(getParameters()).setExpression(reduced);;
		return this;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy