diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-01-06 11:43:01 +0100 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-01-06 11:43:01 +0100 |
| commit | c1a240e873cb0e434ea98506d2f33b4a571e354a (patch) | |
| tree | 71c6c90767a01a7b9b473f84ef54834c8f14b581 /src/de/fhswf/in/inf/java1/aufgabe12/BinaryTemplate.java | |
| parent | 44d4f449ee2b072ec62618d6e1cfa2bd419c4f77 (diff) | |
| download | Java1-c1a240e873cb0e434ea98506d2f33b4a571e354a.tar.gz Java1-c1a240e873cb0e434ea98506d2f33b4a571e354a.zip | |
Assignment No.12 first complete implementation.
Diffstat (limited to 'src/de/fhswf/in/inf/java1/aufgabe12/BinaryTemplate.java')
| -rw-r--r-- | src/de/fhswf/in/inf/java1/aufgabe12/BinaryTemplate.java | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/de/fhswf/in/inf/java1/aufgabe12/BinaryTemplate.java b/src/de/fhswf/in/inf/java1/aufgabe12/BinaryTemplate.java new file mode 100644 index 0000000..7aa47c5 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe12/BinaryTemplate.java @@ -0,0 +1,87 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe12; + +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; + } + +} |
