01 /*
02 * AnnotationFeatureAccessor - transducer class
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.Annotation;
19 import gate.AnnotationSet;
20
21 /**
22 * Accessor that returns a named feature value.
23 *
24 * @version $Revision$
25 * @author esword
26 */
27 public class AnnotationFeatureAccessor implements AnnotationAccessor {
28
29 protected String featureName;
30
31 public AnnotationFeatureAccessor() {
32 }
33
34 public AnnotationFeatureAccessor(Object key) {
35 setKey(key);
36 }
37
38 /**
39 * Obtain a named feature
40 */
41 public Object getValue(Annotation annot, AnnotationSet context) {
42
43 if(featureName == null || featureName.length() == 00)
44 throw new IllegalStateException("setKey has not been called with "
45 + "the featureName or key was empty");
46
47 if(annot == null || annot.getFeatures() == null) return null;
48
49 return annot.getFeatures().get(featureName);
50 }
51
52 @Override
53 public int hashCode() {
54 return 37 + (featureName != null ? featureName.hashCode() : 0);
55 }
56
57 @Override
58 public boolean equals(Object obj) {
59 if(obj == null) return false;
60 if(obj == this) return true;
61 if(!(this.getClass().equals(obj.getClass()))) return false;
62
63 AnnotationFeatureAccessor a = (AnnotationFeatureAccessor)obj;
64
65 if(featureName != a.getKey() && featureName != null
66 && !a.equals(a.getKey())) return false;
67
68 return true;
69 }
70
71 @Override
72 public String toString() {
73 return featureName;
74 }
75
76 public void setKey(Object key) {
77 if(key != null) featureName = key.toString();
78 }
79
80 public Object getKey() {
81 return featureName;
82 }
83 }
|