package com.ruoyi.project.business.dto.JEPCustomFunction;

import java.math.BigDecimal;
import java.util.Stack;
import org.nfunk.jep.ParseException;
import org.nfunk.jep.function.PostfixMathCommand;

/**
 * @author jubs
 */
public class MaxFunction extends PostfixMathCommand {

    public MaxFunction() {
        super();
        // 使用参数的数量
        numberOfParameters = -1;
    }

    @Override
    public void run(Stack inStack) throws ParseException {
        //检查栈
        checkStack(inStack);
        if (this.curNumberOfParameters < 1) {
            throw new ParseException("No arguments for Sum");
        } else {
            Object max = inStack.pop();
            for (int i = 0; i < this.curNumberOfParameters - 1; i++) {
                Object maxCompareParam = inStack.pop();
                double p1 = ((Number)maxCompareParam).doubleValue();
                if (max instanceof BigDecimal) {
                    max = Math.max(((BigDecimal)max).doubleValue(), p1);
                } else {
                    max = Math.max((Double)max, p1);
                }
            }
            inStack.push(max);

        }

    }
}
