/** * */ package de.fhswf.in.inf.upnfx.util; /** * A class that calculates in UPN. * * @author $Author: $ * @version $Revision: $, $Date: $ UTC */ public class UPN { private ObservableDoubleStack upnStack = new ObservableDoubleStack(); private Double lastX; /** * Empty constructor. * */ public UPN() { } /** * Reads a string and puts it on the UPN upnStack. * * @param upnString * A space delimited string in UPN notation. */ public final void calculate(Operator operator) { if (operator == null) { throw new IllegalArgumentException("Operator can't be null."); } System.out.println(operator); lastX = upnStack.peek(); operator.eval(upnStack); } public final void addDouble(Double newVal) { if (newVal == null) { throw new IllegalArgumentException("newVal can't be null."); } upnStack.push(newVal); } /** * Get the result of a calculation done before. * * @return The top most value on the UPN stack. */ public final double getResult() { if (upnStack.isEmpty()) { throw new IllegalArgumentException("UPN stack is empty."); } return upnStack.peek(); } /** * Return the ObservableDoubleStack to tie it to the view. * * @return The ObservableDoubleStack with the operands. */ public final ObservableDoubleStack getStack() { return upnStack; } /** * Pushes the previous x onto the stack. * */ public final void setLastX() { if(lastX != null) { upnStack.push(lastX); } } /** * Swap x and y on the stack. * */ public final void swapXY() { if(upnStack.size() > 1) { double f1 = upnStack.pop(); double f2 = upnStack.pop(); upnStack.push(f1); upnStack.push(f2); } } }