1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package it.jnrpe.utils.thresholds;
17
18 import java.math.BigDecimal;
19
20
21
22
23
24
25 abstract class NumberBoundaryStage extends Stage {
26
27
28
29
30
31 protected NumberBoundaryStage(final String stageName) {
32 super(stageName);
33 }
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 @Override
51 public String parse(final String threshold, final RangeConfig tc)
52 throws RangeException {
53 StringBuffer numberString = new StringBuffer();
54 for (int i = 0; i < threshold.length(); i++) {
55 if (Character.isDigit(threshold.charAt(i))) {
56 numberString.append(threshold.charAt(i));
57 continue;
58 }
59 if (threshold.charAt(i) == '.') {
60 if (numberString.toString().endsWith(".")) {
61 numberString.deleteCharAt(numberString.length() - 1);
62 break;
63 } else {
64 numberString.append(threshold.charAt(i));
65 continue;
66 }
67 }
68 if (threshold.charAt(i) == '+' || threshold.charAt(i) == '-') {
69 if (numberString.length() == 0) {
70 numberString.append(threshold.charAt(i));
71 continue;
72 } else {
73 throw new RangeException("Unexpected '"
74 + threshold.charAt(i)
75 + "' sign parsing boundary");
76 }
77 }
78
79
80 break;
81 }
82 if (numberString.length() != 0
83 && !justSign(numberString.toString())) {
84 BigDecimal bd = new BigDecimal(numberString.toString());
85 setBoundary(tc, bd);
86 return threshold.substring(numberString.length());
87 } else {
88 throw new InvalidRangeSyntaxException(this, threshold);
89 }
90 }
91
92
93
94
95
96 private boolean justSign(final String string) {
97 return string.equals("+") || string.equals("-");
98 }
99
100 @Override
101 public boolean canParse(final String threshold) {
102 if (threshold == null || threshold.isEmpty()) {
103 return false;
104 }
105 switch (threshold.charAt(0)) {
106 case '+':
107 case '-':
108 return !(threshold.startsWith("-inf") || threshold
109 .startsWith("+inf"));
110 default:
111 return Character.isDigit(threshold.charAt(0));
112 }
113 }
114
115 @Override
116 public String expects() {
117 return "+-[0-9]";
118 }
119
120
121
122
123
124
125
126
127
128
129
130 public abstract void setBoundary(final RangeConfig tc,
131 final BigDecimal boundary);
132
133
134
135
136
137
138
139
140
141 public static class LeftBoundaryStage extends NumberBoundaryStage {
142
143
144
145
146 protected LeftBoundaryStage() {
147 super("startboundary");
148 }
149
150 @Override
151 public void setBoundary(final RangeConfig tc,
152 final BigDecimal boundary) {
153 tc.setLeftBoundary(boundary);
154 }
155 }
156
157
158
159
160
161
162
163
164
165 public static class RightBoundaryStage extends NumberBoundaryStage {
166
167
168
169
170 protected RightBoundaryStage() {
171 super("rightboundary");
172 }
173
174 @Override
175 public void setBoundary(final RangeConfig tc,
176 final BigDecimal boundary) {
177 tc.setRightBoundary(boundary);
178 }
179
180
181
182
183
184
185 public final boolean isLeaf() {
186 return true;
187 }
188 }
189 }