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 23:10:20 -0500 (dom, 06 mar 2011) $:
26   * Revision: $LastChangedRevision: 1917 $:
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/TextualGraphNode.java $:
28   */
29  package name.angoca.zemucan.core.graph.model;
30  
31  import name.angoca.zemucan.core.graph.api.InvalidGraphNodeException;
32  
33  /**
34   * Represents a node that contain a text. However, the text could reference a
35   * reserver or a non-reserved word. This is the reason this class is abstract.
36   * <p>
37   * <b>Control Version</b>
38   * <p>
39   * <ul>
40   * <li>1.0.0 Class creation.</li>
41   * <li>1.1.0 Protected constructor.</li>
42   * </ul>
43   *
44   * @author Andres Gomez Casanova <a
45   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
46   * @version 1.1.0 2010-09-03
47   */
48  public abstract class TextualGraphNode extends AbstractGraphNode {
49  
50  	/**
51  	 * Represents an internal identification of the token. Most of the cases its
52  	 * value id the reserved word that the token represents, however it can also
53  	 * be the name of the variable that represents.
54  	 */
55  	private transient String name;
56  
57  	/**
58  	 * Builds a textual node with the id that represents the node, and the name
59  	 * of the node.
60  	 *
61  	 * @param tokenId
62  	 *            If of the token.
63  	 * @param graphNodeName
64  	 *            Name of the token.
65  	 * @param graph
66  	 *            Associated graph.
67  	 * @throws InvalidGraphNodeException
68  	 *             The name is invalid.
69  	 */
70  	protected TextualGraphNode(final String/* ! */tokenId,
71  			final String /* ! */graphNodeName, final Graph/* ! */graph)
72  			throws InvalidGraphNodeException {
73  		super(tokenId, graph);
74  
75  		assert graphNodeName != null;
76  
77  		if (graphNodeName.equals("")) { //$NON-NLS-1$
78  			throw new InvalidGraphNodeException();
79  		}
80  		this.name = graphNodeName;
81  	}
82  
83  	/*
84  	 * (non-Javadoc)
85  	 *
86  	 * @see
87  	 * name.angoca.zemucan.core.graph.model.AbstractGraphNode#compareTo(name
88  	 * .angoca.zemucan.core.graph.model.AbstractGraphNode)
89  	 */
90  	@Override
91  	public final int compareTo(final AbstractGraphNode/* ? */other) {
92  		int ret = -1;
93  		if (other instanceof TextualGraphNode) {
94  			ret = this.name.compareTo(((TextualGraphNode) other).name);
95  		}
96  		// TODO v1.1 Test if the compare is reflexive
97  		// if (other.compareTo(this) != ret){
98  		// throw new RuntimeException("Different compare value");
99  		// }
100 		return ret;
101 	}
102 
103 	/*
104 	 * (non-Javadoc)
105 	 *
106 	 * @see java.lang.Object#equals(java.lang.Object)
107 	 */
108 	@Override
109 	public boolean equals(final Object/* ? */object) {
110 		boolean equals = false;
111 		if (object instanceof TextualGraphNode) {
112 			final TextualGraphNode token = (TextualGraphNode) object;
113 			if (token.name.equals(this.name)) {
114 				equals = super.equals(object);
115 			}
116 		}
117 		return equals;
118 	}
119 
120 	/**
121 	 * Returns the name of the token.
122 	 *
123 	 * @return the name of the token.
124 	 */
125 	public final String/* ! */getName() {
126 		assert this.name != null;
127 		return this.name;
128 	}
129 
130 	/*
131 	 * (non-Javadoc)
132 	 *
133 	 * @see java.lang.Object#hashCode()
134 	 */
135 	@Override
136 	public int hashCode() {
137 		int superHash = super.hashCode();
138 		if (superHash == 0) {
139 			superHash = 1;
140 		}
141 		final int value = -superHash + this.name.hashCode()
142 				* GraphNode.class.getName().hashCode();
143 		return value;
144 	}
145 
146 	/**
147 	 * Changes the current node name with the given one.
148 	 *
149 	 * @param graphName
150 	 *            New node's name.
151 	 */
152 	protected final void setName(final String/* ! */graphName) {
153 		assert graphName != null;
154 		this.name = graphName;
155 	}
156 
157 }