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 IntTranslator implements Translator |
31 |
| { |
32 |
| private int _minValue; |
33 |
| private boolean _isMinValue; |
34 |
| private int _maxValue; |
35 |
| private boolean _isMaxValue; |
36 |
| private int _defaultValue = 0; |
37 |
| |
38 |
2
| public IntTranslator()
|
39 |
| { |
40 |
| } |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
5
| public IntTranslator(String initializer)
|
51 |
| { |
52 |
5
| Map m = RuleUtils.convertInitializer(initializer);
|
53 |
| |
54 |
5
| String defaultInit = (String) m.get("default");
|
55 |
| |
56 |
5
| if (defaultInit != null)
|
57 |
3
| _defaultValue = Integer.parseInt(defaultInit);
|
58 |
| |
59 |
5
| String minInit = (String) m.get("min");
|
60 |
5
| if (minInit != null)
|
61 |
| { |
62 |
3
| _isMinValue = true;
|
63 |
3
| _minValue = Integer.parseInt(minInit);
|
64 |
| } |
65 |
| |
66 |
5
| String maxInit = (String) m.get("max");
|
67 |
5
| if (maxInit != null)
|
68 |
| { |
69 |
3
| _isMaxValue = true;
|
70 |
3
| _maxValue = Integer.parseInt(maxInit);
|
71 |
| } |
72 |
| } |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
11
| public Object translate(
|
79 |
| Module contributingModule, |
80 |
| Class propertyType, |
81 |
| String inputValue, |
82 |
| Location location) |
83 |
| { |
84 |
11
| if (HiveMind.isBlank(inputValue))
|
85 |
3
| return new Integer(_defaultValue);
|
86 |
| |
87 |
8
| int value;
|
88 |
| |
89 |
8
| try
|
90 |
| { |
91 |
8
| value = Integer.parseInt(inputValue);
|
92 |
| } |
93 |
| catch (Exception ex) |
94 |
| { |
95 |
2
| throw new ApplicationRuntimeException(
|
96 |
| RulesMessages.invalidIntValue(inputValue), |
97 |
| location, |
98 |
| null); |
99 |
| } |
100 |
| |
101 |
6
| if (_isMinValue && value < _minValue)
|
102 |
2
| throw new ApplicationRuntimeException(RulesMessages.minIntValue(inputValue, _minValue));
|
103 |
| |
104 |
4
| if (_isMaxValue && value > _maxValue)
|
105 |
2
| throw new ApplicationRuntimeException(RulesMessages.maxIntValue(inputValue, _maxValue));
|
106 |
| |
107 |
2
| return new Integer(value);
|
108 |
| } |
109 |
| |
110 |
| } |