01 /*
02 * Constraint Predicate implementation
03 *
04 * Copyright (c) 1995-2010, The University of Sheffield. See the file
05 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
06 *
07 * This file is part of GATE (see http://gate.ac.uk/), and is free
08 * software, licenced under the GNU Library General Public License,
09 * Version 2, June 1991 (in the distribution as file licence.html,
10 * and also available at http://gate.ac.uk/gate/licence.html).
11 *
12 * Eric Sword, 03/09/08
13 *
14 * $Id$
15 */
16 package gate.jape.constraint;
17
18 import gate.AnnotationSet;
19 import gate.jape.JapeException;
20
21 public class EqualPredicate extends AbstractConstraintPredicate {
22
23 public String getOperator() {
24 return EQUAL;
25 }
26
27 public boolean doMatch(Object annotValue, AnnotationSet context)
28 throws JapeException {
29
30 if(value == null && annotValue != null) return false;
31
32 if(value.equals(annotValue)) return true;
33
34 /*
35 * The stored value can be String/Long/Double/Boolean. The passed
36 * value must be of the same type, otherwise equals will return
37 * false. In that case, let's suppose the annot's attrib. is a
38 * String and let's try to convert it to the same type as the
39 * constraint.
40 */
41 if(annotValue instanceof String && !(value instanceof String)) {
42 String annotValueString = (String)annotValue;
43
44 try {
45 if(value instanceof Long)
46 return value.equals(Long.valueOf(annotValueString));
47
48 if(value instanceof Double)
49 return value.equals(Double.valueOf(annotValueString));
50
51 if(value instanceof Boolean)
52 return value.equals(Boolean.valueOf(annotValueString));
53
54 // if we reach that point, it means constraint has an unexpected
55 // type!
56 throw new JapeException("Cannot compare values for attribute '"
57 + getAccessor() + "' because cannot compare '" + value
58 + "' to '" + annotValue + "'.");
59 }
60 catch(NumberFormatException otherType) {
61 // annotValue is a String and cannot be converted to
62 // Long/Double/Boolean, so the two cannot be equal
63 return false;
64 }
65 } // if String
66
67 return false;
68 }
69
70 }
|