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>String[]</tt> field. 22 * @author Jawaid Hakim. 23 */ 24 public class RFldStringArray extends RFldArray 25 { 26 /*** 27 * Default constructor. 28 */ 29 public RFldStringArray() 30 { 31 } 32 33 /*** 34 * Constructor. 35 * @param name Field name. 36 * @param fieldId Field id. Field ids must be either <tt>0</tt> 37 * to indicate that there is no id on the field, or greater. In addition, 38 * field ids must be unique within a messages - no two fields are allowed 39 * to have the same field id. 40 */ 41 public RFldStringArray(String name, int fieldId) 42 { 43 super(name, fieldId); 44 } 45 46 /*** 47 * Constructor. 48 * @param name Field name. 49 * @param fieldId Field id. Field ids must be either <tt>0</tt> 50 * to indicate that there is no id on the field, or greater. In addition, 51 * field ids must be unique within a messages - no two fields are allowed 52 * to have the same field id. 53 * @param desc Field description. 54 */ 55 public RFldStringArray(String name, int fieldId, String desc) 56 { 57 super(name, fieldId, desc); 58 } 59 60 /*** 61 * Get field type. 62 * @return Field type <tt>STRINGARRAY</tt>. 63 * @see RFldType 64 */ 65 public final RFldType getType() 66 { 67 return RFldType.STRINGARRAY; 68 } 69 70 /*** 71 * Check if another field is equal to this field. Equality is defined 72 * as the fields having the same string representation. A short cut 73 * that needs to be fixed for cases where the string representation is 74 * rounding off numbers etc. 75 * @param anObject Another field. 76 * @return <tt>true</tt> if another field is equal to this field. 77 * Otherwise, returns <tt>false</tt>. 78 */ 79 public boolean equals(Object anObject) 80 { 81 if (this == anObject) 82 { 83 return true; 84 } 85 else if (! (anObject instanceof RFldStringArray)) 86 { 87 return false; 88 } 89 RFldStringArray another = (RFldStringArray)anObject; 90 if (valSet_ != another.isValSet()) 91 return false; 92 else 93 return (! valSet_ || RArrayCompare.equals(dataObj_, another.getValue())); 94 } 95 96 /*** 97 * Get the number of elements in the array. 98 * @return Number of elements in the array. If the value is not set 99 * returns <tt>-1</tt>. 100 */ 101 public int length() 102 { 103 return (! valSet_) ? -1 : dataObj_.length; 104 } 105 106 /*** 107 * Get an element from the array. 108 * @param index Array index. 109 * @return Specified element. Returns <tt>null</tt> if the index 110 * is out of bounds or the value is not set. 111 * @see #length() 112 */ 113 public String getElement(int index) 114 { 115 return (valSet_ && index >= 0 && index < dataObj_.length) ? dataObj_[index] : null; 116 } 117 118 /*** 119 * Returns the hash code value for the field. 120 * @return a hash code value for the field. 121 */ 122 public int hashCode() 123 { 124 int hashCode = 17; 125 if (valSet_) 126 { 127 for (int i = dataObj_.length - 1; i >= 0; --i) 128 hashCode = 31 * hashCode + dataObj_[i].hashCode(); 129 } 130 return hashCode; 131 } 132 133 /*** 134 * Reset the field value. 135 * @see #isValSet() 136 */ 137 public void reset() throws FieldValidationException 138 { 139 if (isLocked()) 140 throw new FieldValidationException("Field " + getName() + " is locked"); 141 142 if (valSet_) 143 { 144 valSet_ = false; 145 dataObj_ = null; 146 } 147 } 148 149 /*** 150 * Set data. 151 * @param newData New data. 152 */ 153 public RFld set(Object newData) throws FieldValidationException 154 { 155 if (newData instanceof String[]) 156 return set((String[])newData); 157 else if (newData != null) 158 throw new FieldValidationException("Invalid value type: " + newData.getClass().getName() + " for field: " + getName()); 159 else 160 throw new FieldValidationException("New value is NULL for field: " + getName()); 161 } 162 163 /*** 164 * Set data. 165 * @param newData New data. 166 */ 167 public RFld set(String[] newData) throws FieldValidationException 168 { 169 validate(newData); 170 171 dataObj_ = newData; 172 valSet_ = true; 173 174 return this; 175 } 176 177 /*** 178 * Set data. 179 * @param newData New data. 180 */ 181 public RFld set(java.util.Hashtable newData) throws FieldValidationException 182 { 183 String[] elements = new String[newData.size()]; 184 for (int i = elements.length - 1; i >= 0; --i) 185 elements[i] = (String)newData.get(String.valueOf(i)); 186 187 return set(elements); 188 } 189 190 /*** 191 * Set the field value from a JDOM element. 192 * @param elem Field value as a JDOM element. 193 */ 194 public final RFld set(org.jdom.Element elem) throws FieldValidationException 195 { 196 java.util.List entries = elem.getChildren(); 197 if (entries != null) 198 { 199 String[] newData = new String[entries.size()]; 200 for (int i = newData.length - 1; i >= 0; --i) 201 { 202 org.jdom.Element entry = (org.jdom.Element)entries.get(i); 203 int index = Integer.parseInt(entry.getAttributeValue(XML_ATTR_NAME)); 204 newData[index] = entry.getAttributeValue(XML_ATTR_VALUE); 205 } 206 return set(newData); 207 } 208 209 return this; 210 } 211 212 /*** 213 * Validate against constraints. A field is valid if either it's value is set 214 * and satisfies all constraints, or the the field is optional. 215 */ 216 public void validate() throws FieldValidationException 217 { 218 // Only need to check that non-optional fields have been set. If a 219 // field has been set then it must be valid since validation is done 220 // with each set. 221 if (! valSet_ && !optional_) 222 throw new FieldValidationException("Field not set: " + getName()); 223 } 224 225 /*** 226 * Check if a new value will satifsy constraints. 227 * @param newData New value. 228 */ 229 public void validate(String[] newData) throws FieldValidationException 230 { 231 if (locked_) 232 throw new FieldValidationException("Cannot modify locked field: " + getName()); 233 234 if (newData == null) 235 throw new FieldValidationException("New value is NULL for field: " + getName());; 236 } 237 238 /*** 239 * Get data. 240 * @return data Data. Returns <tt>null</tt> if the field value is not set. 241 */ 242 public String[] getValue() 243 { 244 return dataObj_; 245 } 246 247 /*** 248 * Get the field value as an object. 249 * @return Field value as an object. Reference to data is passed out so be 250 * very careful about modifying the data. Returns <tt>null</tt> if the field 251 * value is not set. 252 */ 253 public Object getValueAsObject() 254 { 255 return dataObj_; 256 } 257 258 /*** 259 * Get the field as a string. 260 * @return Field value as a string. Returns <tt>null</tt> 261 * if the field value is not set. Elements of the array are separated 262 * by ELEMENT_DELIMITER, and the value of each element is prefixed by 263 * its index.<br> 264 * For example, if the array contains elements <tt>Hello</tt> and 265 * <tt>World</tt>, in that order, then the output string will be 266 * {0=Hello|1=World}. 267 * @see #ELEMENT_DELIMITER 268 */ 269 public String getValueAsString() 270 { 271 if (! valSet_) 272 return null; 273 274 StringBuffer buf = new StringBuffer((20 * dataObj_.length) + 1); 275 buf.append(RMsg.NESTED_FIELD_START); 276 for (int i = 0; i < dataObj_.length; ++i) 277 { 278 if (i > 0) 279 buf.append(ELEMENT_DELIMITER); 280 281 buf.append(String.valueOf(i)).append(RMsg.FIELD_EQUAL).append(dataObj_[i]); 282 } 283 buf.append(RMsg.NESTED_FIELD_END); 284 return buf.toString(); 285 } 286 287 /*** 288 * Get the field value as a java.util.Hashtable. Throws an exception if the 289 * field value is not set. 290 * @return Field value as java.util.Hashtable. 291 */ 292 public java.util.Hashtable getValueAsHashtable() throws FieldValidationException 293 { 294 if (! valSet_) 295 throw new FieldValidationException("Field not set: " + getName()); 296 297 java.util.Hashtable hashTbl = new java.util.Hashtable(3 * dataObj_.length); 298 for (int i = dataObj_.length - 1; i >= 0; --i) 299 hashTbl.put(String.valueOf(i), dataObj_[i]); 300 301 return hashTbl; 302 } 303 304 /*** 305 * Get the XML tag for this field type. 306 * @return XML tag for this field type. 307 */ 308 public final String getTag() 309 { 310 return XML_TAG; 311 } 312 313 /*** 314 * Set the XML tag for this field type. 315 * @param tag New XML tag for this field type. 316 */ 317 public static void setTag(String tag) 318 { 319 XML_TAG = tag; 320 } 321 322 /*** 323 * XML tag for this element type. 324 */ 325 static transient String XML_TAG = "strarray"; 326 327 /*** 328 * Data. 329 */ 330 protected String[] dataObj_; 331 }

This page was automatically generated by Maven