View Javadoc

1   /* Copyright (c) 2008 Sascha Kohlmann
2    *
3    * This program is free software: you can redistribute it and/or modify
4    * it under the terms of the GNU Affero General Public License as published by
5    * the Free Software Foundation, either version 3 of the License, or
6    * (at your option) any later version.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Affero General Public License for more details.
12   *
13   * You should have received a copy of the GNU Affero General Public License
14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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   * Simple implementation for reuse.
27   * @author Sascha Kohlmann
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      /** Creates a new token for the given sequence. {@link #getType()} returns
37       * the {@link Token#DEFAULT_TYPE}.
38       * @param tokenSequence the char sequence of the token. May not be
39       *                      <code>null</code>.
40       * @throws IllegalArgumentException if <em>tokenSequence</em> is
41       *                                  <code>null</code>
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       * Copy constuctor.
58       * @param t the token to copy.
59       */
60      public AbstractToken(final Token t) {
61          this(t.getTokenText(), t.getType(), t.getMeta());
62      }
63  
64      /** Creates a new token for the given sequence for a special <em>type</em>.
65       * @param tokenSequence the char sequence of the token. May not be
66       *                      <code>null</code>.
67       * @param type the type of the token. May not be <code>null</code>.
68       * @throws IllegalArgumentException if <em>tokenSequence</em> or
69       *                                  <em>type</em> are <code>null</code>
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       * @see Token#getTokenText()
82       */
83      public CharSequence getTokenText() {
84          assert this.text != null;
85          return this.text;
86      }
87  
88      /**
89       * @see Token#getType()
90       */
91      public String getType() {
92          assert this.type != null;
93          return this.type;
94      }
95  
96      /**
97       * @see Token#getMeta()
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 }