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/StartingNode.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.InvalidGraphNodeException; 34 import name.angoca.zemucan.core.graph.api.ParentStartingNodeException; 35 import name.angoca.zemucan.tools.Constants; 36 37 /** 38 * This token represents the first token in the grammar (the starting node in 39 * the graph.) All the possible ways starts from 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>0.0.7 part equals super.</li> 54 * <li>1.0.0 Moved to version 1.</li> 55 * <li>1.1.0 Exception hierarchy changed.</li> 56 * <li>1.2.0 final.</li> 57 * <li>1.2.1 compareTo -> equals.</li> 58 * <li>1.3.0 addParent.</li> 59 * <li>1.3.1 GraphToken renamed by GraphNode</li> 60 * <li>1.3.2 Token constant renamed to Node.</li> 61 * <li>1.4.0 GrammarReader separated from Graph.</li> 62 * <li>1.5.0 Viibility, getChildren, toString.</li> 63 * <li>1.6.0 GraphNode hierarchy.</li> 64 * <li>1.7.0 Final methods.</li> 65 * </ul> 66 * 67 * @author Andres Gomez Casanova <a 68 * href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a> 69 * @version 1.7.0 2010-09-03 70 */ 71 public final class StartingNode extends BorderNode { 72 73 /** 74 * Constructor that defines the starting node. Its name is STARTING_NODE and 75 * this works as a reserved name token. 76 * 77 * @param graph 78 * Associated graph. 79 * @throws InvalidGraphNodeException 80 * The name is invalid 81 */ 82 StartingNode(final Graph/* ! */graph) throws InvalidGraphNodeException { 83 super(Constants.STARTING_NODE, graph); 84 } 85 86 /** 87 * Always throws an exception because starting node cannot have parents. 88 * 89 * @see name.angoca.zemucan.core.graph.model.AbstractGraphNode#addParent(name.angoca.zemucan.core.graph.model.AbstractGraphNode) 90 */ 91 @Override 92 final void addParent(final AbstractGraphNode token) 93 throws AbstractGraphException { 94 throw new ParentStartingNodeException(); 95 } 96 97 /* 98 * (non-Javadoc) 99 * 100 * @see 101 * name.angoca.zemucan.core.graph.model.AbstractGraphNode#equals(java.lang 102 * .Object) 103 */ 104 @Override 105 public final boolean equals(final Object/* ? */object) { 106 // The equals was designed not to be symmetric with its parent. 107 boolean equals = false; 108 if (object instanceof StartingNode) { 109 final StartingNode token = (StartingNode) object; 110 final List<AbstractGraphNode> ways = token.getChildren(); 111 if ((ways.size() == this.getChildren().size())) { 112 equals = super.equals(token); 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.STARTING_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.STARTING_NODE)) { 139 ret = true; 140 } 141 return ret; 142 } 143 144 /** 145 * Returns a string representation of an starting token. 146 * 147 * @see java.lang.Object#toString() 148 * @return String representation of an starting token. 149 */ 150 @Override 151 public final String/* ! */toString() { 152 String ret = '{' + Constants.STARTING_NODE; 153 if (super.getChildren().size() > 0) { 154 ret += '<'; 155 for (final AbstractGraphNode node : super.getChildren()) { 156 ret += node.getId() + '-'; 157 } 158 ret = ret.substring(0, ret.length() - 1); 159 ret += '>'; 160 } 161 ret += '}'; 162 return ret; 163 } 164 }