KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > intake > model > DoubleField


1 package org.apache.turbine.services.intake.model;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
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,v 1.7.2.6 2004/05/20 03:16:39 seade Exp $
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 JavaDoc prop)
53     {
54         defaultValue = null;
55
56         if (prop == null)
57         {
58             return;
59         }
60
61         defaultValue = new Double JavaDoc(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 JavaDoc prop)
73     {
74         emptyValue = null;
75
76         if (prop == null)
77         {
78             return;
79         }
80
81         emptyValue = new Double JavaDoc(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 JavaDoc getSafeEmptyValue()
95     {
96         if (isMultiValued)
97         {
98             return new double[0];
99         }
100         else
101         {
102             return (null == getEmptyValue())
103                     ? new Double JavaDoc(0.0) : getEmptyValue();
104         }
105     }
106
107     /**
108      * A suitable validator.
109      *
110      * @return A suitable validator
111      */

112     protected String JavaDoc 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 JavaDoc[] 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 JavaDoc(inputs[i]).doubleValue()
130                         : ((Double JavaDoc) getEmptyValue()).doubleValue();
131             }
132             setTestValue(values);
133         }
134         else
135         {
136             String JavaDoc val = parser.getString(getKey());
137             setTestValue(StringUtils.isNotEmpty(val)
138                     ? new Double JavaDoc(val) : (Double JavaDoc) getEmptyValue());
139         }
140     }
141     
142 }
143
Popular Tags