1 package org.apache.turbine.services.intake.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import org.apache.commons.lang.StringUtils;
20
21 import org.apache.turbine.services.intake.IntakeException;
22 import org.apache.turbine.services.intake.validator.DoubleValidator;
23 import org.apache.turbine.services.intake.xmlmodel.XmlField;
24
25 /***
26 * Creates Double Field objects.
27 *
28 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
29 * @version $Id: DoubleField.java 264148 2005-08-29 14:21:04Z henning $
30 */
31 public class DoubleField
32 extends Field
33 {
34 /***
35 * Constructor.
36 *
37 * @param field xml field definition object
38 * @param group xml group definition object
39 * @throws IntakeException thrown by superclass
40 */
41 public DoubleField(XmlField field, Group group)
42 throws IntakeException
43 {
44 super(field, group);
45 }
46
47 /***
48 * Sets the default value for a Double Field
49 *
50 * @param prop Parameter for the default values
51 */
52 public void setDefaultValue(String prop)
53 {
54 defaultValue = null;
55
56 if (prop == null)
57 {
58 return;
59 }
60
61 defaultValue = new Double(prop);
62 }
63
64 /***
65 * Set the empty Value. This value is used if Intake
66 * maps a field to a parameter returned by the user and
67 * the corresponding field is either empty (empty string)
68 * or non-existant.
69 *
70 * @param prop The value to use if the field is empty.
71 */
72 public void setEmptyValue(String prop)
73 {
74 emptyValue = null;
75
76 if (prop == null)
77 {
78 return;
79 }
80
81 emptyValue = new Double(prop);
82 }
83
84 /***
85 * Provides access to emptyValue such that the value returned will be
86 * acceptable as an argument parameter to Method.invoke. Subclasses
87 * that deal with primitive types should ensure that they return an
88 * appropriate value wrapped in the object wrapper class for the
89 * primitive type.
90 *
91 * @return the value to use when the field is empty or an Object that
92 * wraps the empty value for primitive types.
93 */
94 protected Object getSafeEmptyValue()
95 {
96 if (isMultiValued)
97 {
98 return new double[0];
99 }
100 else
101 {
102 return (null == getEmptyValue())
103 ? new Double(0.0) : getEmptyValue();
104 }
105 }
106
107 /***
108 * A suitable validator.
109 *
110 * @return A suitable validator
111 */
112 protected String getDefaultValidator()
113 {
114 return DoubleValidator.class.getName();
115 }
116
117 /***
118 * Sets the value of the field from data in the parser.
119 */
120 protected void doSetValue()
121 {
122 if (isMultiValued)
123 {
124 String[] inputs = parser.getStrings(getKey());
125 double[] values = new double[inputs.length];
126 for (int i = 0; i < inputs.length; i++)
127 {
128 values[i] = StringUtils.isNotEmpty(inputs[i])
129 ? new Double(inputs[i]).doubleValue()
130 : ((Double) getEmptyValue()).doubleValue();
131 }
132 setTestValue(values);
133 }
134 else
135 {
136 String val = parser.getString(getKey());
137 setTestValue(StringUtils.isNotEmpty(val)
138 ? new Double(val) : (Double) getEmptyValue());
139 }
140 }
141
142 }