001package org.apache.fulcrum.intake.model;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.fulcrum.intake.IntakeException;
023import org.apache.fulcrum.intake.validator.FloatValidator;
024
025/**
026 * Processor for float fields.
027 *
028 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
029 * @version $Id$
030 */
031public class FloatField
032        extends Field<Float>
033{
034    /** Serial version */
035        private static final long serialVersionUID = -8878247008849251720L;
036
037        /**
038     * Constructor.
039     *
040     * @param field xml field definition object
041     * @param group xml group definition object
042     * @throws IntakeException thrown by superclass
043     */
044    public FloatField(XmlField field, Group group)
045            throws IntakeException
046    {
047        super(field, group);
048    }
049
050    /**
051     * Sets the default value for an Float Field
052     *
053     * @param prop Parameter for the default values
054     */
055    @Override
056        public void setDefaultValue(String prop)
057    {
058        defaultValue = null;
059
060        if (prop == null)
061        {
062            return;
063        }
064
065        defaultValue = new Float(prop);
066    }
067
068    /**
069     * Set the empty Value. This value is used if Intake
070     * maps a field to a parameter returned by the user and
071     * the corresponding field is either empty (empty string)
072     * or non-existent.
073     *
074     * @param prop The value to use if the field is empty.
075     */
076    @Override
077        public void setEmptyValue(String prop)
078    {
079        emptyValue = null;
080
081        if (prop == null)
082        {
083            return;
084        }
085
086        emptyValue = new Float(prop);
087    }
088
089    /**
090     * Provides access to emptyValue such that the value returned will be
091     * acceptable as an argument parameter to Method.invoke.  Subclasses
092     * that deal with primitive types should ensure that they return an
093     * appropriate value wrapped in the object wrapper class for the
094     * primitive type.
095     *
096     * @return the value to use when the field is empty or an Object that
097     * wraps the empty value for primitive types.
098     */
099    @Override
100        protected Object getSafeEmptyValue()
101    {
102        if (isMultiValued())
103        {
104            return new float[0];
105        }
106        else
107        {
108            return (null == getEmptyValue())
109                    ? new Float(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 FloatValidator.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            Float[] inputs = parser.getFloatObjects(getKey());
133            float[] values = new float[inputs.length];
134
135            for (int i = 0; i < inputs.length; i++)
136            {
137                values[i] = inputs[i] == null
138                        ? getEmptyValue().floatValue()
139                        : inputs[i].floatValue();
140            }
141
142            setTestValue(values);
143        }
144        else
145        {
146            setTestValue(parser.getFloatObject(getKey(), getEmptyValue()));
147        }
148    }
149
150}