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/graph/src/main/java/name/angoca/zemucan/core/graph/model/GraphNode.java $:
28 */
29 package name.angoca.zemucan.core.graph.model;
30
31 import name.angoca.zemucan.core.graph.api.InvalidGraphNodeException;
32
33 /**
34 * This is the representation of a word in the grammar graph. A token can be a
35 * reserved word, a parenthesis, a comma, or a variable word, like a name or a
36 * number.
37 * <p>
38 * The grammar is represented by a graph, and this graph has the possibles ways
39 * that a command can have.
40 * <p>
41 * A token has the following elements:
42 * <ul>
43 * <li>id</li>
44 * <li>reservedWord</li>
45 * <li>ways</li>
46 * </ul>
47 * The name attribute represents the internal identification of the token, and
48 * its content can be the reserved word that it represents, or the name of the
49 * variable. Then, the reservedWord attribute indicates if the id is a
50 * reservedWord or not. This is used when scanning the graph.
51 * <p>
52 * Two nodes are the same if the data that they have is the same, and the ways
53 * that they have are the same.
54 * <p>
55 * This object is immutable, once it is created, their properties cannot be
56 * changed. This is the reason because there are not setter methods. The public
57 * getter method returns a copy of the object.
58 * <p>
59 * <b>Control Version</b>
60 * <p>
61 * <ul>
62 * <li>0.0.1 Class creation.</li>
63 * <li>0.0.2</li>
64 * <li>0.1.0 Recommendations from PMD.</li>
65 * <li>0.1.1 Organized.</li>
66 * <li>0.2.0 Comparison not sorted.</li>
67 * <li>0.2.1 Change the Id attribute by Name.</li>
68 * <li>0.2.2 isReservedWord and id as an attribute.</li>
69 * <li>0.2.3 Attribute renamed.</li>
70 * <li>0.2.4 equals reduced in complexity.</li>
71 * <li>0.2.5 Variable name and final.</li>
72 * <li>1.0.0 Moved to version 1.</li>
73 * <li>1.1.0 Exception hierarchy changed.</li>
74 * <li>1.1.1 equals.</li>
75 * <li>1.1.2 asserts and logger.</li>
76 * <li>1.1.3 compareTo -> equals.</li>
77 * <li>1.2.0 Setter for name.</li>
78 * <li>1.3.0 Parent.</li>
79 * <li>1.3.1 Exception param.</li>
80 * <li>1.3.2 GraphToken renamed by GraphNode</li>
81 * <li>1.4.0 GrammarReader separated from Graph.</li>
82 * <li>1.5.0 Children instead of Ways; comparable, remove parent and child.</li>
83 * <li>1.5.1 Copy of the objects.</li>
84 * <li>1.6.0 isReserved public.</li>
85 * <li>1.6.1 Hashcode changed.</li>
86 * <li>1.7.0 GraphNode hierarchy.</li>
87 * <li>1.8.0 Final methods.</li>
88 * </ul>
89 *
90 * @author Andres Gomez Casanova <a
91 * href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
92 * @version 1.8.0 2010-09-03
93 */
94 public class GraphNode extends TextualGraphNode {
95
96 /**
97 * Constructor that defines a token object. The name is the name of the
98 * token that this token object represents, and reservedWord says if the
99 * node is a reserved word or a variable name.
100 * <p>
101 * PRE: The token does not have any delimiter, it just have the token.
102 *
103 * @param graphNodeName
104 * Name of the token.
105 * @param graph
106 * Graph.
107 * @throws InvalidGraphNodeException
108 * The name is invalid.
109 */
110 GraphNode(final String/* ! */graphNodeName, final Graph/* ! */graph)
111 throws InvalidGraphNodeException {
112 this(graphNodeName, graphNodeName, graph);
113 }
114
115 /**
116 * Constructor that defines a token object. The id is the unique identifier
117 * of this token. The name is the name of the token that this token object
118 * represents, and reservedWord says if the node is a reserved word or a
119 * variable name.
120 * <p>
121 * PRE: The token does not have any delimiter, it just have the token.
122 *
123 * @param tokenId
124 * Unique id of the token.
125 * @param graphNodeName
126 * Name of the token.
127 * @param graph
128 * Associated graph.
129 * @throws InvalidGraphNodeException
130 * The name is invalid.
131 */
132 GraphNode(final String/* ! */tokenId, final String /* ! */graphNodeName,
133 final Graph/* ! */graph) throws InvalidGraphNodeException {
134 super(tokenId, graphNodeName, graph);
135 }
136
137 /*
138 * (non-Javadoc)
139 * @see java.lang.Object#equals(java.lang.Object)
140 */
141 @Override
142 public final boolean equals(final Object/* ? */object) {
143 boolean equals = false;
144 if (object instanceof GraphNode) {
145 equals = super.equals(object);
146 }
147 return equals;
148 }
149
150 /*
151 * (non-Javadoc)
152 * @see java.lang.Object#hashCode()
153 */
154 @Override
155 public final int hashCode() {
156 int superHash = super.hashCode();
157 if (superHash == 0) {
158 superHash = 1;
159 }
160 final int value = superHash + GraphNode.class.getName().hashCode();
161 return value;
162 }
163
164 /**
165 * Analyzes the given token, and returns true if the current graph token
166 * represents that token.
167 *
168 * @param token
169 * token to analyze.
170 * @return true if the graph node represents that token.
171 */
172 @Override
173 public final boolean represent(final String/* ! */token) {
174 assert token != null;
175
176 boolean represents = false;
177 if (super.getName().compareToIgnoreCase(token) == 0) {
178 represents = true;
179 }
180 return represents;
181 }
182
183 /**
184 * Returns a string representation of a graph token. The representation is:
185 * <ul>
186 * <li>For a non word reserved token: {name}</li>
187 * <li>For a word reserver token: {name|R}</li>
188 * </ul>
189 *
190 * @see java.lang.Object#toString()
191 * @return String representation of a graph token.
192 */
193 @Override
194 public final String/* ! */toString() {
195 final String ret = super.getName() + "|R::" + super.toString();
196
197 return ret;
198 }
199 }