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

panda.el.arithmetic.RPN Maven / Gradle / Ivy

package panda.el.arithmetic;

import java.util.LinkedList;
import java.util.Queue;

import panda.el.ELContext;
import panda.el.obj.ELObj;
import panda.el.opt.Operator;

/**
 * 逆波兰表示法(Reverse Polish
 * notation,RPN,或逆波兰记法),是一种是由波兰数学家扬·武卡谢维奇1920年引入的数学表达式方式,在逆波兰记法中,所有操作符置于操作数的后面,因此也被称为后缀表示法。
* 参考:逆波兰表达式 */ public class RPN { // 预编译后的对象 private LinkedList el; public RPN() { } /** * 进行EL的预编译 * @param rpn the RPN Queue */ public RPN(Queue rpn) { compile(rpn); } /** * 执行已经预编译的EL * * @param ec the ElContext * @return the calculated value */ public Object calculate(ELContext ec) { return calculate(ec, el); } /** * 根据逆波兰表达式进行计算 * * @param ec the ElContext * @param rpn the RPN Queue * @return the calculated value */ public Object calculate(ELContext ec, Queue rpn) { LinkedList operand = OperatorTree(rpn); return calculate(ec, operand); } /** * 计算 */ private Object calculate(ELContext ec, LinkedList el2) { Object obj = el2.peek(); if (obj instanceof Operator) { return ((Operator)obj).calculate(ec); } if (obj instanceof ELObj) { return ((ELObj)obj).getObj(ec); } return obj; } /** * 预先编译 * @param rpn the RPN Queue */ public void compile(Queue rpn) { el = OperatorTree(rpn); } /** * 转换成操作树 */ private LinkedList OperatorTree(Queue rpn) { LinkedList operand = new LinkedList(); while (!rpn.isEmpty()) { Object obj = rpn.poll(); if (obj instanceof Operator) { Operator opt = (Operator)obj; opt.wrap(operand); operand.addFirst(opt); continue; } operand.addFirst(obj); } return operand; } }