View Javadoc

1   /*
2    * Zemucan: A Syntax Assistant for DB2
3    * Copyright (C) 2009, 2010 Andres Gomez Casanova
4    *
5    * This file is part of Zemucan.
6    *
7    * Zemucan is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as published by
9    * the Free Software Foundation; either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * Zemucan is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public License
18   * along with this library; if not, see <http://www.gnu.org/licenses/>.
19   *
20   * Contact:
21   * a n g o c a  at  y a h o o  dot  c o m
22   * Cra. 45 No 61 - 31, Bogota, Colombia.
23   *
24   * Author:   $LastChangedBy: angoca $:
25   * Date:     $LastChangedDate: 2011-03-06 09:19:05 -0500 (dom, 06 mar 2011) $:
26   * Revision: $LastChangedRevision: 1910 $:
27   * URL:      $HeadURL: https://zemucan.svn.sourceforge.net/svnroot/zemucan/branches/zemucan_v1/source-code/analyzers/src/main/java/name/angoca/zemucan/core/lexical/model/Token.java $:
28   */
29  package name.angoca.zemucan.core.lexical.model;
30  
31  import name.angoca.zemucan.core.lexical.impl.InvalidTokenException;
32  
33  /**
34   * This is the representation of the decomposition of a phrase in several words
35   * or symbols. Each token is the product of dividing the given phrase and
36   * tokenize it in spaces, parenthesis and single and double quotation marks.
37   * <p>
38   * This object is immutable, once it is created, their properties cannot be
39   * changed. This is the reason because there are not setter methods.
40   * <p>
41   * <b>Control Version</b>
42   * <p>
43   * <ul>
44   * <li>0.0.1 Class creation.</li>
45   * <li>0.0.2 Recommendations from PMD.</li>
46   * <li>1.0.0 Moved to version 1.</li>
47   * <li>1.0.1 equals.</li>
48   * <li>1.0.2 asserts.</li>
49   * <li>1.0.3 compareTo -> equals.</li>
50   * <li>1.6.0 Method renamed.</li>
51   * <li>1.7.0 Is reserved.</li>
52   * </ul>
53   *
54   * @author Andres Gomez Casanova <a
55   *         href="mailto:a n g o c a at y a h o o dot c o m" >(AngocA)</a>
56   * @version 1.7.0 2010-05-14
57   */
58  public class Token implements Cloneable {
59  
60      /**
61       * Represents if the token is a reserved word or a variable name.
62       */
63      private final transient boolean reservedWord;
64      /**
65       * Content that the token represents.
66       */
67      private final transient String token;
68  
69      /**
70       * Constructor of a token, giving the value of it. A token cannot be a empty
71       * string.
72       *
73       * @param tokenName
74       *            Value to represent.
75       * @param isReservedWord
76       *            Indicates if the token is a reserved word in the grammar.
77       * @throws InvalidTokenException
78       *             When trying to create an invalid token.
79       */
80      public Token(final String/* ! */tokenName, final boolean isReservedWord)
81              throws InvalidTokenException {
82          assert tokenName != null;
83  
84          if (tokenName.equals("")) { //$NON-NLS-1$
85              throw new InvalidTokenException();
86          }
87          this.token = tokenName;
88          this.reservedWord = isReservedWord;
89      }
90  
91      @Override
92      public final Token/* ! */clone() throws CloneNotSupportedException {
93          return (Token) super.clone();
94      }
95  
96      /*
97       * (non-Javadoc)
98       * @see java.lang.Object#equals(java.lang.Object)
99       */
100     @Override
101     public final boolean equals(final Object/* ? */object) {
102         boolean equals = false;
103         if (object instanceof Token) {
104             final Token otherToken = (Token) object;
105             if (otherToken.token.equals(this.token)) {
106                 equals = true;
107             }
108         }
109         return equals;
110     }
111 
112     /**
113      * Returns the value that this token represents.
114      *
115      * @return the value that this token represents.
116      */
117     public final String/* ! */getToken() {
118         assert this.token != null;
119         return this.token;
120     }
121 
122     /*
123      * (non-Javadoc)
124      * @see java.lang.Object#hashCode()
125      */
126     @Override
127     public final int hashCode() {
128         final int value = this.getClass().getName().hashCode()
129                 + this.token.hashCode();
130         return value;
131     }
132 
133     /**
134      * Returns true if the token represents a reserved word in the grammar.
135      * False if not.
136      *
137      * @return if the token represents a reserved word
138      */
139     public final boolean isReservedWord() {
140         return this.reservedWord;
141     }
142 
143     /**
144      * Returns a string representation of a token. The representation is:
145      * T[token]
146      *
147      * @see java.lang.Object#toString()
148      * @return String representation of a token.
149      */
150     @Override
151     public final String/* ! */toString() {
152         String ret = "T[" + this.token;
153         String reserved = "|R"; //$NON-NLS-1$
154         if (!this.reservedWord) {
155             reserved = ""; //$NON-NLS-1$
156         }
157         ret += reserved + ']';
158 
159         return ret;
160     }
161 }