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 one argument returning an integer.
27: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
28: * @version 3.4 <7 March 05>
29: * @since 1.4
30: */
31: public abstract class UnaryDateFunction1 extends AbstractFunction {
32:
33: private static Class[] structure = { 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 c = null;
63:
64: try {
65: c = (Calendar) (parameter[0].resolve(session));
66: } catch (Exception x) {
67: throw new IllegalArgumentException();
68: }
69:
70: return new Integer(compute(c));
71: }
72:
73: /**
74: * Compute the result using the function.
75: * @return the result
76: * @param c value
77: */
78: protected abstract int compute(Calendar c);
79: }
|