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