1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.eos.analyzer;
17
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import static net.sf.eos.util.Conditions.checkArgumentNotNull;
24
25
26
27
28
29 public abstract class AbstractToken implements Token {
30
31 private final CharSequence text;
32 private final Map<String, List<String>> metadata =
33 new HashMap<String, List<String>>();
34 private final String type;
35
36
37
38
39
40
41
42
43 public AbstractToken(final CharSequence tokenSequence) {
44 this(tokenSequence, DEFAULT_TYPE, Collections.EMPTY_MAP);
45 }
46
47 public AbstractToken(final CharSequence tokenSequence,
48 final Map<String, List<String>> metadata) {
49 this(tokenSequence, DEFAULT_TYPE, metadata);
50 }
51
52 public AbstractToken(final CharSequence tokenSequence, final String type) {
53 this(tokenSequence, type, Collections.EMPTY_MAP);
54 }
55
56
57
58
59
60 public AbstractToken(final Token t) {
61 this(t.getTokenText(), t.getType(), t.getMeta());
62 }
63
64
65
66
67
68
69
70
71 @SuppressWarnings("nls")
72 public AbstractToken(final CharSequence tokenSequence,
73 final String type,
74 final Map<String, List<String>> metadata) {
75 this.text = checkArgumentNotNull(tokenSequence, "tokenSequence is null");
76 this.type = checkArgumentNotNull(type, "type is null");
77 this.metadata.putAll(metadata);
78 }
79
80
81
82
83 public CharSequence getTokenText() {
84 assert this.text != null;
85 return this.text;
86 }
87
88
89
90
91 public String getType() {
92 assert this.type != null;
93 return this.type;
94 }
95
96
97
98
99 public Map<String, List<String>> getMeta() {
100 assert this.metadata != null;
101 return this.metadata;
102 }
103
104 @Override
105 public String toString() {
106 final StringBuilder sb =
107 new StringBuilder(this.getClass().getSimpleName());
108
109 sb.append("[tokenText:");
110 sb.append(getTokenText());
111 sb.append("]");
112
113 return sb.toString();
114 }
115 }