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.Double;
21
22 /***
23 * Class to represent a <tt>double</tt> field.
24 * @author Jawaid Hakim.
25 */
26 public class RFldF64 extends RFldNumeric
27 {
28 /***
29 * Default constructor.
30 */
31 public RFldF64()
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 RFldF64(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 RFldF64(String name, int fieldId, String desc)
58 {
59 super(name, fieldId, desc);
60 }
61
62 /***
63 * Get field type.
64 * @return Field type <tt>F64</tt>.
65 * @see RFldType
66 */
67 public final RFldType getType()
68 {
69 return RFldType.F64;
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 field is equal to this field.
77 * Otherwise, returns <tt>false</tt>.
78 */
79 public final boolean equals(Object anObject )
80 {
81 if (this == anObject)
82 {
83 return true;
84 }
85 else if (! (anObject instanceof RFldF64))
86 {
87 return false;
88 }
89
90 RFldF64 another = (RFldF64)anObject;
91 if (valSet_ != another.isValSet())
92 return false;
93 else
94 return (! valSet_ || Double.doubleToLongBits(dataObj_.doubleValue()) == Double.doubleToLongBits(another.getValue()));
95 }
96
97 /***
98 * Returns the hash code value for the field.
99 * @return A hash code value for the field, uses the java.lang.Double
100 * doubleToLongBits(double) method to help compute the value.
101 */
102 public final int hashCode()
103 {
104 if (valSet_)
105 {
106 long bits = Double.doubleToLongBits(dataObj_.doubleValue());
107 return (int)(bits ^ (bits >> 32));
108 }
109 else
110 {
111 return 1231;
112 }
113 }
114
115 /***
116 * Reset the field value.
117 * @see #isValSet()
118 */
119 public final void reset() throws FieldValidationException
120 {
121 if (isLocked())
122 throw new FieldValidationException("Field " + getName() + " is locked");
123
124 valSet_ = false;
125 }
126
127 /***
128 * Set data.
129 * @param newData New data.
130 */
131 public final RFld set(Object newData) throws FieldValidationException
132 {
133 try
134 {
135 return set((Double)newData);
136 }
137 catch (ClassCastException e)
138 {
139 try
140 {
141 return set(Double.valueOf(newData.toString()));
142 }
143 catch (NumberFormatException ex)
144 {
145 throw new FieldValidationException(ex);
146 }
147 }
148 }
149
150 /***
151 * Set data.
152 * @param newData New data.
153 */
154 public final RFld set(double newData) throws FieldValidationException
155 {
156 return set(new Double(newData));
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(elem.getAttributeValue(XML_ATTR_VALUE));
166 }
167
168 /***
169 * Set data.
170 * @param newData New data.
171 * @throws NullPointerException if the new data is <tt>null</tt>.
172 */
173 public final RFld set(Double newData) throws FieldValidationException
174 {
175 validate(this, newData.doubleValue());
176
177 dataObj_ = newData;
178 valSet_ = true;
179
180 return this;
181 }
182
183 /***
184 * Validate against constraints. A field is valid if either it's value is set
185 * and satisfies all constraints, or the the field is optional.
186 */
187 public final void validate() throws FieldValidationException
188 {
189 // Only need to check that non-optional fields have been set. If a
190 // field has been set then it must be valid since validation is done
191 // with each set.
192 if (! valSet_ && ! optional_)
193 throw new FieldValidationException("Field not set: " + getName());
194 }
195
196 /***
197 * Check if a new value will satifsy constraints.
198 * @param fld Field.
199 * @param newData New value.
200 */
201 public final void validate(RFld fld, double newData) throws FieldValidationException
202 {
203 if (locked_)
204 throw new FieldValidationException("Cannot modify locked field: " + getName());
205
206 for (int i = getConstraintCount() - 1; i >= 0; --i)
207 ((RFldConstraintF64)getConstraint(i)).validate(fld, newData);
208 }
209
210 /***
211 * Get data.
212 * @return data Data.
213 * @throws NullPointerException if the field value has not been set.
214 */
215 public final double getValue()
216 {
217 return dataObj_.doubleValue();
218 }
219
220 /***
221 * Get the field value as an object.
222 * @return Field value as an object. Returns <tt>null</tt> if the field value is not set.
223 */
224 public final Object getValueAsObject()
225 {
226 return dataObj_;
227 }
228
229 /***
230 * Get the field value as a string.
231 * @return Field value as a string. Returns <tt>0.0</tt> if the field value is not set.
232 * @throws NullPointerException if the field value has not been set.
233 */
234 public final String getValueAsString()
235 {
236 return dataObj_.toString();
237 }
238
239
240 /***
241 * Get the field value as a <tt>java.util.Hashtable</tt>. Not supported - throws
242 * an exception.
243 * @return Field value as java.util.Hashtable. Returns <tt>null</tt> if the field
244 * value is not set.
245 */
246 public final java.util.Hashtable getValueAsHashtable() throws FieldValidationException
247 {
248 throw new FieldValidationException("Not supported");
249 }
250
251 /***
252 * Get the XML tag for this field type.
253 * @return XML tag for this field type.
254 */
255 public final String getTag()
256 {
257 return XML_TAG;
258 }
259
260 /***
261 * Set the XML tag for this field type.
262 * @param tag New XML tag for this field type.
263 */
264 public static void setTag(String tag)
265 {
266 XML_TAG = tag;
267 }
268
269 /***
270 * XML tag for this element type.
271 */
272 protected static transient String XML_TAG = "f64";
273
274 /***
275 * Data.
276 */
277 protected java.lang.Double dataObj_;
278 }
This page was automatically generated by Maven