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 /***
21 * Class to represent a <tt>int[]</tt> field.
22 * @author Jawaid Hakim.
23 */
24 public class RFldI32Array extends RFldArray
25 {
26 /***
27 * Default constructor.
28 */
29 public RFldI32Array()
30 {
31 }
32
33 /***
34 * Constructor.
35 * @param name Field name.
36 * @param fieldId Field id. Field ids must be either <tt>0</tt>
37 * to indicate that there is no id on the field, or greater. In addition,
38 * field ids must be unique within a messages - no two fields are allowed
39 * to have the same field id.
40 */
41 public RFldI32Array(String name, int fieldId)
42 {
43 super(name, fieldId);
44 }
45
46 /***
47 * Constructor.
48 * @param name Field name.
49 * @param fieldId Field id. Field ids must be either <tt>0</tt>
50 * to indicate that there is no id on the field, or greater. In addition,
51 * field ids must be unique within a messages - no two fields are allowed
52 * to have the same field id.
53 * @param desc Field description.
54 */
55 public RFldI32Array(String name, int fieldId, String desc)
56 {
57 super(name, fieldId, desc);
58 }
59
60 /***
61 * Get field type.
62 * @return Field type <tt>I32ARRAY</tt>.
63 * @see RFldType
64 */
65 public final RFldType getType()
66 {
67 return RFldType.I32ARRAY;
68 }
69
70 /***
71 * Check if another field is equal to this field. Equality is defined
72 * as the fields having the same value.
73 * @param anObject Another field.
74 * @return <tt>true</tt> if another field is equal to this field.
75 * Otherwise, returns <tt>false</tt>.
76 */
77 public boolean equals(Object anObject)
78 {
79 if (this == anObject)
80 {
81 return true;
82 }
83 else if (!(anObject instanceof RFldI32Array))
84 {
85 return false;
86 }
87
88 RFldI32Array another = (RFldI32Array)anObject;
89 if (valSet_ != another.isValSet())
90 return false;
91 else
92 return (! valSet_ || RArrayCompare.equals(dataObj_, another.getValue()));
93 }
94
95 /***
96 * Returns the hash code value for the field array. The hash code of
97 * the array is the sum of the hash code (values) for each entry in the
98 * array.
99 * @return A hash code value for the field.
100 */
101 public int hashCode()
102 {
103 int h = 17;
104 if (valSet_)
105 {
106 for (int i = dataObj_.length - 1; i >= 0; --i)
107 h = 31 * h + dataObj_[i];
108 }
109 return h;
110 }
111
112 /***
113 * Reset the field value.
114 * @see #isValSet()
115 */
116 public void reset() throws FieldValidationException
117 {
118 if (isLocked())
119 throw new FieldValidationException("Field " + getName() + " is locked");
120
121 if (valSet_)
122 {
123 dataObj_ = null;
124 valSet_ = false;
125 }
126 }
127
128 /***
129 * Set data.
130 * @param newData New data.
131 */
132 public RFld set(Object newData) throws FieldValidationException
133 {
134 if (newData instanceof int[])
135 return set((int[]) newData);
136 else if (newData != null)
137 throw new FieldValidationException("Invalid value type: " + newData.getClass().getName() + " for field: " + getName());
138 else
139 throw new FieldValidationException("New value is NULL for field: " + getName());
140 }
141
142
143 /***
144 * Set data.
145 * @param newData New data.
146 */
147 public RFld set(int[] newData) throws FieldValidationException
148 {
149 validate(newData);
150
151 dataObj_ = newData;
152 valSet_ = true;
153
154 return this;
155 }
156
157 /***
158 * Set the field value from a JDOM element.
159 * @param elem Field value as a JDOM element.
160 */
161 public final RFld set(org.jdom.Element elem) throws FieldValidationException
162 {
163 return set(parse(elem));
164 }
165
166 /***
167 * Set the field value from a JDOM element.
168 * @param elem Field value as a JDOM element.
169 */
170 final static int[] parse(org.jdom.Element elem)
171 {
172 int[] newData = null;
173 java.util.List entries = elem.getChildren();
174 if (entries != null)
175 {
176 newData = new int[entries.size()];
177 for (int i = newData.length - 1; i >= 0; --i)
178 {
179 org.jdom.Element entry = (org.jdom.Element)entries.get(i);
180 int index = Integer.parseInt(entry.getAttributeValue(XML_ATTR_NAME));
181 newData[index] = Integer.parseInt(entry.getAttributeValue(XML_ATTR_VALUE));
182 }
183 }
184 return newData;
185 }
186
187 /***
188 * Validate against constraints. A field is valid if either it's value is set
189 * and satisfies all constraints, or the the field is optional.
190 */
191 public void validate() throws FieldValidationException
192 {
193 // Only need to check that non-optional fields have been set. If a
194 // field has been set then it must be valid since validation is done
195 // with each set.
196 if (! valSet_ && ! optional_)
197 throw new FieldValidationException("Field not set: " + getName());
198 }
199
200 /***
201 * Check if a new value will satifsy constraints.
202 * @param newData New value.
203 */
204 public void validate(int[] newData) throws FieldValidationException
205 {
206 if (locked_)
207 throw new FieldValidationException("Cannot modify locked field: " + getName());
208
209 if (newData == null)
210 throw new FieldValidationException("New value is NULL for field: " + getName());;
211 }
212
213 /***
214 * Get data.
215 * @return data Data. Returns <tt>null</tt> if the field value is not set.
216 */
217 public int[] getValue()
218 {
219 return dataObj_;
220 }
221
222 /***
223 * Get the field value as an object.
224 * @return Field value as an object. Returns <tt>null</tt> if the field value is not set.
225 */
226 public Object getValueAsObject()
227 {
228 return dataObj_;
229 }
230
231 /***
232 * Get the field value as a string.
233 * @return Field value as a string. Returns <tt>null</tt> if the field value is not set.
234 */
235 public String getValueAsString()
236 {
237 if (! valSet_)
238 return null;
239
240 StringBuffer buf = new StringBuffer((20 * dataObj_.length) + 1);
241 buf.append(RMsg.NESTED_FIELD_START);
242 for (int i = 0; i < dataObj_.length; ++i)
243 {
244 if (i > 0)
245 buf.append(ELEMENT_DELIMITER);
246
247 buf.append(String.valueOf(i)).append(RMsg.FIELD_EQUAL).append(dataObj_[i]);
248 }
249 buf.append(RMsg.NESTED_FIELD_END);
250 return buf.toString();
251 }
252
253 /***
254 * Get the field value as a <tt>java.util.Hashtable</tt>. Not supported - throws
255 * an exception.
256 * @return Field value as java.util.Hashtable. Returns <tt>null</tt> if the field
257 * value is not set.
258 */
259 public java.util.Hashtable getValueAsHashtable() throws FieldValidationException
260 {
261 throw new FieldValidationException("Not supported");
262 }
263
264 /***
265 * Get the XML tag for this field type.
266 * @return XML tag for this field type.
267 */
268 public final String getTag()
269 {
270 return XML_TAG;
271 }
272
273 /***
274 * Set the XML tag for this field type.
275 * @param tag New XML tag for this field type.
276 */
277 public static void setTag(String tag)
278 {
279 XML_TAG = tag;
280 }
281
282 /***
283 * XML tag for this element type.
284 */
285 protected static transient String XML_TAG = "i32array";
286
287 /***
288 * Data.
289 */
290 protected int[] dataObj_;
291 }
This page was automatically generated by Maven