summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/upnfx/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/fhswf/in/inf/upnfx/util')
-rw-r--r--src/de/fhswf/in/inf/upnfx/util/Addition.java34
-rw-r--r--src/de/fhswf/in/inf/upnfx/util/BinaryOperator.java50
-rw-r--r--src/de/fhswf/in/inf/upnfx/util/BinaryTemplate.java87
-rw-r--r--src/de/fhswf/in/inf/upnfx/util/Division.java39
-rw-r--r--src/de/fhswf/in/inf/upnfx/util/Modulo.java39
-rw-r--r--src/de/fhswf/in/inf/upnfx/util/Multiplication.java34
-rw-r--r--src/de/fhswf/in/inf/upnfx/util/ObservableDoubleStack.java40
-rw-r--r--src/de/fhswf/in/inf/upnfx/util/Operator.java23
-rw-r--r--src/de/fhswf/in/inf/upnfx/util/Subtraction.java34
-rw-r--r--src/de/fhswf/in/inf/upnfx/util/UPN.java110
-rw-r--r--src/de/fhswf/in/inf/upnfx/util/UnaryOperator.java47
-rw-r--r--src/de/fhswf/in/inf/upnfx/util/UnaryTemplate.java86
12 files changed, 623 insertions, 0 deletions
diff --git a/src/de/fhswf/in/inf/upnfx/util/Addition.java b/src/de/fhswf/in/inf/upnfx/util/Addition.java
new file mode 100644
index 0000000..f01ee1f
--- /dev/null
+++ b/src/de/fhswf/in/inf/upnfx/util/Addition.java
@@ -0,0 +1,34 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.upnfx.util;
+
+/**
+ * Adds two double values.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public class Addition extends BinaryOperator
+{
+
+ /**
+ * Intentionally empty.
+ *
+ */
+ public Addition()
+ {
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see de.fhswf.in.inf.java1.aufgabe12.BinaryOperator#eval(double, double)
+ */
+ @Override
+ final double eval(double d1, double d2)
+ {
+ return d1 + d2;
+ }
+
+}
diff --git a/src/de/fhswf/in/inf/upnfx/util/BinaryOperator.java b/src/de/fhswf/in/inf/upnfx/util/BinaryOperator.java
new file mode 100644
index 0000000..4f704ee
--- /dev/null
+++ b/src/de/fhswf/in/inf/upnfx/util/BinaryOperator.java
@@ -0,0 +1,50 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.upnfx.util;
+
+
+/**
+ * An abstract class for calculations with unary operators.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public abstract class BinaryOperator implements Operator
+{
+ /*
+ * (non-Javadoc)
+ *
+ * @see de.fhswf.in.inf.java1.aufgabe12.Operator#eval(java.util.Stack)
+ */
+ @Override
+ public final void eval(ObservableDoubleStack stack)
+ {
+ if (stack == null)
+ {
+ throw new IllegalArgumentException("Stack must not be null.");
+ }
+
+ if (stack.size() < 2)
+ {
+ throw new IllegalArgumentException(
+ "Binary operation requires two operand.");
+ }
+
+ double d2 = stack.pop();
+ double d1 = stack.pop();
+
+ stack.push(eval(d1, d2));
+ }
+
+ /**
+ * This method implements the calculation.
+ *
+ * @param d1
+ * The first operator.
+ * @param d2
+ * The second operator.
+ * @return The result of the calculation.
+ */
+ abstract double eval(double d1, double d2);
+}
diff --git a/src/de/fhswf/in/inf/upnfx/util/BinaryTemplate.java b/src/de/fhswf/in/inf/upnfx/util/BinaryTemplate.java
new file mode 100644
index 0000000..6f77dc4
--- /dev/null
+++ b/src/de/fhswf/in/inf/upnfx/util/BinaryTemplate.java
@@ -0,0 +1,87 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.upnfx.util;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+/**
+ * Uses Reflection API to get the method for calculation.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public class BinaryTemplate extends BinaryOperator
+{
+
+ private Method operator;
+
+ /**
+ * Gets the operator function from the Math class.
+ *
+ * @param mathFunction
+ * The name of the operation.
+ */
+ public BinaryTemplate(String mathFunction)
+ {
+ if (mathFunction == null)
+ {
+ throw new IllegalArgumentException("Operation can't be null.");
+ }
+
+ if (mathFunction.isEmpty())
+ {
+ throw new IllegalArgumentException("Operations can't be empty.");
+ }
+
+ try
+ {
+ operator = Math.class.getMethod(mathFunction, Double.TYPE,
+ Double.TYPE);
+ }
+ catch (NoSuchMethodException e)
+ {
+ throw new IllegalArgumentException("Not a valid Operator.", e);
+ }
+ catch (SecurityException e)
+ {
+ throw new IllegalArgumentException("Security violation.", e);
+ }
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see de.fhswf.in.inf.java1.aufgabe12.UnaryOperator#eval(double)
+ */
+ @Override
+ final double eval(double d1, double d2)
+ {
+ Object tmp = null;
+ try
+ {
+ tmp = operator.invoke(null, d1, d2);
+ }
+ catch (IllegalAccessException e)
+ {
+ throw new IllegalStateException("Can't access method.", e);
+ }
+ catch (IllegalArgumentException e)
+ {
+ throw new IllegalStateException(
+ "Argument for Math function is not double.", e);
+ }
+ catch (InvocationTargetException e)
+ {
+ if (e.getCause() instanceof IllegalArgumentException)
+ {
+ throw new IllegalArgumentException("Illegal Argument.", e);
+ }
+ throw new IllegalStateException("Unexpected exception.", e);
+ }
+ return (double) tmp;
+ }
+
+}
diff --git a/src/de/fhswf/in/inf/upnfx/util/Division.java b/src/de/fhswf/in/inf/upnfx/util/Division.java
new file mode 100644
index 0000000..1bcd9fb
--- /dev/null
+++ b/src/de/fhswf/in/inf/upnfx/util/Division.java
@@ -0,0 +1,39 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.upnfx.util;
+
+/**
+ * Divides two double values.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public class Division extends BinaryOperator
+{
+
+ /**
+ * Intentionally empty.
+ *
+ */
+ public Division()
+ {
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see de.fhswf.in.inf.java1.aufgabe12.BinaryOperator#eval(double, double)
+ */
+ @Override
+ final double eval(double d1, double d2)
+ {
+ if (d2 == 0.0)
+ {
+ throw new IllegalArgumentException("Divisor can't be 0.");
+ }
+
+ return d1 / d2;
+ }
+
+}
diff --git a/src/de/fhswf/in/inf/upnfx/util/Modulo.java b/src/de/fhswf/in/inf/upnfx/util/Modulo.java
new file mode 100644
index 0000000..c285d74
--- /dev/null
+++ b/src/de/fhswf/in/inf/upnfx/util/Modulo.java
@@ -0,0 +1,39 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.upnfx.util;
+
+/**
+ * Divides two double values and returns the remainder.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public class Modulo extends BinaryOperator
+{
+
+ /**
+ * Intentionally empty.
+ *
+ */
+ public Modulo()
+ {
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see de.fhswf.in.inf.java1.aufgabe12.BinaryOperator#eval(double, double)
+ */
+ @Override
+ final double eval(double d1, double d2)
+ {
+ if (d2 == 0.0)
+ {
+ throw new IllegalArgumentException("Divisor can't be 0.");
+ }
+
+ return d1 % d2;
+ }
+
+}
diff --git a/src/de/fhswf/in/inf/upnfx/util/Multiplication.java b/src/de/fhswf/in/inf/upnfx/util/Multiplication.java
new file mode 100644
index 0000000..59148ea
--- /dev/null
+++ b/src/de/fhswf/in/inf/upnfx/util/Multiplication.java
@@ -0,0 +1,34 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.upnfx.util;
+
+/**
+ * Multiplies two double values.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public class Multiplication extends BinaryOperator
+{
+
+ /**
+ * Intentionally empty.
+ *
+ */
+ public Multiplication()
+ {
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see de.fhswf.in.inf.java1.aufgabe12.BinaryOperator#eval(double, double)
+ */
+ @Override
+ final double eval(double d1, double d2)
+ {
+ return d1 * d2;
+ }
+
+}
diff --git a/src/de/fhswf/in/inf/upnfx/util/ObservableDoubleStack.java b/src/de/fhswf/in/inf/upnfx/util/ObservableDoubleStack.java
new file mode 100644
index 0000000..8fbcc6d
--- /dev/null
+++ b/src/de/fhswf/in/inf/upnfx/util/ObservableDoubleStack.java
@@ -0,0 +1,40 @@
+/**
+ *
+ */
+
+package de.fhswf.in.inf.upnfx.util;
+
+import javafx.beans.property.SimpleListProperty;
+import javafx.collections.FXCollections;
+
+/**
+ * An observable list that has the Stack methods.
+ *
+ * @author Stefan Suhren
+ * @version 1.0
+ */
+public class ObservableDoubleStack extends SimpleListProperty<Double>
+{
+ public ObservableDoubleStack()
+ {
+ super(FXCollections.observableArrayList());
+ }
+
+ public Double peek()
+ {
+ return get(size() - 1);
+ }
+
+ public Double pop()
+ {
+ Double ret = get(size() - 1);
+ remove(size() - 1);
+ return ret;
+ }
+
+ public void push(Double item)
+ {
+ add(item);
+ }
+
+}
diff --git a/src/de/fhswf/in/inf/upnfx/util/Operator.java b/src/de/fhswf/in/inf/upnfx/util/Operator.java
new file mode 100644
index 0000000..4d1a8c6
--- /dev/null
+++ b/src/de/fhswf/in/inf/upnfx/util/Operator.java
@@ -0,0 +1,23 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.upnfx.util;
+
+
+/**
+ * An interface for on UPN operator.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public interface Operator
+{
+ /**
+ * Gets a stack of doubles and calculates a new value to be put on the
+ * stack.
+ *
+ * @param upnStack
+ * The UPN stack of the calculator.
+ */
+ void eval(ObservableDoubleStack upnStack);
+}
diff --git a/src/de/fhswf/in/inf/upnfx/util/Subtraction.java b/src/de/fhswf/in/inf/upnfx/util/Subtraction.java
new file mode 100644
index 0000000..a4c8677
--- /dev/null
+++ b/src/de/fhswf/in/inf/upnfx/util/Subtraction.java
@@ -0,0 +1,34 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.upnfx.util;
+
+/**
+ * Subtracts two double values.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public class Subtraction extends BinaryOperator
+{
+
+ /**
+ * Intentionally empty.
+ *
+ */
+ public Subtraction()
+ {
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see de.fhswf.in.inf.java1.aufgabe12.BinaryOperator#eval(double, double)
+ */
+ @Override
+ final double eval(double d1, double d2)
+ {
+ return d1 - d2;
+ }
+
+}
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);
+ }
+ }
+
+}
diff --git a/src/de/fhswf/in/inf/upnfx/util/UnaryOperator.java b/src/de/fhswf/in/inf/upnfx/util/UnaryOperator.java
new file mode 100644
index 0000000..be44b16
--- /dev/null
+++ b/src/de/fhswf/in/inf/upnfx/util/UnaryOperator.java
@@ -0,0 +1,47 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.upnfx.util;
+
+
+/**
+ * An abstract class for calculations with unary operators.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public abstract class UnaryOperator implements Operator
+{
+ /*
+ * (non-Javadoc)
+ *
+ * @see de.fhswf.in.inf.java1.aufgabe12.Operator#eval(java.util.Stack)
+ */
+ @Override
+ public final void eval(ObservableDoubleStack stack)
+ {
+ if (stack == null)
+ {
+ throw new IllegalArgumentException("Stack must not be null.");
+ }
+
+ if (stack.size() < 1)
+ {
+ throw new IllegalArgumentException(
+ "Unary operation requires one operand.");
+ }
+
+ double d = stack.pop();
+
+ stack.push(eval(d));
+ }
+
+ /**
+ * This class implements the calculation.
+ *
+ * @param d
+ * The unary operator.
+ * @return The result of the calculation.
+ */
+ abstract double eval(double d);
+}
diff --git a/src/de/fhswf/in/inf/upnfx/util/UnaryTemplate.java b/src/de/fhswf/in/inf/upnfx/util/UnaryTemplate.java
new file mode 100644
index 0000000..671707a
--- /dev/null
+++ b/src/de/fhswf/in/inf/upnfx/util/UnaryTemplate.java
@@ -0,0 +1,86 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.upnfx.util;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+/**
+ * Uses Reflection API to get the method for calculation.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public class UnaryTemplate extends UnaryOperator
+{
+
+ private Method operator;
+
+ /**
+ * Gets the operator function from the Math class.
+ *
+ * @param mathFunction
+ * The name of the operation.
+ */
+ public UnaryTemplate(String mathFunction)
+ {
+ if (mathFunction == null)
+ {
+ throw new IllegalArgumentException("Operation can't be null.");
+ }
+
+ if (mathFunction.isEmpty())
+ {
+ throw new IllegalArgumentException("Operations can't be empty.");
+ }
+
+ try
+ {
+ operator = Math.class.getMethod(mathFunction, Double.TYPE);
+ }
+ catch (NoSuchMethodException e)
+ {
+ throw new IllegalArgumentException("Not a valid Operator.", e);
+ }
+ catch (SecurityException e)
+ {
+ throw new IllegalArgumentException("Security violation.", e);
+ }
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see de.fhswf.in.inf.java1.aufgabe12.UnaryOperator#eval(double)
+ */
+ @Override
+ final double eval(double d)
+ {
+ Object tmp = null;
+ try
+ {
+ tmp = operator.invoke(null, d);
+ }
+ catch (IllegalAccessException e)
+ {
+ throw new IllegalStateException("Can't access method.", e);
+ }
+ catch (IllegalArgumentException e)
+ {
+ throw new IllegalStateException(
+ "Argument for Math function is not double.", e);
+ }
+ catch (InvocationTargetException e)
+ {
+ if (e.getCause() instanceof IllegalArgumentException)
+ {
+ throw new IllegalArgumentException("Illegal Argument.", e);
+ }
+ throw new IllegalStateException("Unexpected exception.", e);
+ }
+ return (double) tmp;
+ }
+
+}