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