|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DoubleTranslator.java | 100% | 100% | 100% | 100% |
|
1 |
// Copyright 2004 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.hivemind.schema.rules;
|
|
16 |
|
|
17 |
import java.util.Map;
|
|
18 |
|
|
19 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
20 |
import org.apache.hivemind.HiveMind;
|
|
21 |
import org.apache.hivemind.internal.Module;
|
|
22 |
import org.apache.hivemind.schema.Translator;
|
|
23 |
|
|
24 |
/**
|
|
25 |
* Translates strings to integer values.
|
|
26 |
*
|
|
27 |
* @author Howard Lewis Ship
|
|
28 |
*/
|
|
29 |
public class DoubleTranslator implements Translator |
|
30 |
{ |
|
31 |
private double _minValue; |
|
32 |
private boolean _isMinValue; |
|
33 |
private double _maxValue; |
|
34 |
private boolean _isMaxValue; |
|
35 |
private double _defaultValue = 0; |
|
36 |
|
|
37 | 2 |
public DoubleTranslator()
|
38 |
{ |
|
39 |
} |
|
40 |
|
|
41 |
/**
|
|
42 |
* Initializers:
|
|
43 |
* <ul>
|
|
44 |
* <li>default: default value for empty or invalid input
|
|
45 |
* <li>min: minimum acceptible value
|
|
46 |
* <li>max: maximum acceptible value
|
|
47 |
* </ul>
|
|
48 |
*/
|
|
49 | 4 |
public DoubleTranslator(String initializer)
|
50 |
{ |
|
51 | 4 |
Map m = RuleUtils.convertInitializer(initializer); |
52 |
|
|
53 | 4 |
String defaultInit = (String) m.get("default");
|
54 |
|
|
55 | 4 |
if (defaultInit != null) |
56 | 2 |
_defaultValue = Double.parseDouble(defaultInit); |
57 |
|
|
58 | 4 |
String minInit = (String) m.get("min");
|
59 | 4 |
if (minInit != null) |
60 |
{ |
|
61 | 2 |
_isMinValue = true;
|
62 | 2 |
_minValue = Double.parseDouble(minInit); |
63 |
} |
|
64 |
|
|
65 | 4 |
String maxInit = (String) m.get("max");
|
66 | 4 |
if (maxInit != null) |
67 |
{ |
|
68 | 2 |
_isMaxValue = true;
|
69 | 2 |
_maxValue = Double.parseDouble(maxInit); |
70 |
} |
|
71 |
} |
|
72 |
|
|
73 |
/**
|
|
74 |
* Converts the string to an Double. The empty string is returned as zero.
|
|
75 |
* On failure, an error is logged and the method returns zero.
|
|
76 |
*/
|
|
77 | 6 |
public Object translate(Module contributingModule, Class propertyType, String inputValue)
|
78 |
{ |
|
79 | 6 |
if (HiveMind.isBlank(inputValue))
|
80 | 2 |
return new Double(_defaultValue); |
81 |
|
|
82 | 4 |
double value;
|
83 |
|
|
84 | 4 |
try
|
85 |
{ |
|
86 | 4 |
value = Double.parseDouble(inputValue); |
87 |
} |
|
88 |
catch (Exception ex)
|
|
89 |
{ |
|
90 | 1 |
throw new ApplicationRuntimeException(RulesMessages.invalidDoubleValue(inputValue), ex); |
91 |
} |
|
92 |
|
|
93 | 3 |
if (_isMinValue && value < _minValue)
|
94 | 1 |
throw new ApplicationRuntimeException( |
95 |
RulesMessages.minDoubleValue(inputValue, _minValue)); |
|
96 |
|
|
97 | 2 |
if (_isMaxValue && value > _maxValue)
|
98 | 1 |
throw new ApplicationRuntimeException( |
99 |
RulesMessages.maxDoubleValue(inputValue, _maxValue)); |
|
100 |
|
|
101 | 1 |
return new Double(value); |
102 |
} |
|
103 |
} |
|
104 |
|
|