@@ -42,10 +42,7 @@ public class ConditionalParser {
42
42
private final Set <String > restrictedValues ;
43
43
private final List <ConditionalValueParser > valueParsers = new ArrayList <>(5 );
44
44
private final boolean enabledLogs ;
45
-
46
- private String simpleValue ;
47
- private String unevaluatedRestrictions = "" ;
48
- private Conditions unevaluatedConditions ;
45
+ private final String simpleValue ;
49
46
50
47
public ConditionalParser (Set <String > restrictedValues ) {
51
48
this (restrictedValues , false );
@@ -55,12 +52,7 @@ public ConditionalParser(Set<String> restrictedValues, boolean enabledLogs) {
55
52
// use map => key & type (date vs. double)
56
53
this .restrictedValues = restrictedValues ;
57
54
this .enabledLogs = enabledLogs ;
58
-
59
- if (hasRestrictedValues ()) {
60
- if (restrictedValues .contains ("yes" ))
61
- this .simpleValue = "yes" ;
62
- else this .simpleValue = "no" ;
63
- }
55
+ this .simpleValue = hasRestrictedValues () && restrictedValues .contains ("yes" ) ? "yes" : "no" ;
64
56
}
65
57
66
58
public static ConditionalValueParser createNumberParser (final String assertKey , final Number obj ) {
@@ -155,38 +147,112 @@ public ConditionalParser setConditionalValueParser(ConditionalValueParser vp) {
155
147
}
156
148
157
149
// attempt to parse the value with any of the registered parsers
158
- private boolean checkAtomicCondition (Condition condition ) throws ParseException {
159
- for (ConditionalValueParser valueParser : valueParsers ) {
160
- ConditionalValueParser .ConditionState c = valueParser .checkCondition (condition );
161
- if (c .isValid ()) {
162
- if (c .isEvaluated ())
163
- return c .isCheckPassed ();
164
- else { // condition could not be evaluated but might evaluate to true during query
165
- unevaluatedConditions .addCondition (c .getCondition ());
166
- return true ;
150
+ private ParsedCondition checkAtomicCondition (Condition condition , ParsedCondition parsedCondition ) throws ParseException {
151
+ parsedCondition .reset ();
152
+ try {
153
+ for (ConditionalValueParser valueParser : valueParsers ) {
154
+ ConditionalValueParser .ConditionState conditionState = valueParser .checkCondition (condition );
155
+ if (conditionState .isValid ()) {
156
+ parsedCondition .setValid (true );
157
+ if (conditionState .isEvaluated ()) {
158
+ parsedCondition .setEvaluated (true );
159
+ parsedCondition .setCheckPassed (conditionState .isCheckPassed ());
160
+ break ;
161
+ } else { // condition could not be evaluated but might evaluate to true during query
162
+ parsedCondition .setLazyEvaluated (true );
163
+ parsedCondition .getLazyEvaluatedConditions ().add (conditionState .getCondition ());
164
+ }
167
165
}
168
166
}
169
167
}
170
- return false ;
168
+ catch (ParseException e ) {
169
+ throw e ;
170
+ }
171
+ finally {
172
+ return parsedCondition ;
173
+ }
174
+ }
175
+
176
+ class ParsedCondition {
177
+ private boolean valid ;
178
+ private boolean evaluated ;
179
+ private boolean checkPassed ;
180
+ private boolean lazyEvaluated ;
181
+ private ArrayList <Condition > lazyEvaluatedConditions = new ArrayList <Condition >();
182
+
183
+ void reset () {
184
+ valid = evaluated = checkPassed = lazyEvaluated = false ;
185
+ }
186
+
187
+ void setValid (boolean valid ) {
188
+ this .valid = valid ;
189
+ }
190
+
191
+ void setEvaluated (boolean evaluated ) {
192
+ this .evaluated = evaluated ;
193
+ }
194
+
195
+ void setCheckPassed (boolean checkPassed ) {
196
+ this .checkPassed = checkPassed ;
197
+ }
198
+
199
+ void setLazyEvaluated (boolean lazyEvaluated ) {
200
+ this .lazyEvaluated = lazyEvaluated ;
201
+ }
202
+
203
+ boolean isValid () {
204
+ return valid ;
205
+ }
206
+
207
+ boolean isEvaluated () {
208
+ return evaluated ;
209
+ }
210
+
211
+ boolean isCheckPassed () {
212
+ return checkPassed ;
213
+ }
214
+
215
+ boolean isLazyEvaluated () {
216
+ return lazyEvaluated ;
217
+ }
218
+
219
+ boolean isInvalidOrFalse () {
220
+ return !valid || (!lazyEvaluated && evaluated && !checkPassed );
221
+ }
222
+
223
+ ArrayList <Condition > getLazyEvaluatedConditions () {
224
+ return lazyEvaluatedConditions ;
225
+ }
171
226
}
172
227
173
228
// all of the combined conditions need to be met
174
- private boolean checkCombinedCondition (List <Condition > conditions ) throws ParseException {
229
+ private ParsedCondition checkCombinedCondition (Restriction restriction ) throws ParseException {
230
+ ParsedCondition parsedCondition = new ParsedCondition ();
175
231
// combined conditions, must be all matched
176
- for (Condition condition : conditions ) {
177
- if (!checkAtomicCondition (condition ))
178
- return false ;
232
+ boolean checkPassed = true ;
233
+ boolean lazyEvaluated = false ;
234
+ for (Condition condition : restriction .getConditions ()) {
235
+ parsedCondition = checkAtomicCondition (condition , parsedCondition );
236
+ checkPassed = checkPassed && parsedCondition .isCheckPassed ();
237
+ if (parsedCondition .isInvalidOrFalse ()) {
238
+ lazyEvaluated = false ;
239
+ break ;
240
+ }
241
+ if (parsedCondition .isLazyEvaluated ())
242
+ lazyEvaluated = true ;
179
243
}
180
- return true ;
244
+ parsedCondition .setLazyEvaluated (lazyEvaluated );
245
+ parsedCondition .setCheckPassed (checkPassed );
246
+ return parsedCondition ;
181
247
}
182
248
183
249
184
- public boolean checkCondition (String tagValue ) throws ParseException {
250
+ public Result checkCondition (String tagValue ) throws ParseException {
251
+ Result result = new Result ();
185
252
if (tagValue == null || tagValue .isEmpty () || !tagValue .contains ("@" ))
186
- return false ;
253
+ return result ;
187
254
188
255
ArrayList <Restriction > parsedRestrictions = new ArrayList <>();
189
- unevaluatedRestrictions = "" ;
190
256
191
257
try {
192
258
ConditionalRestrictionParser parser = new ConditionalRestrictionParser (new ByteArrayInputStream (tagValue .getBytes ()));
@@ -200,52 +266,40 @@ public boolean checkCondition(String tagValue) throws ParseException {
200
266
String restrictionValue = restriction .getValue ();
201
267
202
268
if (hasRestrictedValues ()) {
203
- // check whether the encountered value is on the list
204
- if (!restrictedValues .contains (restrictionValue ))
269
+ if (restrictedValues .contains (restrictionValue ))
270
+ restrictionValue = simpleValue ;
271
+ else
205
272
continue ;
206
273
}
207
274
else {
208
- simpleValue = restrictionValue ;
275
+ result . setRestrictions ( restrictionValue ) ;
209
276
}
210
277
211
- List < Condition > conditions = restriction . getConditions ( );
212
-
213
- unevaluatedConditions = new Conditions ( new ArrayList < Condition >(), restriction . inParen () );
278
+ ParsedCondition parsedConditions = checkCombinedCondition ( restriction );
279
+ boolean checkPassed = parsedConditions . isCheckPassed ();
280
+ result . setCheckPassed ( result . isCheckPassed () || checkPassed );
214
281
215
- if (checkCombinedCondition (conditions )) {
216
- // check for unevaluated conditions
217
- if (unevaluatedConditions .getConditions ().isEmpty ()) {
218
- return true ; // terminate once the first matching condition which can be fully evaluated is encountered
219
- }
220
- else {
221
- parsedRestrictions .add (new Restriction (simpleValue , unevaluatedConditions ));
222
- }
282
+ // check for unevaluated conditions
283
+ if (!parsedConditions .isLazyEvaluated ()) {
284
+ if (checkPassed )
285
+ return result ; // terminate once the first matching condition which can be fully evaluated is encountered
286
+ }
287
+ else {
288
+ parsedRestrictions .add (0 , new Restriction (restrictionValue , new Conditions (parsedConditions .getLazyEvaluatedConditions (), restriction .inParen ())));
223
289
}
224
290
}
225
291
} catch (ch .poole .conditionalrestrictionparser .ParseException e ) {
226
292
if (enabledLogs )
227
293
logger .warn ("Parser exception for " + tagValue + " " + e .toString ());
228
- return false ;
229
- }
230
- // at this point either no matching restriction was found or the encountered restrictions need to be lazy evaluated
231
- if (parsedRestrictions .isEmpty ()) {
232
- return false ;
233
- }
234
- else {
235
- unevaluatedRestrictions = Util .restrictionsToString (parsedRestrictions );
236
- return true ;
294
+ return result ;
237
295
}
238
- }
239
296
240
- public String getRestrictions () {
241
- if (hasUnevaluatedRestrictions ())
242
- return unevaluatedRestrictions ;
243
- else
244
- return simpleValue ;
245
- }
297
+ if (!parsedRestrictions .isEmpty ()) {
298
+ result .setRestrictions (Util .restrictionsToString (parsedRestrictions ));
299
+ result .setLazyEvaluated (true );
300
+ }
246
301
247
- public boolean hasUnevaluatedRestrictions () {
248
- return !unevaluatedRestrictions .isEmpty ();
302
+ return result ;
249
303
}
250
304
251
305
protected static double parseNumber (String str ) {
@@ -260,4 +314,34 @@ protected static double parseNumber(String str) {
260
314
private boolean hasRestrictedValues () {
261
315
return !( restrictedValues ==null || restrictedValues .isEmpty () );
262
316
}
317
+
318
+ class Result {
319
+ private boolean checkPassed ;
320
+ private boolean lazyEvaluated ;
321
+ private String restrictions ;
322
+
323
+ boolean isCheckPassed () {
324
+ return checkPassed ;
325
+ }
326
+
327
+ void setCheckPassed (boolean checkPassed ) {
328
+ this .checkPassed = checkPassed ;
329
+ }
330
+
331
+ boolean isLazyEvaluated () {
332
+ return lazyEvaluated ;
333
+ }
334
+
335
+ void setLazyEvaluated (boolean lazyEvaluated ) {
336
+ this .lazyEvaluated = lazyEvaluated ;
337
+ }
338
+
339
+ String getRestrictions () {
340
+ return restrictions ;
341
+ }
342
+
343
+ void setRestrictions (String restrictions ) {
344
+ this .restrictions = restrictions ;
345
+ }
346
+ }
263
347
}
0 commit comments