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 R<tt>double[]</tt> field. 22 * @author Jawaid Hakim. 23 */ 24 public class RFldF64Array extends RFldArray 25 { 26 /*** 27 * Default constructor. 28 */ 29 public RFldF64Array() 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 RFldF64Array(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 RFldF64Array(String name, int fieldId, String desc) 56 { 57 super(name, fieldId, desc); 58 } 59 60 /*** 61 * Get field type. 62 * @return Field type <tt>F64ARRAY</tt>. 63 * @see RFldType 64 */ 65 public final RFldType getType() 66 { 67 return RFldType.F64ARRAY; 68 } 69 70 /*** 71 * Check if another field is equal to this field. Equality is defined 72 * as the fields having the same value. 73 * @param anObject Another field. 74 * @return <tt>true</tt> if another message is equal to this message. 75 * Otherwise, returns <tt>false</tt>. 76 */ 77 public boolean equals(Object anObject) 78 { 79 if (this == anObject) 80 { 81 return true; 82 } 83 else if (!(anObject instanceof RFldF64Array)) 84 { 85 return false; 86 } 87 88 RFldF64Array another = (RFldF64Array)anObject; 89 if (valSet_ != another.isValSet()) 90 return false; 91 else 92 return (! valSet_ || RArrayCompare.equals(dataObj_, another.getValue())); 93 } 94 95 /*** 96 * Returns the hash code value for the field array. The hash code of 97 * the array is the sum of the hash code (values) for each entry in the 98 * array. Uses java.lang.Double doubleToLongBits(double) method to help 99 * compute the value. 100 * @return A hash code value for the field. 101 */ 102 public int hashCode() 103 { 104 int h = 17; 105 if (valSet_) 106 { 107 long bits = 0; 108 for (int i = dataObj_.length - 1; i >= 0; --i) 109 { 110 bits = Double.doubleToLongBits(dataObj_[i]); 111 h += (int)(bits ^ (bits >> 32)); 112 } 113 } 114 return h; 115 } 116 117 /*** 118 * Reset the field value. 119 * @see #isValSet() 120 */ 121 public void reset() throws FieldValidationException 122 { 123 if (isLocked()) 124 throw new FieldValidationException("Field " + getName() + " is locked"); 125 126 if (valSet_) 127 { 128 dataObj_ = null; 129 valSet_ = false; 130 } 131 } 132 133 /*** 134 * Set data. 135 * @param newData New data. 136 */ 137 public RFld set(Object newData) throws FieldValidationException 138 { 139 if (newData instanceof double[]) 140 return set((double[]) newData); 141 else if (newData != null) 142 throw new FieldValidationException("Invalid value type: " + newData.getClass().getName() + " for field: " + getName()); 143 else 144 throw new FieldValidationException("New value is NULL for field: " + getName()); 145 } 146 147 148 /*** 149 * Set data. 150 * @param newData New data. 151 */ 152 public RFld set(double[] newData) throws FieldValidationException 153 { 154 validate(newData); 155 156 dataObj_ = newData; 157 valSet_ = true; 158 159 return this; 160 } 161 /*** 162 * Set the field value from a JDOM element. 163 * @param elem Field value as a JDOM element. 164 */ 165 public final RFld set(org.jdom.Element elem) throws FieldValidationException 166 { 167 return set(parse(elem)); 168 } 169 170 /*** 171 * Set the field value from a JDOM element. 172 * @param elem Field value as a JDOM element. 173 */ 174 final static double[] parse(org.jdom.Element elem) 175 { 176 double[] newData = null; 177 java.util.List entries = elem.getChildren(); 178 if (entries != null) 179 { 180 newData = new double[entries.size()]; 181 for (int i = newData.length - 1; i >= 0; --i) 182 { 183 org.jdom.Element entry = (org.jdom.Element)entries.get(i); 184 int index = Integer.parseInt(entry.getAttributeValue(XML_ATTR_NAME)); 185 newData[index] = Double.valueOf(entry.getAttributeValue(XML_ATTR_VALUE)).doubleValue(); 186 } 187 } 188 return newData; 189 } 190 191 /*** 192 * Validate against constraints. A field is valid if either it's value is set 193 * and satisfies all constraints, or the the field is optional. 194 */ 195 public void validate() throws FieldValidationException 196 { 197 // Only need to check that non-optional fields have been set. If a 198 // field has been set then it must be valid since validation is done 199 // with each set. 200 if (! valSet_ && ! optional_) 201 throw new FieldValidationException("Field not set: " + getName()); 202 } 203 204 /*** 205 * Check if a new value will satifsy constraints. 206 * @param newData New value. 207 */ 208 public void validate(double[] newData) throws FieldValidationException 209 { 210 if (locked_) 211 throw new FieldValidationException("Cannot modify locked field: " + getName()); 212 213 if (newData == null) 214 throw new FieldValidationException("New value is NULL for field: " + getName());; 215 } 216 217 /*** 218 * Get data. 219 * @return data Data. Returns <tt>null</tt> if the field value is not set. 220 */ 221 public double[] getValue() 222 { 223 return dataObj_; 224 } 225 226 /*** 227 * Get the field value as an object. 228 * @return Field value as an object. Returns <tt>null</tt> if the field value is not set. 229 */ 230 public Object getValueAsObject() 231 { 232 return dataObj_; 233 } 234 235 /*** 236 * Get the field value as a string. 237 * @return Field value as a string. Returns <tt>null</tt> if the field value is not set. 238 */ 239 public String getValueAsString() 240 { 241 if (! valSet_) 242 return null; 243 244 StringBuffer buf = new StringBuffer((20 * dataObj_.length) + 1); 245 buf.append(RMsg.NESTED_FIELD_START); 246 for (int i = 0; i < dataObj_.length; ++i) 247 { 248 if (i > 0) 249 buf.append(ELEMENT_DELIMITER); 250 251 buf.append(String.valueOf(i)).append(RMsg.FIELD_EQUAL).append(dataObj_[i]); 252 } 253 buf.append(RMsg.NESTED_FIELD_END); 254 return buf.toString(); 255 } 256 257 /*** 258 * Get the field value as a <tt>java.util.Hashtable</tt>. Not supported - throws 259 * an exception. 260 * @return Field value as java.util.Hashtable. Returns <tt>null</tt> if the field 261 * value is not set. 262 */ 263 public java.util.Hashtable getValueAsHashtable() throws FieldValidationException 264 { 265 throw new FieldValidationException("Not supported"); 266 } 267 268 /*** 269 * Get the XML tag for this field type. 270 * @return XML tag for this field type. 271 */ 272 public final String getTag() 273 { 274 return XML_TAG; 275 } 276 277 /*** 278 * Set the XML tag for this field type. 279 * @param tag New XML tag for this field type. 280 */ 281 public static void setTag(String tag) 282 { 283 XML_TAG = tag; 284 } 285 286 /*** 287 * XML tag for this element type. 288 */ 289 protected static transient String XML_TAG = "f64array"; 290 291 /*** 292 * Data. 293 */ 294 protected double[] dataObj_; 295 }

This page was automatically generated by Maven