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.date;
19:
20: import java.util.Calendar;
21: import org.mandarax.kernel.Term;
22: import org.mandarax.kernel.Session;
23: import org.mandarax.lib.AbstractFunction;
24:
25: /**
26: * Abstract superclass for date functions with two arguments (Calendar, Calendar) returning an Calendar.
27: * @author <A HREF="mailto:adrian.paschke@in.tum.de">Adrian Paschke</A>
28: * @version 3.4 <7 March 05>
29: * @since 3.3.1
30: */
31: public abstract class BinaryDateFunction1 extends AbstractFunction {
32:
33: private static Class[] structure = { Calendar.class, Calendar.class };
34:
35: /**
36: * Get the return type of the function.
37: * @return the return type of the function
38: */
39: public Class getReturnType() {
40: return Integer.class;
41: }
42:
43: /**
44: * Get the structure of the function.
45: * @return the structure of the function
46: */
47: public Class[] getStructure() {
48: return structure;
49: }
50:
51: /**
52: * Perform the function using an array of terms as parameters.
53: * @return the result of the computation
54: * @param parameter an array of terms
55: * @param session a session object
56: * @throws java.lang.UnsupportedOperationException
57: * @throws java.lang.IllegalArgumentException
58: */
59: public Object perform(Term[] parameter, Session session)
60: throws IllegalArgumentException,
61: UnsupportedOperationException {
62: Calendar c1 = null;
63: Calendar c2 = null;
64:
65: try {
66: c1 = (Calendar) (parameter[0].resolve(session));
67: c2 = (Calendar) (parameter[1].resolve(session));
68: } catch (Exception x) {
69: throw new IllegalArgumentException();
70: }
71:
72: return new Integer(compute(c1, c2));
73: }
74:
75: /**
76: * Compute the result using the function.
77: * @return the result
78: * @param c1 value
79: * @param c2 value
80: */
81: protected abstract int compute(Calendar c1, Calendar c2);
82: }
|