1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
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 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| public class LongTranslator implements Translator |
31 |
| { |
32 |
| private long _minValue; |
33 |
| private boolean _isMinValue; |
34 |
| private long _maxValue; |
35 |
| private boolean _isMaxValue; |
36 |
| private long _defaultValue = 0; |
37 |
| |
38 |
3
| public LongTranslator()
|
39 |
| { |
40 |
| } |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
4
| public LongTranslator(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 = Long.parseLong(defaultInit);
|
58 |
| |
59 |
4
| String minInit = (String) m.get("min");
|
60 |
4
| if (minInit != null)
|
61 |
| { |
62 |
2
| _isMinValue = true;
|
63 |
2
| _minValue = Long.parseLong(minInit);
|
64 |
| } |
65 |
| |
66 |
4
| String maxInit = (String) m.get("max");
|
67 |
4
| if (maxInit != null)
|
68 |
| { |
69 |
2
| _isMaxValue = true;
|
70 |
2
| _maxValue = Long.parseLong(maxInit);
|
71 |
| } |
72 |
| } |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
12
| public Object translate(
|
79 |
| Module contributingModule, |
80 |
| Class propertyType, |
81 |
| String inputValue, |
82 |
| Location location) |
83 |
| { |
84 |
12
| if (HiveMind.isBlank(inputValue))
|
85 |
2
| return new Long(_defaultValue);
|
86 |
| |
87 |
10
| long value;
|
88 |
| |
89 |
10
| try
|
90 |
| { |
91 |
10
| value = Long.parseLong(inputValue);
|
92 |
| } |
93 |
| catch (Exception ex) |
94 |
| { |
95 |
1
| throw new ApplicationRuntimeException(
|
96 |
| RulesMessages.invalidLongValue(inputValue), |
97 |
| location, |
98 |
| ex); |
99 |
| } |
100 |
| |
101 |
9
| if (_isMinValue && value < _minValue)
|
102 |
1
| throw new ApplicationRuntimeException(
|
103 |
| RulesMessages.minLongValue(inputValue, _minValue)); |
104 |
| |
105 |
8
| if (_isMaxValue && value > _maxValue)
|
106 |
1
| throw new ApplicationRuntimeException(
|
107 |
| RulesMessages.maxLongValue(inputValue, _maxValue)); |
108 |
| |
109 |
7
| return new Long(value);
|
110 |
| |
111 |
| } |
112 |
| |
113 |
| } |