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