001 package 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
022 import org.apache.fulcrum.intake.IntakeException;
023 import org.apache.fulcrum.intake.validator.FloatValidator;
024 import org.apache.fulcrum.intake.xmlmodel.XmlField;
025
026 /**
027 * Processor for float fields.
028 *
029 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
030 * @version $Id: FloatField.java 663702 2008-06-05 19:01:36Z tv $
031 */
032 public class FloatField
033 extends Field
034 {
035 /**
036 * Constructor.
037 *
038 * @param field xml field definition object
039 * @param group xml group definition object
040 * @throws IntakeException thrown by superclass
041 */
042 public FloatField(XmlField field, Group group)
043 throws IntakeException
044 {
045 super(field, group);
046 }
047
048 /**
049 * Sets the default value for an Float Field
050 *
051 * @param prop Parameter for the default values
052 */
053 public void setDefaultValue(String prop)
054 {
055 defaultValue = null;
056
057 if (prop == null)
058 {
059 return;
060 }
061
062 defaultValue = new Float(prop);
063 }
064
065 /**
066 * Set the empty Value. This value is used if Intake
067 * maps a field to a parameter returned by the user and
068 * the corresponding field is either empty (empty string)
069 * or non-existant.
070 *
071 * @param prop The value to use if the field is empty.
072 */
073 public void setEmptyValue(String prop)
074 {
075 emptyValue = null;
076
077 if (prop == null)
078 {
079 return;
080 }
081
082 emptyValue = new Double(prop);
083 }
084
085 /**
086 * Provides access to emptyValue such that the value returned will be
087 * acceptable as an argument parameter to Method.invoke. Subclasses
088 * that deal with primitive types should ensure that they return an
089 * appropriate value wrapped in the object wrapper class for the
090 * primitive type.
091 *
092 * @return the value to use when the field is empty or an Object that
093 * wraps the empty value for primitive types.
094 */
095 protected Object getSafeEmptyValue()
096 {
097 if (isMultiValued)
098 {
099 return new float[0];
100 }
101 else
102 {
103 return (null == getEmptyValue())
104 ? new Float(0.0) : getEmptyValue();
105 }
106 }
107
108 /**
109 * A suitable validator.
110 *
111 * @return A suitable validator
112 */
113 protected String getDefaultValidator()
114 {
115 return FloatValidator.class.getName();
116 }
117
118 /**
119 * Sets the value of the field from data in the parser.
120 */
121 protected void doSetValue()
122 {
123 if (isMultiValued)
124 {
125 Float[] inputs = parser.getFloatObjects(getKey());
126 float[] values = new float[inputs.length];
127
128 for (int i = 0; i < inputs.length; i++)
129 {
130 values[i] = inputs[i] == null
131 ? ((Float) getEmptyValue()).floatValue()
132 : inputs[i].floatValue();
133 }
134
135 setTestValue(values);
136 }
137 else
138 {
139 setTestValue(parser.getFloatObject(getKey(), (Float)getEmptyValue()));
140 }
141 }
142
143 }