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.FloatValidator; 24 25 /** 26 * Processor for float fields. 27 * 28 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a> 29 * @version $Id$ 30 */ 31 public class FloatField 32 extends Field<Float> 33 { 34 /** Serial version */ 35 private static final long serialVersionUID = -8878247008849251720L; 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 FloatField(XmlField field, Group group) 45 throws IntakeException 46 { 47 super(field, group); 48 } 49 50 /** 51 * Sets the default value for an Float 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 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-existent. 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 Float(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 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 }