summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/upnfx/util/UPN.java
diff options
context:
space:
mode:
authorStefan Suhren <suhren.stefan@fh-swf.de>2015-06-09 09:13:55 +0200
committerStefan Suhren <suhren.stefan@fh-swf.de>2015-06-09 09:13:55 +0200
commitd8cd813ddc54c455eb3071795038b97b72dc1ee9 (patch)
tree3da37c0ec1d4d0f2de6e3e1a981542df8222ae6d /src/de/fhswf/in/inf/upnfx/util/UPN.java
parentf74cd282da1f14bc3d2d768b2f68b8f6d6429e68 (diff)
downloadUpnFx-d8cd813ddc54c455eb3071795038b97b72dc1ee9.tar.gz
UpnFx-d8cd813ddc54c455eb3071795038b97b72dc1ee9.zip
Add working version of UpnFx
Diffstat (limited to 'src/de/fhswf/in/inf/upnfx/util/UPN.java')
-rw-r--r--src/de/fhswf/in/inf/upnfx/util/UPN.java110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/de/fhswf/in/inf/upnfx/util/UPN.java b/src/de/fhswf/in/inf/upnfx/util/UPN.java
new file mode 100644
index 0000000..bc773d5
--- /dev/null
+++ b/src/de/fhswf/in/inf/upnfx/util/UPN.java
@@ -0,0 +1,110 @@
+/**
+ *
+ */
+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);
+ }
+ }
+
+}