01: /*
02: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18: package org.mandarax.lib.math;
19:
20: import org.mandarax.kernel.*;
21:
22: /**
23: * Abstract superclass for double arithmetic functions with one argument.
24: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
25: * @version 3.4 <7 March 05>
26: * @since 1.4 (in 1.3 in the renamed package org.mandarax.math)
27: */
28: public abstract class UnaryDoubleArithmeticFunction extends
29: org.mandarax.lib.AbstractFunction {
30:
31: private static Class[] structure = { Double.class };
32:
33: /**
34: * Compute the result using the function.
35: * @return the result
36: * @param d value
37: */
38: protected abstract double compute(double d);
39:
40: /**
41: * Get the return type of the function.
42: * @return the return type of the function
43: */
44: public Class getReturnType() {
45: return Double.class;
46: }
47:
48: /**
49: * Get the structure of the function.
50: * @return the structure of the function
51: */
52: public Class[] getStructure() {
53: return structure;
54: }
55:
56: /**
57: * Perform the function using an array of terms as parameters.
58: * @return the result of the computation
59: * @param parameter an array of terms
60: * @param session a session object
61: * @throws java.lang.UnsupportedOperationException
62: * @throws java.lang.IllegalArgumentException
63: */
64: public Object perform(Term[] parameter, Session session)
65: throws IllegalArgumentException,
66: UnsupportedOperationException {
67: double d;
68:
69: try {
70: d = ((Number) (parameter[0].resolve(session)))
71: .doubleValue();
72: } catch (Exception x) {
73: throw new IllegalArgumentException();
74: }
75:
76: return new Double(compute(d));
77: }
78: }
|