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.Byte;
21
22 /***
23 * Class to represent a <tt>byte</tt> field.
24 * @author Jawaid Hakim.
25 */
26 public class RFldI8 extends RFldNumeric
27 {
28 /***
29 * Default constructor.
30 */
31 public RFldI8()
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 RFldI8(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 RFldI8(String name, int fieldId, String desc)
58 {
59 super(name, fieldId, desc);
60 }
61
62 /***
63 * Get field type.
64 * @return Field type <tt>I16</tt>.
65 * @see RFldType
66 */
67 public final RFldType getType()
68 {
69 return RFldType.I8;
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 RFldI8))
86 {
87 return false;
88 }
89
90 RFldI8 another = (RFldI8)anObject;
91 if (valSet_ != another.isValSet())
92 return false;
93 else
94 return (! valSet_ || dataObj_.byteValue() == another.getValue());
95 }
96
97 /***
98 * Returns the hash code value for the field.
99 * @return A hash code value for the field.
100 */
101 public final int hashCode()
102 {
103 return (valSet_) ? (int)dataObj_.byteValue() : 1231;
104 }
105
106 /***
107 * Reset the field value.
108 * @see #isValSet()
109 */
110 public final void reset() throws FieldValidationException
111 {
112 if (isLocked())
113 throw new FieldValidationException("Field " + getName() + " is locked");
114
115 valSet_ = false;
116 }
117
118 /***
119 * Set data.
120 * @param newData New data.
121 */
122 public final RFld set(Object newData) throws FieldValidationException
123 {
124 try
125 {
126 return set((Byte)newData);
127 }
128 catch (ClassCastException e)
129 {
130 try
131 {
132 return set(Byte.valueOf(newData.toString()));
133 }
134 catch (NumberFormatException ex)
135 {
136 throw new FieldValidationException(ex);
137 }
138 }
139 }
140
141 /***
142 * Set data.
143 * @param newData New data.
144 */
145 public final RFld set(byte newData) throws FieldValidationException
146 {
147 return set(new Short(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(Byte newData) throws FieldValidationException
165 {
166 validate(newData.byteValue());
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: " + getName());
185 }
186
187 /***
188 * Check if a new value will satifsy constraints.
189 * @param newData New value.
190 */
191 public final void validate(byte newData) throws FieldValidationException
192 {
193 if (locked_)
194 throw new FieldValidationException("Cannot modify locked field: " + getName());
195
196 for (int i = getConstraintCount() - 1; i >= 0; --i)
197 ((RFldConstraintI8)getConstraint(i)).validate(this, newData);
198 }
199
200 /***
201 * Get data.
202 * @return data Data.
203 * @throws NullPointerException if the field value has not been set.
204 */
205 public final byte getValue()
206 {
207 return dataObj_.byteValue();
208 }
209
210 /***
211 * Get the field value as an object.
212 * @return Field value as an object. Returns <tt>null</tt> if the field
213 * value is not set.
214 */
215 public final Object getValueAsObject()
216 {
217 return dataObj_;
218 }
219
220 /***
221 * Get the field value as a string.
222 * @return Field value as a string.
223 * @throws NullPointerException if the field value is not set.
224 */
225 public final String getValueAsString()
226 {
227 return dataObj_.toString();
228 }
229
230 /***
231 * Get the field value as a <tt>java.util.Hashtable</tt>. Not supported - throws
232 * an exception.
233 * @return Field value as java.util.Hashtable. Returns <tt>null</tt> if the field
234 * value is not set.
235 */
236 public final java.util.Hashtable getValueAsHashtable() throws FieldValidationException
237 {
238 throw new FieldValidationException("Not supported");
239 }
240
241 /***
242 * Get the XML tag for this field type.
243 * @return XML tag for this field type.
244 */
245 public final String getTag()
246 {
247 return XML_TAG;
248 }
249
250 /***
251 * Set the XML tag for this field type.
252 * @param tag New XML tag for this field type.
253 */
254 public static void setTag(String tag)
255 {
256 XML_TAG = tag;
257 }
258
259 /***
260 * XML tag for this element type.
261 */
262 protected static transient String XML_TAG = "i8";
263
264 /***
265 * Data.
266 */
267 protected java.lang.Byte dataObj_;
268 }
This page was automatically generated by Maven