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 integer arithmetic functions with one argument.
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 UnaryIntArithmeticFunction extends
30: AbstractFunction {
31:
32: private static Class[] structure = { Integer.class };
33:
34: /**
35: * Compute the result using the function.
36: * @return the result
37: * @param d value
38: */
39: protected abstract int compute(int d);
40:
41: /**
42: * Get the return type of the function.
43: * @return the return type of the function
44: */
45: public Class getReturnType() {
46: return Integer.class;
47: }
48:
49: /**
50: * Get the structure of the function.
51: * @return the structure of the function
52: */
53: public Class[] getStructure() {
54: return structure;
55: }
56:
57: /**
58: * Perform the function using an array of terms as parameters.
59: * @return the result of the computation
60: * @param parameter an array of terms
61: * @param session a session object
62: * @throws java.lang.UnsupportedOperationException
63: * @throws java.lang.IllegalArgumentException
64: */
65: public Object perform(Term[] parameter, Session session)
66: throws IllegalArgumentException,
67: UnsupportedOperationException {
68: int d;
69:
70: try {
71: d = ((Number) (parameter[0].resolve(session))).intValue();
72: } catch (Exception x) {
73: throw new IllegalArgumentException();
74: }
75:
76: return new Integer(compute(d));
77: }
78: }
|