KickJava   Java API By Example, From Geeks To Geeks.

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


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.FloatValidator;
23 import org.apache.turbine.services.intake.xmlmodel.XmlField;
24
25 /**
26  * Creates Float Field objects.
27  *
28  * @author <a HREF="mailto:r.wekker@rubicon-bv.com>Ronald Wekker</a>
29  * @author <a HREF="mailto:jmcnally@collab.net>John McNally</a>
30  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
31  * @author <a HREF="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
32  * @version $Id: FloatField.java,v 1.9.2.6 2004/05/20 03:16:39 seade Exp $
33  */

34 public class FloatField
35         extends Field
36 {
37
38     /**
39      * Constructor.
40      *
41      * @param field xml field definition object
42      * @param group xml group definition object
43      * @throws IntakeException thrown by superclass
44      */

45     public FloatField(XmlField field, Group group)
46             throws IntakeException
47     {
48         super(field, group);
49     }
50
51     /**
52      * Sets the default value for an Float Field
53      *
54      * @param prop Parameter for the default values
55      */

56     public void setDefaultValue(String JavaDoc prop)
57     {
58         defaultValue = null;
59
60         if (prop == null)
61         {
62             return;
63         }
64
65         defaultValue = new Float JavaDoc(prop);
66     }
67
68     /**
69      * Set the empty Value. This value is used if Intake
70      * maps a field to a parameter returned by the user and
71      * the corresponding field is either empty (empty string)
72      * or non-existant.
73      *
74      * @param prop The value to use if the field is empty.
75      */

76     public void setEmptyValue(String JavaDoc prop)
77     {
78         emptyValue = null;
79
80         if (prop == null)
81         {
82             return;
83         }
84
85         emptyValue = new Double JavaDoc(prop);
86     }
87
88     /**
89      * Provides access to emptyValue such that the value returned will be
90      * acceptable as an argument parameter to Method.invoke. Subclasses
91      * that deal with primitive types should ensure that they return an
92      * appropriate value wrapped in the object wrapper class for the
93      * primitive type.
94      *
95      * @return the value to use when the field is empty or an Object that
96      * wraps the empty value for primitive types.
97      */

98     protected Object JavaDoc getSafeEmptyValue()
99     {
100         if (isMultiValued)
101         {
102             return new float[0];
103         }
104         else
105         {
106             return (null == getEmptyValue())
107                     ? new Float JavaDoc(0.0) : getEmptyValue();
108         }
109     }
110
111     /**
112      * A suitable validator.
113      *
114      * @return A suitable validator
115      */

116     protected String JavaDoc getDefaultValidator()
117     {
118         return FloatValidator.class.getName();
119     }
120
121     /**
122      * Sets the value of the field from data in the parser.
123      */

124     protected void doSetValue()
125     {
126         if (isMultiValued)
127         {
128             String JavaDoc[] inputs = parser.getStrings(getKey());
129             float[] values = new float[inputs.length];
130             for (int i = 0; i < inputs.length; i++)
131             {
132                 values[i] = StringUtils.isNotEmpty(inputs[i])
133                         ? new Float JavaDoc(inputs[i]).floatValue()
134                         : ((Float JavaDoc) getEmptyValue()).floatValue();
135             }
136             setTestValue(values);
137         }
138         else
139         {
140             String JavaDoc val = parser.getString(getKey());
141             setTestValue(StringUtils.isNotEmpty(val)
142                     ? new Float JavaDoc(val) : getEmptyValue());
143         }
144     }
145     
146 }
147
Popular Tags