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/uiApi/src/main/java/name/angoca/zemucan/interfaze/model/ReturnOptions.java $: 28 */ 29 package name.angoca.zemucan.interfaze.model; 30 31 import name.angoca.zemucan.ParameterNullException; 32 33 /** 34 * This is the object that has the phrase written by the user but completed by 35 * the program, and also the possible options. This object does not contain 36 * anything about business logic, it's just a object that represents the data 37 * between user interface layer and business layer. 38 * <p> 39 * This object is immutable, once it is created, their properties cannot be 40 * changed. This is the reason because there are not setter methods. The getter 41 * methods return a clone copy of the arrays. 42 * <p> 43 * This object returns always a copy of its members. 44 * <p> 45 * <b>Control Version</b> 46 * <p> 47 * <ul> 48 * <li>0.0.1 Class creation.</li> 49 * <li>0.0.2 Recommendations from PMD.</li> 50 * <li>0.0.3 Organized.</li> 51 * <li>0.0.4 Contains and copyArray methods.</li> 52 * <li>0.0.5 copyArray correction.</li> 53 * <li>0.0.6 hashcode correction.</li> 54 * <li>1.0.0 Moved to version 1.</li> 55 * <li>1.0.1 Assert and params verification.</li> 56 * <li>1.0.2 compareTo -> equals.</li> 57 * </ul> 58 * 59 * @author Andres Gomez Casanova <a 60 * href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a> 61 * @version 1.0.2 2009-10-04 62 */ 63 public class ReturnOptions { 64 65 /** 66 * The possible options for the user. 67 */ 68 private final transient String[] options; 69 /** 70 * User's phrase but completed. 71 */ 72 private final transient String phrase; 73 /** 74 * Set of possible way to complete the current phrase. 75 */ 76 private final transient String[] phrases; 77 78 /** 79 * Constructor that associates the possible options for the phrase and the 80 * options. 81 * <p> 82 * This constructor is used when there are several ways to complete the 83 * current phrase, or when there is a complete possibility at the same time 84 * that there are options (create table: create table <tableName> | 85 * create tablespace). 86 * 87 * @param phraseUncomplete 88 * Phrase uncompleted. 89 * @param setOfPhrases 90 * Possible phrases to complete. 91 * @param setOfOptions 92 * Options for the current phrase. 93 * @throws ParameterNullException 94 * When a given parameter is null. 95 */ 96 public ReturnOptions(final String/* ! */phraseUncomplete, 97 final String[]/* [!]! */setOfPhrases, 98 final String[]/* [!]! */setOfOptions) throws ParameterNullException { 99 100 if (phraseUncomplete == null) { 101 throw new ParameterNullException("phrase"); //$NON-NLS-1$ 102 } else if (setOfPhrases == null) { 103 throw new ParameterNullException("phrases"); //$NON-NLS-1$ 104 } else if (setOfOptions == null) { 105 throw new ParameterNullException("options"); //$NON-NLS-1$ 106 } else { 107 for (int i = 0; i < setOfPhrases.length; i += 1) { 108 if (setOfPhrases[i] == null) { 109 throw new ParameterNullException("phrases." + i); //$NON-NLS-1$ 110 } 111 } 112 for (int i = 0; i < setOfOptions.length; i += 1) { 113 if (setOfOptions[i] == null) { 114 throw new ParameterNullException("options." + i); //$NON-NLS-1$ 115 } 116 } 117 } 118 119 this.phrase = phraseUncomplete; 120 this.phrases = setOfPhrases.clone(); 121 this.options = setOfOptions.clone(); 122 } 123 124 /** 125 * Makes a comparison between the elements of two arrays. 126 * 127 * @param obj 128 * Object to compare. 129 * @return true if the options are the same. 130 */ 131 private boolean compareArrays(final ReturnOptions/* ! */obj) { 132 assert obj != null; 133 134 boolean equals = false; 135 int size = this.getOptions().length; 136 if (obj.getOptions().length == size) { 137 equals = true; 138 for (int i = 0; (i < size) && equals; i += 1) { 139 if (!(this.contains(obj.getOptions(), this.getOptions()[i]))) { 140 equals = false; 141 } 142 } 143 } 144 size = this.getPhrases().length; 145 if (equals && (obj.getPhrases().length == size)) { 146 for (int i = 0; (i < size) && equals; i += 1) { 147 if (!(this.contains(obj.getPhrases(), this.getPhrases()[i]))) { 148 equals = false; 149 } 150 } 151 } 152 return equals; 153 } 154 155 /** 156 * Scan the given array and search if the given string exists in the array. 157 * 158 * @param array 159 * Array to scan. 160 * @param string 161 * String to search in the array. 162 * @return True if the string exists in the array. 163 */ 164 private boolean contains(final String[]/* [!]! */array, 165 final String/* ! */string) { 166 assert array != null; 167 boolean assertsEnabled = false; 168 // Intentional side-effect! 169 assert assertsEnabled = true; 170 if (assertsEnabled) { 171 for (final String string2 : array) { 172 assert string2 != null; 173 } 174 } 175 assert string != null; 176 177 boolean ret = false; 178 for (int i = 0; (i < array.length) && !ret; i += 1) { 179 if (array[i].equals(string)) { 180 ret = true; 181 } 182 } 183 return ret; 184 } 185 186 /* 187 * (non-Javadoc) 188 * @see java.lang.Object#equals(java.lang.Object) 189 */ 190 @Override 191 public final boolean equals(final Object/* ? */object) { 192 boolean equals = false; 193 if (object instanceof ReturnOptions) { 194 final ReturnOptions obj = (ReturnOptions) object; 195 if (obj.getPhrase().equals(this.getPhrase())) { 196 equals = this.compareArrays(obj); 197 } 198 } 199 return equals; 200 } 201 202 /** 203 * Returns the set of options of the current command. 204 * 205 * @return Set of options. 206 */ 207 public final String[]/* [!]! */getOptions() { 208 return this.options.clone(); 209 } 210 211 /** 212 * Returns the phrase that has to be print in the user interface. 213 * 214 * @return Phrase to print. 215 */ 216 public final String/* ! */getPhrase() { 217 return this.phrase; 218 } 219 220 /** 221 * Returns the phrase to print in the user interface. 222 * 223 * @return Phrase to print. 224 */ 225 public final String[]/* [!]! */getPhrases() { 226 return this.phrases.clone(); 227 } 228 229 /* 230 * (non-Javadoc) 231 * @see java.lang.Object#hashCode() 232 */ 233 @Override 234 public final int hashCode() { 235 int ret = this.getPhrase().hashCode() / (this.getOptions().length + 1); 236 int size = this.getOptions().length; 237 for (int i = 0; i < size; i += 1) { 238 ret += this.getOptions()[i].hashCode() * -1; 239 } 240 size = this.getPhrases().length; 241 for (int i = 0; i < size; i += 1) { 242 ret += this.getPhrases()[i].hashCode(); 243 } 244 return ret; 245 } 246 247 /** 248 * Returns a string representation of a return options object. The 249 * representation is: [phrase]<(phrases)(phrases)>{[options][options]} 250 * 251 * @see java.lang.Object#toString() 252 * @return String representation of a return options. 253 */ 254 @Override 255 public final String/* ! */toString() { 256 // Phrase auto completed. 257 final StringBuffer ret = new StringBuffer('[' + this.getPhrase() + ']'); 258 // Possible phrases. 259 int size = this.getPhrases().length; 260 if (size > 0) { 261 ret.append('<'); 262 for (int i = 0; i < size; i += 1) { 263 ret.append('(' + this.getPhrases()[i] + ')'); 264 } 265 ret.append('>'); 266 } 267 // Possible options. 268 size = this.getOptions().length; 269 if (size > 0) { 270 ret.append('{'); 271 for (int i = 0; i < size; i += 1) { 272 ret.append('[' + this.getOptions()[i] + ']'); 273 } 274 ret.append('}'); 275 } 276 277 assert ret.toString() != null; 278 return ret.toString(); 279 } 280 }