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 import java.util.List; 21 22 /*** 23 * Class to represent a <tt>List</tt> of <tt>String</tt>. 24 * 25 * @author Jawaid Hakim. 26 */ 27 public class RFldStringList extends RFldList 28 { 29 /*** 30 * Default constructor. 31 */ 32 public RFldStringList() 33 { 34 } 35 36 /*** 37 * Constructor. 38 * 39 * @param name 40 * Field name. 41 * @param fieldId 42 * Field id. Field ids must be either <tt>0</tt> to indicate 43 * that there is no id on the field, or greater. In addition, 44 * field ids must be unique within a messages - no two fields 45 * are allowed to have the same field id. 46 */ 47 public RFldStringList(String name, int fieldId) 48 { 49 super(name, fieldId); 50 } 51 52 /*** 53 * Constructor. 54 * 55 * @param name 56 * Field name. 57 * @param fieldId 58 * Field id. Field ids must be either <tt>0</tt> to indicate 59 * that there is no id on the field, or greater. In addition, 60 * field ids must be unique within a messages - no two fields 61 * are allowed to have the same field id. 62 * @param desc 63 * Field description. 64 */ 65 public RFldStringList(String name, int fieldId, String desc) 66 { 67 super(name, fieldId, desc); 68 } 69 70 /*** 71 * Get field type. 72 * 73 * @return Field type <tt>STRINGHASHTABLE</tt>. 74 * @see RFldType 75 */ 76 public final RFldType getType() 77 { 78 return RFldType.STRINGLIST; 79 } 80 81 /*** 82 * Check if another field is equal to this field. Equality is defined as 83 * the fields having the same string representation. A short cut that needs 84 * to be fixed for cases where the string representation is rounding off 85 * numbers etc. 86 * 87 * @param anObject 88 * Another field. 89 * @return <tt>true</tt> if another field is equal to this field. 90 * Otherwise, returns <tt>false</tt>. 91 */ 92 public boolean equals(Object anObject) 93 { 94 if (this == anObject) 95 return true; 96 else if (!(anObject instanceof RFldStringList)) 97 return false; 98 99 RFldStringList another = (RFldStringList) anObject; 100 101 if (valSet_ != another.isValSet()) 102 return false; 103 else if (!valSet_) 104 return true; 105 else if (dataObj_.size() != another.size()) 106 return false; 107 else 108 { 109 try 110 { 111 java.util.List anotherData = another.getValue(); 112 for (int i = 0; i < dataObj_.size(); ++i) 113 { 114 String thisValue = (String) dataObj_.get(i); 115 String anotherValue = (String) anotherData.get(i); 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 * Returns the hash code value for the field. Since the equals method is 145 * using the comparison of the string representation of the TibrvMsg then 146 * the hashcode is the hashcode of this string. 147 * 148 * @return A hash code value for the field. 149 */ 150 public int hashCode() 151 { 152 int hash = 17; 153 if (valSet_) 154 { 155 for (int i = 0; i < dataObj_.size(); ++i) 156 hash = 31 * hash + dataObj_.get(i).hashCode(); 157 } 158 return hash; 159 } 160 161 /*** 162 * Reset the field value. 163 * 164 * @see #isValSet() 165 */ 166 public void reset() throws FieldValidationException 167 { 168 if (isLocked()) 169 throw new FieldValidationException( 170 "Field " + getName() + " is locked"); 171 172 if (valSet_) 173 { 174 valSet_ = false; 175 dataObj_ = null; 176 } 177 } 178 179 /*** 180 * Set data. 181 * 182 * @param newData 183 * New data. 184 */ 185 public RFld set(Object newData) throws FieldValidationException 186 { 187 if (newData instanceof java.util.List) 188 return set((java.util.List) newData); 189 else if (newData != null) 190 throw new FieldValidationException( 191 "Unsupported value type: " + newData.getClass().getName()); 192 else 193 throw new FieldValidationException( 194 "New value is NULL for field: " + getName()); 195 } 196 197 /*** 198 * Set data. 199 * 200 * @param newData 201 * New data. 202 */ 203 public RFld set(java.util.List newData) throws FieldValidationException 204 { 205 validate(newData); 206 207 if (valSet_) 208 dataObj_.clear(); 209 210 dataObj_ = newData; 211 valSet_ = true; 212 213 return this; 214 } 215 216 /*** 217 * Set the field value from a JDOM element. 218 * 219 * @param elem 220 * Field value as a JDOM element. 221 */ 222 public final RFld set(org.jdom.Element elem) 223 throws FieldValidationException 224 { 225 java.util.List entries = elem.getChildren(); 226 if (entries.size() > 0) 227 { 228 java.util.Hashtable newData = new java.util.Hashtable(); 229 for (java.util.Iterator iter = entries.iterator(); iter.hasNext();) 230 { 231 org.jdom.Element entry = (org.jdom.Element) iter.next(); 232 String key = entry.getAttributeValue(XML_ATTR_NAME); 233 newData.put(key, entry.getAttributeValue(XML_ATTR_VALUE)); 234 } 235 return set(newData); 236 } 237 238 return this; 239 } 240 241 /*** 242 * Validate against constraints. A field is valid if either it's value is 243 * set and satisfies all constraints, or the the field is optional. 244 */ 245 public void validate() throws FieldValidationException 246 { 247 // Only need to check that non-optional fields have been set. If a 248 // field has been set then it must be valid since validation is done 249 // with each set. 250 if (!valSet_ && !optional_) 251 throw new FieldValidationException("Field not set: " + getName()); 252 } 253 254 /*** 255 * Validate against constraints. A field is valid if either it's value is 256 * set and satisfies all constraints, or the the field is optional. 257 * 258 * @param newData 259 * New data value. 260 */ 261 public void validate(java.util.List newData) 262 throws FieldValidationException 263 { 264 if (locked_) 265 throw new FieldValidationException( 266 "Cannot modify locked field: " + getName()); 267 268 if (newData == null) 269 throw new FieldValidationException( 270 "New value is NULL for field: " + getName()); 271 ; 272 } 273 274 /*** 275 * Get data. 276 * 277 * @return data Data. If the value of the field has not been set then <tt>null</tt> 278 * is returned. 279 */ 280 public java.util.List getValue() 281 { 282 return dataObj_; 283 } 284 285 /*** 286 * Get the field value as an object. 287 * 288 * @return Field value as an object. Reference to data is passed out so be 289 * very careful about modifying the data. If the value of the field 290 * has not been set then <tt>null</tt> is returned. 291 */ 292 public Object getValueAsObject() 293 { 294 return dataObj_; 295 } 296 297 /*** 298 * Get the field value as a string. Throws an exception if the field value 299 * is not set. 300 * 301 * @return Field value as a string. Returns <tt>null</tt> if the field 302 * value is not set, or if a <tt>ClassCastException</tt> is 303 * thrown. 304 */ 305 public String getValueAsString() 306 { 307 if (!valSet_) 308 return null; 309 310 StringBuffer buf = new StringBuffer((80 * dataObj_.size()) + 1); 311 buf.append(RMsg.NESTED_FIELD_START); 312 313 try 314 { 315 for (int i = 0; i < dataObj_.size(); ++i) 316 { 317 if (++i > 1) 318 buf.append(RFldHashtable.ELEMENT_DELIMITER).append(' '); 319 320 String elem = (String) dataObj_.get(i); 321 buf.append(String.valueOf(i)).append(RMsg.FIELD_EQUAL).append( 322 elem); 323 } 324 return buf.append(RMsg.NESTED_FIELD_END).toString(); 325 } 326 catch (ClassCastException ex) 327 { 328 return null; 329 } 330 } 331 /*** 332 * Get the field value as a java.util.Hashtable. Throws an exception if the 333 * field value is not set. 334 * @return Field value as java.util.Hashtable. 335 */ 336 public java.util.Hashtable getValueAsHashtable() throws FieldValidationException 337 { 338 if (! valSet_) 339 throw new FieldValidationException("Field not set: " + getName()); 340 341 java.util.Hashtable hashTbl = new java.util.Hashtable(3 * dataObj_.size()); 342 for (int i = dataObj_.size() - 1; i >= 0; --i) 343 hashTbl.put(String.valueOf(i), dataObj_.get(i)); 344 345 return hashTbl; 346 } 347 348 /*** 349 * Get the XML tag for this field type. 350 * 351 * @return XML tag for this field type. 352 */ 353 public final String getTag() 354 { 355 return XML_TAG; 356 } 357 358 /*** 359 * Set the XML tag for this field type. 360 * 361 * @param tag 362 * New XML tag for this field type. 363 */ 364 public static void setTag(String tag) 365 { 366 XML_TAG = tag; 367 } 368 369 /*** 370 * XML tag for this element type. 371 */ 372 static transient String XML_TAG = "strlist"; 373 374 /*** 375 * Data. 376 */ 377 protected List dataObj_; 378 }

This page was automatically generated by Maven