ExpressionLiteral.java

package org.microspace.evaluator;

import java.math.BigDecimal;

/**
 * A literal expression.
 * 
 * @author Gaspar Sinai - {@literal gaspar.sinai@microspace.org}
 * @version 2017-10-16
 */
public class ExpressionLiteral implements Expression {
	
	private static final long serialVersionUID = -6685891984096873580L;
	BigDecimal value;
	boolean needsBracket;
	
	public ExpressionLiteral (BigDecimal value) {
		this.value = value;
	}

	@Override
	public void setVariable(String variable, BigDecimal value, String alias) {
		// nothing to do.
	}
	@Override
	public void clear () {
	}

	@Override
	public BigDecimal calculate(int scale) {
		return value;
	}

	@Override
	public String format(EvaluatorFormat format, int scale, int accuracy) {
		// NO ROUNDING
		//BigDecimal ret = value.round(new MathContext(accuracy, RoundingMode.HALF_UP));
		if (needsBracket) {
			return "( " + value.toString() + " )";
		}
		return value.toString();
	}
	
	@Override
	public void setNeedsBracket(boolean needsBracket) {
		this.needsBracket = needsBracket;
	}

}