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