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.text;
19:
20: import org.mandarax.kernel.*;
21:
22: /**
23: * Abstract superclass for predicates associating two strings.
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
27: */
28: public abstract class BinaryStringPredicate extends
29: org.mandarax.lib.AbstractPredicate {
30:
31: private static Class[] structure = { String.class, String.class };
32:
33: /**
34: * Get the structure of the predicate.
35: * @return the structure of the predicate
36: */
37: public Class[] getStructure() {
38: return structure;
39: }
40:
41: /**
42: * Perform the function or predicate using an array of terms as parameters.
43: * @return an object
44: * @param parameter an array of terms
45: * @param session a session object
46: * @throws java.lang.UnsupportedOperationException
47: * @throws java.lang.IllegalArgumentException
48: */
49: public Object perform(Term[] parameter, Session session)
50: throws IllegalArgumentException,
51: UnsupportedOperationException {
52: String s1, s2;
53:
54: try {
55: s1 = (String) (parameter[0].resolve(session));
56: s2 = (String) (parameter[1].resolve(session));
57: } catch (Exception x) {
58: throw new IllegalArgumentException();
59: }
60:
61: if (s1 == null) {
62: throw new IllegalArgumentException();
63: }
64:
65: return (evaluate(s1, s2)) ? Boolean.TRUE : Boolean.FALSE;
66: }
67:
68: /**
69: * Evaluate the two values using the predicate.
70: * @return true or false
71: * @param s1 value 1
72: * @param s2 value 2
73: */
74: protected abstract boolean evaluate(String s1, String s2);
75: }
|