01: // Copyright 2004, 2005 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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: private double _minValue;
32: private boolean _isMinValue;
33: private double _maxValue;
34: private boolean _isMaxValue;
35: private double _defaultValue = 0;
36:
37: public DoubleTranslator() {
38: }
39:
40: /**
41: * Initializers:
42: * <ul>
43: * <li>default: default value for empty or invalid input
44: * <li>min: minimum acceptible value
45: * <li>max: maximum acceptible value
46: * </ul>
47: */
48: public DoubleTranslator(String initializer) {
49: Map m = RuleUtils.convertInitializer(initializer);
50:
51: String defaultInit = (String) m.get("default");
52:
53: if (defaultInit != null)
54: _defaultValue = Double.parseDouble(defaultInit);
55:
56: String minInit = (String) m.get("min");
57: if (minInit != null) {
58: _isMinValue = true;
59: _minValue = Double.parseDouble(minInit);
60: }
61:
62: String maxInit = (String) m.get("max");
63: if (maxInit != null) {
64: _isMaxValue = true;
65: _maxValue = Double.parseDouble(maxInit);
66: }
67: }
68:
69: /**
70: * Converts the string to an Double. The empty string is returned as zero.
71: * On failure, an error is logged and the method returns zero.
72: */
73: public Object translate(Module contributingModule,
74: Class propertyType, String inputValue, Location location) {
75: if (HiveMind.isBlank(inputValue))
76: return new Double(_defaultValue);
77:
78: double value;
79:
80: try {
81: value = Double.parseDouble(inputValue);
82: } catch (Exception ex) {
83: throw new ApplicationRuntimeException(RulesMessages
84: .invalidDoubleValue(inputValue), location, ex);
85: }
86:
87: if (_isMinValue && value < _minValue)
88: throw new ApplicationRuntimeException(RulesMessages
89: .minDoubleValue(inputValue, _minValue));
90:
91: if (_isMaxValue && value > _maxValue)
92: throw new ApplicationRuntimeException(RulesMessages
93: .maxDoubleValue(inputValue, _maxValue));
94:
95: return new Double(value);
96: }
97: }
|