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.lang.Float; 21 22 /*** 23 * Class to represent a <tt>float</tt> field. 24 * @author Jawaid Hakim. 25 */ 26 public class RFldF32 extends RFldNumeric 27 { 28 static final long serialVersionUID = -2163883015583127242L; //Generated by jserial 29 30 /*** 31 * Default constructor. 32 */ 33 public RFldF32() 34 { 35 } 36 37 /*** 38 * Constructor. 39 * @param name Field name. 40 * @param fieldId Field id. Field ids must be either <tt>0</tt> 41 * to indicate that there is no id on the field, or greater. In addition, 42 * field ids must be unique within a messages - no two fields are allowed 43 * to have the same field id. 44 */ 45 public RFldF32(String name, int fieldId) 46 { 47 super(name, fieldId); 48 } 49 50 /*** 51 * Constructor. 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 RFldF32(String name, int fieldId, String desc) 60 { 61 super(name, fieldId, desc); 62 } 63 64 /*** 65 * Get field type. 66 * @return Field type <tt>F32</tt>. 67 * @see RFldType 68 */ 69 public final RFldType getType() 70 { 71 return RFldType.F32; 72 } 73 74 /*** 75 * Check if another field is equal to this field. Equality is defined 76 * as the fields having the same value. 77 * @param anObject Another field. 78 * @return <tt>true</tt> if another field is equal to this field 79 * Otherwise, returns <tt>false</tt>. 80 */ 81 public final boolean equals(Object anObject) 82 { 83 if (this == anObject) 84 { 85 return true; 86 } 87 else if (! (anObject instanceof RFldF32)) 88 { 89 return false; 90 } 91 92 RFldF32 another = (RFldF32)anObject; 93 if (valSet_ != another.isValSet()) 94 return false; 95 else 96 return (! valSet_ || Float.floatToIntBits(dataObj_.floatValue()) == Float.floatToIntBits(another.getValue())); 97 } 98 99 100 /*** 101 * Returns the hash code value for the field. 102 * @return A hash code value for the field, uses the java.lang.Float 103 * floatToIntBits(float) method to compute the value. 104 */ 105 public final int hashCode() 106 { 107 return (valSet_) ? Float.floatToIntBits(dataObj_.floatValue()) : 1231; 108 } 109 110 /*** 111 * Reset the field value. 112 * @see #isValSet() 113 */ 114 public final void reset() throws FieldValidationException 115 { 116 if (isLocked()) 117 throw new FieldValidationException("Field " + this.getName() + " is locked"); 118 119 valSet_ = false; 120 } 121 122 /*** 123 * Set data. 124 * @param newData New data. 125 */ 126 public final RFld set(Object newData) throws FieldValidationException 127 { 128 try 129 { 130 return set((Float)newData); 131 } 132 catch (ClassCastException e) 133 { 134 try 135 { 136 return set(Float.valueOf(newData.toString())); 137 } 138 catch (NumberFormatException ex) 139 { 140 throw new FieldValidationException(ex); 141 } 142 } 143 } 144 145 /*** 146 * Set data. 147 * @param newData New data. 148 */ 149 public final RFld set(float newData) throws FieldValidationException 150 { 151 return set(new Float(newData)); 152 } 153 154 /*** 155 * Set the field value from a JDOM element. 156 * @param elem Field value as a JDOM element. 157 */ 158 public final RFld set(org.jdom.Element elem) throws FieldValidationException 159 { 160 return set(elem.getAttributeValue(XML_ATTR_VALUE)); 161 } 162 163 /*** 164 * Set data. 165 * @param newData New data. 166 * @throws NullPointerException if the new data is <tt>null</tt>. 167 */ 168 public final RFld set(Float newData) throws FieldValidationException 169 { 170 validate(newData.floatValue()); 171 172 dataObj_ = newData; 173 valSet_ = true; 174 175 return this; 176 } 177 178 /*** 179 * Validate against constraints. A field is valid if either it's value is set 180 * and satisfies all constraints, or the the field is optional. 181 */ 182 public final void validate() throws FieldValidationException 183 { 184 // Only need to check that non-optional fields have been set. If a 185 // field has been set then it must be valid since validation is done 186 // with each set. 187 if (! valSet_ && ! optional_) 188 throw new FieldValidationException("Field not set: " + this.getName()); 189 } 190 191 /*** 192 * Check if a new value will satifsy constraints. 193 * @param newData New value. 194 */ 195 public final void validate(float newData) throws FieldValidationException 196 { 197 if (locked_) 198 throw new FieldValidationException("Cannot modify locked field: " + getName()); 199 200 for (int i = getConstraintCount() - 1; i >= 0; --i) 201 ((RFldConstraintF32)getConstraint(i)).validate(this, newData); 202 } 203 204 /*** 205 * Get data. 206 * @return Field value. 207 * @throws NullPointerException if the field value is not set. 208 */ 209 public final float getValue() 210 { 211 return dataObj_.floatValue(); 212 } 213 214 /*** 215 * Get the field value as an object. 216 * @return Field value as an object. Returns <tt>null</tt> if the 217 * field value is not set. 218 */ 219 public final Object getValueAsObject() 220 { 221 return dataObj_; 222 } 223 224 /*** 225 * Get the field value as a string. 226 * @return Field value as a string. 227 * @throws NullPointerException if the field value is not set. 228 */ 229 public final String getValueAsString() 230 { 231 return dataObj_.toString(); 232 } 233 234 /*** 235 * Get the field value as a <tt>java.util.Hashtable</tt>. Not supported - throws 236 * an exception. 237 * @return Field value as java.util.Hashtable. 238 */ 239 public final java.util.Hashtable getValueAsHashtable() throws FieldValidationException 240 { 241 throw new FieldValidationException("Not supported"); 242 } 243 244 /*** 245 * Get the XML tag for this field type. 246 * @return XML tag for this field type. 247 */ 248 public final String getTag() 249 { 250 return XML_TAG; 251 } 252 253 /*** 254 * Set the XML tag for this field type. 255 * @param tag New XML tag for this field type. 256 */ 257 public static void setTag(String tag) 258 { 259 XML_TAG = tag; 260 } 261 262 /*** 263 * XML tag for this element type. 264 */ 265 protected static transient String XML_TAG = "f32"; 266 267 /*** 268 * Data. 269 */ 270 protected java.lang.Float dataObj_; 271 }

This page was automatically generated by Maven