View Javadoc
1   package org.apache.fulcrum.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.fulcrum.intake.IntakeException;
23  import org.apache.fulcrum.intake.validator.DoubleValidator;
24  
25  /**
26   * Processor for double fields.
27   *
28   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
29   * @version $Id$
30   */
31  public class DoubleField
32          extends Field<Double>
33  {
34      /** Serial version */
35  	private static final long serialVersionUID = -1685467778904884936L;
36  
37  	/**
38       * Constructor.
39       *
40       * @param field xml field definition object
41       * @param group xml group definition object
42       * @throws IntakeException thrown by superclass
43       */
44      public DoubleField(XmlField field, Group group)
45              throws IntakeException
46      {
47          super(field, group);
48      }
49  
50      /**
51       * Sets the default value for a Double Field
52       *
53       * @param prop Parameter for the default values
54       */
55      @Override
56  	public void setDefaultValue(String prop)
57      {
58          defaultValue = null;
59  
60          if (prop == null)
61          {
62              return;
63          }
64  
65          defaultValue = new Double(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      @Override
77  	public void setEmptyValue(String prop)
78      {
79          emptyValue = null;
80  
81          if (prop == null)
82          {
83              return;
84          }
85  
86          emptyValue = new Double(prop);
87      }
88  
89      /**
90       * Provides access to emptyValue such that the value returned will be
91       * acceptable as an argument parameter to Method.invoke.  Subclasses
92       * that deal with primitive types should ensure that they return an
93       * appropriate value wrapped in the object wrapper class for the
94       * primitive type.
95       *
96       * @return the value to use when the field is empty or an Object that
97       * wraps the empty value for primitive types.
98       */
99      @Override
100 	protected Object getSafeEmptyValue()
101     {
102         if (isMultiValued())
103         {
104             return new double[0];
105         }
106         else
107         {
108             return (null == getEmptyValue())
109                     ? new Double(0.0) : getEmptyValue();
110         }
111     }
112 
113     /**
114      * A suitable validator.
115      *
116      * @return A suitable validator
117      */
118     @Override
119 	protected String getDefaultValidator()
120     {
121         return DoubleValidator.class.getName();
122     }
123 
124     /**
125      * Sets the value of the field from data in the parser.
126      */
127     @Override
128 	protected void doSetValue()
129     {
130         if (isMultiValued())
131         {
132             Double[] inputs = parser.getDoubleObjects(getKey());
133             double[] values = new double[inputs.length];
134 
135             for (int i = 0; i < inputs.length; i++)
136             {
137                 values[i] = inputs[i] == null
138                         ? getEmptyValue().doubleValue()
139                         : inputs[i].doubleValue();
140             }
141 
142             setTestValue(values);
143         }
144         else
145         {
146             setTestValue(parser.getDoubleObject(getKey(), getEmptyValue()));
147         }
148     }
149 
150 }