1 /***
2 * Copyright (c) 2002, CodeStreet LLC. All rights reserved.
3 * <p>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * <p>
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer. Redistributions in binary
9 * form must reproduce the above copyright notice, this list of conditions and
10 * the following disclaimer in the documentation and/or other materials provided
11 * with the distribution. Neither the name of CodeStreet LLC. nor the names of
12 * its contributors may be used to endorse or promote products derived from this
13 * software without specific prior written permission.
14 * <p>
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 * <p>
27 */
28
29 package com.codestreet.messageforge;
30
31 /***
32 * Class to represent a <tt>String</tt> field.
33 *
34 * @author Jawaid Hakim.
35 */
36 public class RFldString extends RFld
37 {
38 static final long serialVersionUID = 4897653670438967731L; //Generated by
39 // jserial
40
41 /***
42 * Default constructor.
43 */
44 public RFldString()
45 {
46 }
47
48 /***
49 * Constructor.
50 *
51 * @param name
52 * Field name.
53 * @param fieldId
54 * Field id. Field ids must be either <tt>0</tt> to indicate
55 * that there is no id on the field, or greater. In addition,
56 * field ids must be unique within a messages - no two fields are
57 * allowed to have the same field id.
58 */
59 public RFldString(String name, int fieldId)
60 {
61 super(name, fieldId);
62 }
63
64 /***
65 * Constructor.
66 *
67 * @param name
68 * Field name.
69 * @param fieldId
70 * Field id. Field ids must be either <tt>0</tt> to indicate
71 * that there is no id on the field, or greater. In addition,
72 * field ids must be unique within a messages - no two fields are
73 * allowed to have the same field id.
74 * @param desc
75 * Field description.
76 */
77 public RFldString(String name, int fieldId, String desc)
78 {
79 super(name, fieldId, desc);
80 }
81
82 /***
83 * Get field type.
84 *
85 * @return Field type <tt>STRING</tt>.
86 * @see RFldType
87 */
88 public final RFldType getType()
89 {
90 return RFldType.STRING;
91 }
92
93 /***
94 * Check if another field is equal to this field. Equality is defined as the
95 * fields having the same value - the comparison is case-sensitive.
96 *
97 * @param anObject
98 * Another field.
99 * @return <tt>true</tt> if another message is equal to this message.
100 * Otherwise, returns <tt>false</tt>.
101 */
102 public boolean equals(Object anObject)
103 {
104 if (this == anObject)
105 {
106 return true;
107 }
108 else if (!(anObject instanceof RFldString))
109 {
110 return false;
111 }
112 RFldString another = (RFldString) anObject;
113 if (valSet_ != another.isValSet())
114 return false;
115 else
116 return (!valSet_ || dataObj_.equals(another.getValue()));
117 }
118
119 /***
120 * Returns a hash code value for the field.
121 *
122 * @return a hash code value for the field, uses the java.lang.String
123 * hashCode method to compute the value.
124 */
125 public int hashCode()
126 {
127 return (valSet_) ? dataObj_.hashCode() : 1231;
128 }
129
130 /***
131 * Add a constraint.
132 *
133 * @param cons
134 * Constraint.
135 */
136 public void addConstraint(RFldConstraintString cons)
137 throws FieldValidationException
138 {
139 super.addConstraint(cons);
140 }
141
142 /***
143 * Reset the field value.
144 *
145 * @see #isValSet()
146 */
147 public void reset() throws FieldValidationException
148 {
149 if (isLocked())
150 throw new FieldValidationException("Field " + getName()
151 + " is locked");
152
153 valSet_ = false;
154 dataObj_ = null;
155 }
156
157 /***
158 * Set data.
159 *
160 * @param newData
161 * New data.
162 */
163 public RFld set(Object newData) throws FieldValidationException
164 {
165 try
166 {
167 return set((String) newData);
168 }
169 catch (ClassCastException ex)
170 {
171 return set(newData.toString());
172 }
173 }
174
175 /***
176 * Set the field value from a JDOM element.
177 *
178 * @param elem
179 * Field value as a JDOM element.
180 */
181 public final RFld set(org.jdom.Element elem)
182 throws FieldValidationException
183 {
184 return set(elem.getAttributeValue(XML_ATTR_VALUE));
185 }
186
187 /***
188 * Set data.
189 *
190 * @param newData
191 * New data.
192 */
193 public RFld set(String newData) throws FieldValidationException
194 {
195 validate(newData);
196
197 dataObj_ = newData;
198 valSet_ = true;
199
200 return this;
201 }
202
203 /***
204 * Validate against constraints. A field is valid if either it's value is
205 * set and satisfies all constraints, or the the field is optional.
206 */
207 public void validate() throws FieldValidationException
208 {
209 // Only need to check that non-optional fields have been set. If a
210 // field has been set then it must be valid since validation is done
211 // with each set.
212 if (!valSet_ && !optional_)
213 throw new FieldValidationException("Field not set: " + getName());
214 }
215
216 /***
217 * Check if a new value will satifsy constraints.
218 *
219 * @param newData
220 * New value.
221 */
222 public void validate(String newData) throws FieldValidationException
223 {
224 if (locked_)
225 throw new FieldValidationException("Cannot modify locked field: "
226 + getName());
227
228 if (newData == null)
229 throw new FieldValidationException("New value is NULL for field: "
230 + getName());
231 ;
232
233 for (int i = getConstraintCount() - 1; i >= 0; --i)
234 ((RFldConstraintString) getConstraint(i)).validate(this, newData);
235 }
236
237 /***
238 * Get data.
239 *
240 * @return data Data. Returns <tt>null</tt> if the field value is not set.
241 */
242 public String getValue()
243 {
244 return dataObj_;
245 }
246
247 /***
248 * Get the field value as an object.
249 *
250 * @return Field value as an object. Returns <tt>null</tt> if the field
251 * value is not set.
252 */
253 public Object getValueAsObject()
254 {
255 return dataObj_;
256 }
257
258 /***
259 * Get the field value as a string.
260 *
261 * @return Field value as a string. Returns <tt>null</tt> if the field
262 * value is not set.
263 */
264 public String getValueAsString()
265 {
266 return dataObj_;
267 }
268
269 /***
270 * Get the field value as a <tt>java.util.Hashtable</tt>. Not supported -
271 * throws an exception.
272 *
273 * @return Field value as java.util.Hashtable. Returns <tt>null</tt> if
274 * the field value is not set.
275 */
276 public java.util.Hashtable getValueAsHashtable()
277 throws FieldValidationException
278 {
279 throw new FieldValidationException("Not supported");
280 }
281
282 /***
283 * Get the XML tag for this field type.
284 *
285 * @return XML tag for this field type.
286 */
287 public final String getTag()
288 {
289 return XML_TAG;
290 }
291
292 /***
293 * Set the XML tag for this field type.
294 *
295 * @param tag
296 * New XML tag for this field type.
297 */
298 public static void setTag(String tag)
299 {
300 XML_TAG = tag;
301 }
302
303 /***
304 * XML tag for this element type.
305 */
306 protected static transient String XML_TAG = "str";
307
308 /***
309 * Not Available - if a field has this marker value, then it implies that
310 * the value of the field is not available.
311 */
312 public static final transient String NA_VALUE = "N/A";
313
314 /***
315 * Data.
316 */
317 protected String dataObj_;
318 }
This page was automatically generated by Maven