1 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 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 public DoubleTranslator() 39 { 40 } 41 42 50 public DoubleTranslator(String initializer) 51 { 52 Map m = RuleUtils.convertInitializer(initializer); 53 54 String defaultInit = (String ) m.get("default"); 55 56 if (defaultInit != null) 57 _defaultValue = Double.parseDouble(defaultInit); 58 59 String minInit = (String ) m.get("min"); 60 if (minInit != null) 61 { 62 _isMinValue = true; 63 _minValue = Double.parseDouble(minInit); 64 } 65 66 String maxInit = (String ) m.get("max"); 67 if (maxInit != null) 68 { 69 _isMaxValue = true; 70 _maxValue = Double.parseDouble(maxInit); 71 } 72 } 73 74 78 public Object translate( 79 Module contributingModule, 80 Class propertyType, 81 String inputValue, 82 Location location) 83 { 84 if (HiveMind.isBlank(inputValue)) 85 return new Double (_defaultValue); 86 87 double value; 88 89 try 90 { 91 value = Double.parseDouble(inputValue); 92 } 93 catch (Exception ex) 94 { 95 throw new ApplicationRuntimeException( 96 RulesMessages.invalidDoubleValue(inputValue), 97 location, 98 ex); 99 } 100 101 if (_isMinValue && value < _minValue) 102 throw new ApplicationRuntimeException( 103 RulesMessages.minDoubleValue(inputValue, _minValue)); 104 105 if (_isMaxValue && value > _maxValue) 106 throw new ApplicationRuntimeException( 107 RulesMessages.maxDoubleValue(inputValue, _maxValue)); 108 109 return new Double (value); 110 } 111 } 112 | Popular Tags |