View Javadoc

1   package org.apache.turbine.services.intake.model;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.turbine.services.intake.IntakeException;
23  import org.apache.turbine.services.intake.validator.FloatValidator;
24  import org.apache.turbine.services.intake.xmlmodel.XmlField;
25  
26  /***
27   * Creates Float Field objects.
28   *
29   * @author <a href="mailto:r.wekker@rubicon-bv.com>Ronald Wekker</a>
30   * @author <a href="mailto:jmcnally@collab.net>John McNally</a>
31   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
32   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
33   * @version $Id: FloatField.java 646288 2008-04-09 11:51:16Z tv $
34   */
35  public class FloatField
36          extends Field
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 prop)
57      {
58          defaultValue = null;
59  
60          if (prop == null)
61          {
62              return;
63          }
64  
65          defaultValue = new Float(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 prop)
77      {
78          emptyValue = null;
79  
80          if (prop == null)
81          {
82              return;
83          }
84  
85          emptyValue = new Double(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 getSafeEmptyValue()
99      {
100         if (isMultiValued)
101         {
102             return new float[0];
103         }
104         else
105         {
106             return (null == getEmptyValue())
107                     ? new Float(0.0) : getEmptyValue();
108         }
109     }
110 
111     /***
112      * A suitable validator.
113      *
114      * @return A suitable validator
115      */
116     protected String 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             Float[] inputs = parser.getFloatObjects(getKey());
129             float[] values = new float[inputs.length];
130 
131             for (int i = 0; i < inputs.length; i++)
132             {
133                 values[i] = inputs[i] == null 
134                         ? ((Float) getEmptyValue()).floatValue() 
135                         : inputs[i].floatValue();
136             }
137 
138             setTestValue(values);
139         }
140         else
141         {
142             setTestValue(parser.getFloatObject(getKey(), (Float)getEmptyValue()));
143         }
144     }
145 
146 }