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 integer arithmetic predicates with two arguments.
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 BinaryIntArithmeticPredicate extends
29: org.mandarax.lib.AbstractPredicate {
30:
31: private static Class[] structure = { Integer.class, Integer.class };
32:
33: /**
34: * Evaluate the two values using the predicate.
35: * @return true or false
36: * @param d1 value 1
37: * @param d2 value 2
38: */
39: protected abstract boolean evaluate(int d1, int d2);
40:
41: /**
42: * Get the structure of the predicate.
43: * @return the structure of the predicate
44: */
45: public Class[] getStructure() {
46: return structure;
47: }
48:
49: /**
50: * Perform the function or predicate using an array of terms as parameters.
51: * @return an object
52: * @param parameter org.mandarax.kernel.Term[]
53: * @param session a session object
54: * @throws java.lang.UnsupportedOperationException
55: * @throws java.lang.IllegalArgumentException
56: */
57: public Object perform(Term[] parameter, Session session)
58: throws IllegalArgumentException,
59: UnsupportedOperationException {
60: int d1, d2;
61:
62: try {
63: d1 = ((Number) (parameter[0].resolve(session))).intValue();
64: d2 = ((Number) (parameter[1].resolve(session))).intValue();
65: } catch (Exception x) {
66: throw new IllegalArgumentException();
67: }
68:
69: return (evaluate(d1, d2)) ? Boolean.TRUE : Boolean.FALSE;
70: }
71: }
|