1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.text.interpretation;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* An annotation is a description of a an area of text, with a given class. For example, an annotation for the
*
* @author <a href="mailto:arnebef@yahoo-inc.com">Arne Bergene Fossaa</a>
*/
public class Annotations {
private Span span;
protected Map<String,Object> annotations;
/**
* Adds an annotation to the the the set of annotations.
*/
public void put(String key,Object o) {
if(annotations == null) {
annotations = new HashMap<>();
}
annotations.put(key,o);
}
public Map<String,Object> getMap() {
if(annotations == null) {
return Collections.emptyMap();
} else {
return annotations;
}
}
public Annotations(Span span) {
this.span = span;
}
public Object get(String key) {
return getMap().get(key);
}
/**
* The span that this annotation is for.
*/
public Span getSpan() {
return span;
}
/**
* The text this annotation is for.
*/
public String getSubString() {
return span.getText();
}
/**
* Helper function to get a Double annotation.
* <p>
* This function first checks if the Object in a map is a <code>Number</code>, and then calls doubleValue() on it
* If it is not, then Double.parseDouble() is called on the string representation of the object. If the string
* is not parseable as a double, a NumberFormatException is thrown.
*/
public Double getDouble(String key) {
Object o = getMap().get(key);
if(o instanceof Number) {
return ((Number)o).doubleValue();
} else if(o == null) {
return null;
} else {
return Double.parseDouble(o.toString());
}
}
/**
* Helper function to get a String from the Annotation. This function will simply call <code>toString()</code> on the
* object saved in the Annotation or return null if the object is null;
*/
public String getString(String key) {
Object o = getMap().get(key);
if(o == null) {
return null;
} else {
return o.toString();
}
}
/**
* Helper function to get a Double annotation.
* <p>
* This function first checks if the Object in a map is a <code>Number</code>, and intValue() is called on it.
* If it is not, then Double.parseDouble() is called on the string representation of the object. If the string
* is not parseable as a double, a NumberFormatException is thrown.
*/
public Integer getInteger(String key) {
Object o = getMap().get(key);
if(o == null) {
return null;
} else if(o instanceof Number) {
return ((Number)o).intValue();
} else {
return Integer.parseInt(o.toString());
}
}
/**
* Helper function to get a Boolean annotation.
*/
public Boolean getBoolean(String key) {
Object o = getMap().get(key);
if(o == null || !(o instanceof Boolean)) {
return null;
} else {
return (Boolean) o;
}
}
}
|