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.math.BigDecimal; 21 22 /*** 23 * Class to represent a <tt>float</tt> field. 24 * @author Jawaid Hakim. 25 */ 26 public class RFldDecimal extends RFldNumeric 27 { 28 static final long serialVersionUID = -2163883015583127242L; //Generated by jserial 29 30 /*** 31 * Default constructor. 32 */ 33 public RFldDecimal() 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 RFldDecimal(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 RFldDecimal(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.DECIMAL; 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 RFldDecimal)) 88 { 89 return false; 90 } 91 92 RFldDecimal another = (RFldDecimal)anObject; 93 if (valSet_ != another.isValSet()) 94 return false; 95 else 96 return (! valSet_ || Double.doubleToLongBits(dataObj_.doubleValue()) == Double.doubleToLongBits(another.getValue().doubleValue())); 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 if (newData instanceof BigDecimal) 131 return set((BigDecimal)newData); 132 else 133 return set(new BigDecimal(newData.toString())); 134 } 135 catch (NumberFormatException ex) 136 { 137 throw new FieldValidationException(ex); 138 } 139 } 140 141 /*** 142 * Set data. 143 * @param newData New data. 144 */ 145 public final RFld set(String newData) throws FieldValidationException 146 { 147 return set(new BigDecimal(newData)); 148 } 149 150 /*** 151 * Set the field value from a JDOM element. 152 * @param elem Field value as a JDOM element. 153 */ 154 public final RFld set(org.jdom.Element elem) throws FieldValidationException 155 { 156 return set(elem.getAttributeValue(XML_ATTR_VALUE)); 157 } 158 159 /*** 160 * Set data. 161 * @param newData New data. 162 * @throws NullPointerException if the new data is <tt>null</tt>. 163 */ 164 public final RFld set(BigDecimal newData) throws FieldValidationException 165 { 166 validate(newData); 167 168 dataObj_ = newData; 169 valSet_ = true; 170 171 return this; 172 } 173 174 /*** 175 * Validate against constraints. A field is valid if either it's value is set 176 * and satisfies all constraints, or the the field is optional. 177 */ 178 public final void validate() throws FieldValidationException 179 { 180 // Only need to check that non-optional fields have been set. If a 181 // field has been set then it must be valid since validation is done 182 // with each set. 183 if (! valSet_ && ! optional_) 184 throw new FieldValidationException("Field not set: " + this.getName()); 185 } 186 187 /*** 188 * Check if a new value will satifsy constraints. 189 * @param newData New value. 190 */ 191 public final void validate(BigDecimal newData) throws FieldValidationException 192 { 193 if (locked_) 194 throw new FieldValidationException("Cannot modify locked field: " + getName()); 195 } 196 197 /*** 198 * Get data. 199 * @return Field value. 200 * @throws NullPointerException if the field value is not set. 201 */ 202 public final BigDecimal getValue() 203 { 204 return dataObj_; 205 } 206 207 /*** 208 * Get the field value as an object. 209 * @return Field value as an object. Returns <tt>null</tt> if the 210 * field value is not set. 211 */ 212 public final Object getValueAsObject() 213 { 214 return dataObj_; 215 } 216 217 /*** 218 * Get the field value as a string. 219 * @return Field value as a string. 220 * @throws NullPointerException if the field value is not set. 221 */ 222 public final String getValueAsString() 223 { 224 return Converter.getBigDecimalFormatter().format(dataObj_); 225 } 226 227 /*** 228 * Get the field value as a <tt>java.util.Hashtable</tt>. Not supported - throws 229 * an exception. 230 * @return Field value as java.util.Hashtable. 231 */ 232 public final java.util.Hashtable getValueAsHashtable() throws FieldValidationException 233 { 234 throw new FieldValidationException("Not supported"); 235 } 236 237 /*** 238 * Get the XML tag for this field type. 239 * @return XML tag for this field type. 240 */ 241 public final String getTag() 242 { 243 return XML_TAG; 244 } 245 246 /*** 247 * Set the XML tag for this field type. 248 * @param tag New XML tag for this field type. 249 */ 250 public static void setTag(String tag) 251 { 252 XML_TAG = tag; 253 } 254 255 /*** 256 * XML tag for this element type. 257 */ 258 protected static transient String XML_TAG = "decimal"; 259 260 /*** 261 * Data. 262 */ 263 protected java.math.BigDecimal dataObj_; 264 }

This page was automatically generated by Maven