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/EndingNode.java $:
28   */
29  package name.angoca.zemucan.core.graph.model;
30  
31  import java.util.List;
32  
33  import name.angoca.zemucan.core.graph.api.ChildEndingNodeException;
34  import name.angoca.zemucan.core.graph.api.InvalidGraphNodeException;
35  import name.angoca.zemucan.tools.Constants;
36  
37  /**
38   * This token represents the last token in the grammar (the ending node in the
39   * graph.) All the possible ways ends in this token.
40   * <p>
41   * This object is immutable, once it is created, their properties cannot be
42   * changed. This is the reason because there are not setter methods.
43   * <p>
44   * <b>Control Version</b>
45   * <p>
46   * <ul>
47   * <li>0.0.1 Class creation.</li>
48   * <li>0.0.2 Recommendations from PMD.</li>
49   * <li>0.0.3 Change constant value.</li>
50   * <li>0.0.4 Starting and ending token as constants.</li>
51   * <li>0.0.5 Equals from super.</li>
52   * <li>0.0.6 Equals different super.</li>
53   * <li>1.0.0 Moved to version 1.</li>
54   * <li>1.1.0 Exception hierarchy changed.</li>
55   * <li>1.2.0 final.</li>
56   * <li>1.2.1 compareTo -> equals.</li>
57   * <li>1.3.0 Add way.</li>
58   * <li>1.3.1 GraphToken renamed by GraphNode</li>
59   * <li>1.3.2 Token constant renamed to Node.</li>
60   * <li>1.4.0 GrammarReader separated from Graph.</li>
61   * <li>1.5.0 AddChildren, toString.</li>
62   * <li>1.6.0 GraphNode hierarchy.</li>
63   * <li>1.7.0 Final methods.</li>
64   * </ul>
65   *
66   * @author Andres Gomez Casanova <a
67   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
68   * @version 1.7.0 2010-09-03
69   */
70  public final class EndingNode extends BorderNode {
71  
72  	/**
73  	 * Constructor that defines the ending node. Its name is ENDING_NODE and
74  	 * this works as a reserved name token..
75  	 *
76  	 * @param graph
77  	 *            Associated graph.
78  	 * @throws InvalidGraphNodeException
79  	 *             The name is invalid.
80  	 */
81  	EndingNode(final Graph/* ! */graph) throws InvalidGraphNodeException {
82  		super(Constants.ENDING_NODE, graph);
83  	}
84  
85  	/**
86  	 * Always throws an exception, because an EndingNode cannot have children.
87  	 *
88  	 * @see name.angoca.zemucan.core.graph.model.AbstractGraphNode#addChild(name.angoca.zemucan.core.graph.model.AbstractGraphNode)
89  	 */
90  	@Override
91  	final void addChild(final AbstractGraphNode/* ! */token)
92  			throws AbstractGraphException {
93  		throw new ChildEndingNodeException();
94  	}
95  
96  	/*
97  	 * (non-Javadoc)
98  	 *
99  	 * @see
100 	 * name.angoca.zemucan.core.graph.model.AbstractGraphNode#equals(java.lang
101 	 * .Object)
102 	 */
103 	@Override
104 	public final boolean equals(final Object/* ? */object) {
105 		// The equals was designed not to be symmetric with its parent.
106 		boolean equals = false;
107 		if (object instanceof EndingNode) {
108 			final EndingNode token = (EndingNode) object;
109 			final List<AbstractGraphNode> ways = token.getChildren();
110 			if ((ways.size() == this.getChildren().size())
111 					&& (this.getChildren().size() == 0)) {
112 				equals = true;
113 			}
114 		}
115 		return equals;
116 	}
117 
118 	/*
119 	 * (non-Javadoc)
120 	 *
121 	 * @see java.lang.Object#hashCode()
122 	 */
123 	@Override
124 	public final int hashCode() {
125 		return Constants.ENDING_NODE.hashCode() * -1;
126 	}
127 
128 	/*
129 	 * (non-Javadoc)
130 	 *
131 	 * @see
132 	 * name.angoca.zemucan.core.graph.model.AbstractGraphNode#represent(java
133 	 * .lang.String)
134 	 */
135 	@Override
136 	public final boolean represent(final String token) {
137 		boolean ret = false;
138 		if (token.equals(Constants.ENDING_NODE)) {
139 			ret = true;
140 		}
141 		return ret;
142 	}
143 
144 	/**
145 	 * Returns a string representation of an ending token.
146 	 *
147 	 * @see java.lang.Object#toString()
148 	 * @return String representation of an ending token.
149 	 */
150 	@Override
151 	public final String/* ! */toString() {
152 		String ret = '{' + Constants.ENDING_NODE;
153 		if (super.getParents().size() > 0) {
154 			ret += '[';
155 			for (final AbstractGraphNode node : super.getParents()) {
156 				ret += node.getId() + '-';
157 			}
158 			ret = ret.substring(0, ret.length() - 1);
159 			ret += ']';
160 		}
161 		ret += '}';
162 		return ret;
163 	}
164 
165 }