diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-06-09 09:13:55 +0200 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-06-09 09:13:55 +0200 |
| commit | d8cd813ddc54c455eb3071795038b97b72dc1ee9 (patch) | |
| tree | 3da37c0ec1d4d0f2de6e3e1a981542df8222ae6d /src/de/fhswf/in/inf/upnfx/util/UnaryTemplate.java | |
| parent | f74cd282da1f14bc3d2d768b2f68b8f6d6429e68 (diff) | |
| download | UpnFx-d8cd813ddc54c455eb3071795038b97b72dc1ee9.tar.gz UpnFx-d8cd813ddc54c455eb3071795038b97b72dc1ee9.zip | |
Add working version of UpnFx
Diffstat (limited to 'src/de/fhswf/in/inf/upnfx/util/UnaryTemplate.java')
| -rw-r--r-- | src/de/fhswf/in/inf/upnfx/util/UnaryTemplate.java | 86 |
1 files changed, 86 insertions, 0 deletions
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; + } + +} |
