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

This page was automatically generated by Maven