View Javadoc
1 /*** 2 * Copyright (c) 2002, CodeStreet LLC. All rights reserved.<p> 3 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following 4 * conditions are met:<p> 5 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 7 * in the documentation and/or other materials provided with the distribution. Neither the name of CodeStreet LLC. nor the 8 * names of its contributors may be used to endorse or promote products derived from this software without specific prior written 9 * permission.<p> 10 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 11 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 12 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 14 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 15 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<p> 16 */ 17 18 package com.codestreet.messageforge; 19 20 /*** 21 * Class to represent a <tt>Hashtable</tt> of <tt>String</tt>. 22 * 23 * @author Jawaid Hakim. 24 */ 25 public class RFldStringHashtable extends RFldHashtable 26 { 27 /*** 28 * Default constructor. 29 */ 30 public RFldStringHashtable() 31 { 32 } 33 34 /*** 35 * Constructor. 36 * 37 * @param name 38 * Field name. 39 * @param fieldId 40 * Field id. Field ids must be either <tt>0</tt> to indicate 41 * that there is no id on the field, or greater. In addition, 42 * field ids must be unique within a messages - no two fields 43 * are allowed to have the same field id. 44 */ 45 public RFldStringHashtable(String name, int fieldId) 46 { 47 super(name, fieldId); 48 } 49 50 /*** 51 * Constructor. 52 * 53 * @param name 54 * Field name. 55 * @param fieldId 56 * Field id. Field ids must be either <tt>0</tt> to indicate 57 * that there is no id on the field, or greater. In addition, 58 * field ids must be unique within a messages - no two fields 59 * are allowed to have the same field id. 60 * @param desc 61 * Field description. 62 */ 63 public RFldStringHashtable(String name, int fieldId, String desc) 64 { 65 super(name, fieldId, desc); 66 } 67 68 /*** 69 * Get field type. 70 * 71 * @return Field type <tt>STRINGHASHTABLE</tt>. 72 * @see RFldType 73 */ 74 public final RFldType getType() 75 { 76 return RFldType.STRINGHASHTABLE; 77 } 78 79 /*** 80 * Check if another field is equal to this field. Equality is defined as 81 * the fields having the same string representation. A short cut that needs 82 * to be fixed for cases where the string representation is rounding off 83 * numbers etc. 84 * 85 * @param anObject 86 * Another field. 87 * @return <tt>true</tt> if another field is equal to this field. 88 * Otherwise, returns <tt>false</tt>. 89 */ 90 public boolean equals(Object anObject) 91 { 92 if (this == anObject) 93 return true; 94 else if (!(anObject instanceof RFldStringHashtable)) 95 return false; 96 97 RFldStringHashtable another = (RFldStringHashtable) anObject; 98 99 if (valSet_ != another.isValSet()) 100 return false; 101 else if (!valSet_) 102 return true; 103 else if (dataObj_.size() != another.size()) 104 return false; 105 else 106 { 107 try 108 { 109 java.util.Hashtable anotherData = another.getValue(); 110 java.util.Enumeration enum = dataObj_.keys(); 111 while (enum.hasMoreElements()) 112 { 113 String key = (String) enum.nextElement(); 114 String thisValue = (String) dataObj_.get(key); 115 String anotherValue = (String) anotherData.get(key); 116 if (anotherValue == null 117 || !thisValue.equals(anotherValue)) 118 return false; 119 } 120 } 121 catch (ClassCastException ex) 122 { 123 return false; 124 } 125 } 126 return true; 127 } 128 129 /*** 130 * Get the number of elements in the hashtable. 131 * 132 * @return Number of elements in the hashtable. If the value is not set 133 * returns <tt>-1</tt>. 134 */ 135 public int size() 136 { 137 if (!valSet_) 138 return -1; 139 140 return dataObj_.size(); 141 } 142 143 /*** 144 * Get an element from the hashtable. 145 * 146 * @param key 147 * Key. 148 * @return Specified element. Returns <tt>null</tt> if the value of the 149 * field is noit set, the key is not found, or if a <tt>ClassCastException</tt> 150 * if thrown. 151 * @see #size() 152 */ 153 public String get(String key) 154 { 155 if (!valSet_) 156 return null; 157 158 try 159 { 160 return (String) dataObj_.get(key); 161 } 162 catch (ClassCastException ex) 163 { 164 return null; 165 } 166 } 167 168 /*** 169 * Returns the hash code value for the field. Since the equals method is 170 * using the comparison of the string representation of the TibrvMsg then 171 * the hashcode is the hashcode of this string. 172 * 173 * @return A hash code value for the field. 174 */ 175 public int hashCode() 176 { 177 int hash = 17; 178 try 179 { 180 if (valSet_) 181 { 182 java.util.Enumeration enum = dataObj_.elements(); 183 while (enum.hasMoreElements()) 184 hash = 31 * hash + enum.nextElement().hashCode(); 185 } 186 } 187 catch (ClassCastException ex) 188 { 189 } 190 191 return hash; 192 } 193 194 /*** 195 * Reset the field value. 196 * 197 * @see #isValSet() 198 */ 199 public void reset() throws FieldValidationException 200 { 201 if (isLocked()) 202 throw new FieldValidationException( 203 "Field " + getName() + " is locked"); 204 205 if (valSet_) 206 { 207 valSet_ = false; 208 dataObj_ = null; 209 } 210 } 211 212 /*** 213 * Set data. 214 * 215 * @param newData 216 * New data. 217 */ 218 public RFld set(Object newData) throws FieldValidationException 219 { 220 if (newData instanceof java.util.Hashtable) 221 return set((java.util.Hashtable) newData); 222 else if (newData != null) 223 throw new FieldValidationException( 224 "Unsupported value type: " + newData.getClass().getName()); 225 else 226 throw new FieldValidationException( 227 "New value is NULL for field: " + getName()); 228 } 229 230 /*** 231 * Set data. 232 * 233 * @param newData 234 * New data. 235 */ 236 public RFld set(java.util.Hashtable newData) 237 throws FieldValidationException 238 { 239 validate(newData); 240 241 if (valSet_) 242 dataObj_.clear(); 243 244 dataObj_ = newData; 245 valSet_ = true; 246 247 return this; 248 } 249 250 /*** 251 * Set the field value from a JDOM element. 252 * 253 * @param elem 254 * Field value as a JDOM element. 255 */ 256 public final RFld set(org.jdom.Element elem) 257 throws FieldValidationException 258 { 259 java.util.List entries = elem.getChildren(); 260 if (entries.size() > 0) 261 { 262 java.util.Hashtable newData = new java.util.Hashtable(); 263 for (java.util.Iterator iter = entries.iterator(); iter.hasNext();) 264 { 265 org.jdom.Element entry = (org.jdom.Element) iter.next(); 266 String key = entry.getAttributeValue(XML_ATTR_NAME); 267 newData.put(key, entry.getAttributeValue(XML_ATTR_VALUE)); 268 } 269 return set(newData); 270 } 271 272 return this; 273 } 274 275 /*** 276 * Validate against constraints. A field is valid if either it's value is 277 * set and satisfies all constraints, or the the field is optional. 278 */ 279 public void validate() throws FieldValidationException 280 { 281 // Only need to check that non-optional fields have been set. If a 282 // field has been set then it must be valid since validation is done 283 // with each set. 284 if (!valSet_ && !optional_) 285 throw new FieldValidationException("Field not set: " + getName()); 286 } 287 288 /*** 289 * Validate against constraints. A field is valid if either it's value is 290 * set and satisfies all constraints, or the the field is optional. 291 * 292 * @param newData 293 * New data value. 294 */ 295 public void validate(java.util.Hashtable newData) 296 throws FieldValidationException 297 { 298 if (locked_) 299 throw new FieldValidationException( 300 "Cannot modify locked field: " + getName()); 301 302 if (newData == null) 303 throw new FieldValidationException( 304 "New value is NULL for field: " + getName()); 305 ; 306 } 307 308 /*** 309 * Get data. 310 * 311 * @return data Data. If the value of the field has not been set then <tt>null</tt> 312 * is returned. 313 */ 314 public java.util.Hashtable getValue() 315 { 316 return dataObj_; 317 } 318 319 /*** 320 * Get the field value as an object. 321 * 322 * @return Field value as an object. Reference to data is passed out so be 323 * very careful about modifying the data. If the value of the field 324 * has not been set then <tt>null</tt> is returned. 325 */ 326 public Object getValueAsObject() 327 { 328 return dataObj_; 329 } 330 331 /*** 332 * Get the field value as a string. Throws an exception if the field value 333 * is not set. 334 * 335 * @return Field value as a string. Returns <tt>null</tt> if the field 336 * value is not set, or if a <tt>ClassCastException</tt> is 337 * thrown. 338 */ 339 public String getValueAsString() 340 { 341 if (!valSet_) 342 return null; 343 344 StringBuffer buf = new StringBuffer((80 * dataObj_.size()) + 1); 345 buf.append(RMsg.NESTED_FIELD_START); 346 347 try 348 { 349 int i = 0; 350 java.util.Enumeration enum = dataObj_.keys(); 351 while (enum.hasMoreElements()) 352 { 353 if (++i > 1) 354 buf.append(RFldHashtable.ELEMENT_DELIMITER).append(' '); 355 356 String fldName = (String) enum.nextElement(); 357 buf.append(fldName).append(RMsg.FIELD_EQUAL).append( 358 (String) dataObj_.get(fldName)); 359 } 360 return buf.append(RMsg.NESTED_FIELD_END).toString(); 361 } 362 catch (ClassCastException ex) 363 { 364 return null; 365 } 366 } 367 368 /*** 369 * Get the field value as a <tt>java.util.Hashtable</tt>. Throws an 370 * exception if the field value is not set. 371 * 372 * @return Field value as a <tt>java.util.Hashtable</tt>. 373 */ 374 public java.util.Hashtable getValueAsHashtable() 375 throws FieldValidationException 376 { 377 if (!valSet_) 378 throw new FieldValidationException("Field not set: " + getName()); 379 380 return dataObj_; 381 } 382 383 /*** 384 * Get the XML tag for this field type. 385 * 386 * @return XML tag for this field type. 387 */ 388 public final String getTag() 389 { 390 return XML_TAG; 391 } 392 393 /*** 394 * Set the XML tag for this field type. 395 * 396 * @param tag 397 * New XML tag for this field type. 398 */ 399 public static void setTag(String tag) 400 { 401 XML_TAG = tag; 402 } 403 404 /*** 405 * XML tag for this element type. 406 */ 407 static transient String XML_TAG = "strhashtbl"; 408 409 /*** 410 * Data. 411 */ 412 protected java.util.Hashtable dataObj_; 413 }

This page was automatically generated by Maven