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: import org.mandarax.lib.AbstractFunction;
22:
23: /**
24: * Abstract superclass for double arithmetic functions with two arguments.
25: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
26: * @version 3.4 <7 March 05>
27: * @since 1.4 (in 1.3 in the renamed package org.mandarax.math)
28: */
29: public abstract class BinaryDoubleArithmeticFunction extends
30: AbstractFunction {
31:
32: private static Class[] structure = { Double.class, Double.class };
33:
34: /**
35: * Compute the result using the function.
36: * @return the result
37: * @param d1 value 1
38: * @param d2 value 2
39: */
40: protected abstract double compute(double d1, double d2);
41:
42: /**
43: * Get the return type of the function.
44: * @return the return type of the function
45: */
46: public Class getReturnType() {
47: return Double.class;
48: }
49:
50: /**
51: * Get the structure of the function.
52: * @return the structure of the function
53: */
54: public Class[] getStructure() {
55: return structure;
56: }
57:
58: /**
59: * Perform the function using an array of terms as parameters.
60: * @return the result of the computation
61: * @param parameter an array of terms
62: * @param session a session object
63: * @throws java.lang.UnsupportedOperationException
64: * @throws java.lang.IllegalArgumentException
65: */
66: public Object perform(Term[] parameter, Session session)
67: throws IllegalArgumentException,
68: UnsupportedOperationException {
69: double d1, d2;
70:
71: try {
72: d1 = ((Number) (parameter[0].resolve(session)))
73: .doubleValue();
74: d2 = ((Number) (parameter[1].resolve(session)))
75: .doubleValue();
76: } catch (Exception x) {
77: throw new IllegalArgumentException();
78: }
79:
80: return new Double(compute(d1, d2));
81: }
82: }
|