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

This page was automatically generated by Maven