AbstractConstraintPredicate.java
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.*;
19 import gate.jape.JapeException;
20 
21 /**
22  * Base class for most {@link ConstraintPredicate}s. Contains standard
23  * getters/setters and other routines.
24  *
25  @version $Revision$
26  @author esword
27  */
28 public abstract class AbstractConstraintPredicate implements
29                                                  ConstraintPredicate {
30   protected AnnotationAccessor accessor;
31   protected Object value;
32 
33   public AbstractConstraintPredicate() {
34   }
35 
36   public AbstractConstraintPredicate(AnnotationAccessor accessor, Object value) {
37     setAccessor(accessor);
38     setValue(value);
39   }
40 
41   public int hashCode() {
42     int hashCode = getOperator().hashCode();
43     hashCode = 37 * hashCode + ((accessor != null? accessor.hashCode() 0);
44     hashCode = 37 * hashCode + ((value != null? value.hashCode() 0);
45     return hashCode;
46   }
47 
48   public boolean equals(Object obj) {
49     if(obj == nullreturn false;
50     if(obj == thisreturn true;
51     if(!(this.getClass().equals(obj.getClass()))) return false;
52 
53     ConstraintPredicate a = (ConstraintPredicate)obj;
54 
55     if(accessor != a.getAccessor() && accessor != null
56             && !accessor.equals(a.getAccessor())) return false;
57 
58     if(value != a.getValue() && value != null && !value.equals(a.getValue()))
59       return false;
60 
61     return true;
62   }
63 
64   public String toString() {
65     // If value is a String, quote it. Otherwise (for things like
66     // Numbers), don't.
67     Object val = getValue();
68     if(val instanceof Stringval = "\"" + val + "\"";
69 
70     return accessor + getOperator() + val;
71   }
72 
73   public boolean matches(Annotation annot, AnnotationSet contextthrows JapeException {
74     //get the appropriate value using the accessor and then have
75     //concrete subclasses do the eval
76     return doMatch(accessor.getValue(annot, context), context);
77   }
78 
79   protected abstract boolean doMatch(Object value, AnnotationSet context)
80           throws JapeException;
81 
82 
83 
84   public void setAccessor(AnnotationAccessor accessor) {
85     this.accessor = accessor;
86   }
87 
88   public AnnotationAccessor getAccessor() {
89     return accessor;
90   }
91 
92   public void setValue(Object value) {
93     this.value = value;
94   }
95 
96   public Object getValue() {
97     return value;
98   }
99 }