|
|||||||||||||||||||
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.Location;
|
|
22 |
import org.apache.hivemind.internal.Module;
|
|
23 |
import org.apache.hivemind.schema.Translator;
|
|
24 |
|
|
25 |
/**
|
|
26 |
* Translates strings to integer values.
|
|
27 |
*
|
|
28 |
* @author Howard Lewis Ship
|
|
29 |
*/
|
|
30 |
public class DoubleTranslator implements Translator |
|
31 |
{ |
|
32 |
private double _minValue; |
|
33 |
private boolean _isMinValue; |
|
34 |
private double _maxValue; |
|
35 |
private boolean _isMaxValue; |
|
36 |
private double _defaultValue = 0; |
|
37 |
|
|
38 | 2 |
public DoubleTranslator()
|
39 |
{ |
|
40 |
} |
|
41 |
|
|
42 |
/**
|
|
43 |
* Initializers:
|
|
44 |
* <ul>
|
|
45 |
* <li>default: default value for empty or invalid input
|
|
46 |
* <li>min: minimum acceptible value
|
|
47 |
* <li>max: maximum acceptible value
|
|
48 |
* </ul>
|
|
49 |
*/
|
|
50 | 4 |
public DoubleTranslator(String initializer)
|
51 |
{ |
|
52 | 4 |
Map m = RuleUtils.convertInitializer(initializer); |
53 |
|
|
54 | 4 |
String defaultInit = (String) m.get("default");
|
55 |
|
|
56 | 4 |
if (defaultInit != null) |
57 | 2 |
_defaultValue = Double.parseDouble(defaultInit); |
58 |
|
|
59 | 4 |
String minInit = (String) m.get("min");
|
60 | 4 |
if (minInit != null) |
61 |
{ |
|
62 | 2 |
_isMinValue = true;
|
63 | 2 |
_minValue = Double.parseDouble(minInit); |
64 |
} |
|
65 |
|
|
66 | 4 |
String maxInit = (String) m.get("max");
|
67 | 4 |
if (maxInit != null) |
68 |
{ |
|
69 | 2 |
_isMaxValue = true;
|
70 | 2 |
_maxValue = Double.parseDouble(maxInit); |
71 |
} |
|
72 |
} |
|
73 |
|
|
74 |
/**
|
|
75 |
* Converts the string to an Double. The empty string is returned as zero.
|
|
76 |
* On failure, an error is logged and the method returns zero.
|
|
77 |
*/
|
|
78 | 6 |
public Object translate(
|
79 |
Module contributingModule, |
|
80 |
Class propertyType, |
|
81 |
String inputValue, |
|
82 |
Location location) |
|
83 |
{ |
|
84 | 6 |
if (HiveMind.isBlank(inputValue))
|
85 | 2 |
return new Double(_defaultValue); |
86 |
|
|
87 | 4 |
double value;
|
88 |
|
|
89 | 4 |
try
|
90 |
{ |
|
91 | 4 |
value = Double.parseDouble(inputValue); |
92 |
} |
|
93 |
catch (Exception ex)
|
|
94 |
{ |
|
95 | 1 |
throw new ApplicationRuntimeException( |
96 |
RulesMessages.invalidDoubleValue(inputValue), |
|
97 |
location, |
|
98 |
ex); |
|
99 |
} |
|
100 |
|
|
101 | 3 |
if (_isMinValue && value < _minValue)
|
102 | 1 |
throw new ApplicationRuntimeException( |
103 |
RulesMessages.minDoubleValue(inputValue, _minValue)); |
|
104 |
|
|
105 | 2 |
if (_isMaxValue && value > _maxValue)
|
106 | 1 |
throw new ApplicationRuntimeException( |
107 |
RulesMessages.maxDoubleValue(inputValue, _maxValue)); |
|
108 |
|
|
109 | 1 |
return new Double(value); |
110 |
} |
|
111 |
} |
|
112 |
|
|