Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FloatField |
|
| 2.6666666666666665;2.667 |
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: FloatField.java 1823858 2018-02-11 17:23:06Z tv $ | |
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 | 2 | super(field, group); |
48 | 2 | } |
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 | 2 | defaultValue = null; |
59 | ||
60 | 2 | if (prop == null) |
61 | { | |
62 | 2 | return; |
63 | } | |
64 | ||
65 | 0 | defaultValue = new Float(prop); |
66 | 0 | } |
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 | 2 | emptyValue = null; |
80 | ||
81 | 2 | if (prop == null) |
82 | { | |
83 | 2 | return; |
84 | } | |
85 | ||
86 | 0 | emptyValue = new Float(prop); |
87 | 0 | } |
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 | 0 | if (isMultiValued()) |
103 | { | |
104 | 0 | return new float[0]; |
105 | } | |
106 | else | |
107 | { | |
108 | 0 | return (null == getEmptyValue()) |
109 | 0 | ? 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 | 2 | 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 | 0 | if (isMultiValued()) |
131 | { | |
132 | 0 | Float[] inputs = parser.getFloatObjects(getKey()); |
133 | 0 | float[] values = new float[inputs.length]; |
134 | ||
135 | 0 | for (int i = 0; i < inputs.length; i++) |
136 | { | |
137 | 0 | values[i] = inputs[i] == null |
138 | 0 | ? getEmptyValue().floatValue() |
139 | 0 | : inputs[i].floatValue(); |
140 | } | |
141 | ||
142 | 0 | setTestValue(values); |
143 | 0 | } |
144 | else | |
145 | { | |
146 | 0 | setTestValue(parser.getFloatObject(getKey(), getEmptyValue())); |
147 | } | |
148 | 0 | } |
149 | ||
150 | } |