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