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

org.unlaxer.sample.calc.parser.operator.ExpressionOperator Maven / Gradle / Ivy

There is a newer version: 1.1.26
Show newest version
package org.unlaxer.sample.calc.parser.operator;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

import org.unlaxer.Token;
import org.unlaxer.parser.ascii.MinusParser;
import org.unlaxer.parser.ascii.PlusParser;
import org.unlaxer.sample.calc.CalculationContext;

public class ExpressionOperator implements Operator{
	
	public static ExpressionOperator SINGLETON = new ExpressionOperator();

	@Override
	public BigDecimal evaluate(CalculationContext calculationContext , Token token) {
		
		List originalTokens = token.filteredChildren;
		Iterator iterator = originalTokens.iterator();
		
		TermOperator termOperator = TermOperator.SINGLETON;
		
		BigDecimal value = termOperator.evaluate(calculationContext , iterator.next());
		
		while(iterator.hasNext()){
			Token operator = iterator.next();
			BigDecimal operand = termOperator.evaluate(calculationContext , iterator.next());
			if(operator.parser instanceof PlusParser){
				value = value.add(operand);
			}else if(operator.parser instanceof MinusParser){
				value = value.subtract(operand);
			}
		}
		return value;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy