Skip to content

Commit 23a2f6f

Browse files
committed
added toBoolean to get safe boolean representation
1 parent 62be5c6 commit 23a2f6f

File tree

1 file changed

+26
-14
lines changed
  • jcp/src/main/java/com/igormaznitsa/jcp/expression

1 file changed

+26
-14
lines changed

jcp/src/main/java/com/igormaznitsa/jcp/expression/Value.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -252,51 +252,63 @@ public Boolean asBoolean() {
252252
public String toStringDetail() {
253253
switch (type) {
254254
case BOOLEAN: {
255-
return "Boolean : " + value;
255+
return "Boolean: " + value;
256256
}
257257
case INT: {
258-
return "Integer : " + value;
258+
return "Integer: " + value;
259259
}
260260
case UNKNOWN: {
261-
return "Unknown : -";
261+
return "Unknown: -";
262262
}
263263
case FLOAT: {
264-
return "Float : " + value;
264+
return "Float: " + value;
265265
}
266266
case STRING: {
267-
return "String : " + value;
267+
return "String: " + value;
268268
}
269269
}
270-
return "!!! ERROR , UNSUPPORTED TYPE [" + type + "]";
270+
return "Unexpected unsupported type, contact developer [" + type + "]";
271271
}
272272

273273
@Override
274-
275274
public String toString() {
276-
switch (type) {
275+
switch (this.type) {
277276
case BOOLEAN:
278-
return asBoolean().toString();
277+
return this.asBoolean().toString();
279278
case INT:
280-
return asLong().toString();
279+
return this.asLong().toString();
281280
case UNKNOWN:
282281
return "<UNKNOWN>";
283282
case FLOAT:
284-
return asFloat().toString();
283+
return this.asFloat().toString();
285284
case STRING:
286285
return asString();
287286
default:
288-
return "!!! ERROR , UNSUPPORTED TYPE [" + type + "]";
287+
return "Unexpected unsupported type, contact developer [" + type + "]";
289288
}
290289
}
291290

292-
@Override
291+
public boolean toBoolean() {
292+
switch (this.type) {
293+
case BOOLEAN:
294+
return this.asBoolean();
295+
case INT:
296+
return this.asLong() != 0L;
297+
case FLOAT:
298+
return Math.round(this.asFloat()) != 0;
299+
case STRING:
300+
return "true".equalsIgnoreCase(this.asString().trim());
301+
default:
302+
return false;
303+
}
304+
}
293305

306+
@Override
294307
public ExpressionItemType getExpressionItemType() {
295308
return ExpressionItemType.VALUE;
296309
}
297310

298311
@Override
299-
300312
public ExpressionItemPriority getExpressionItemPriority() {
301313
return ExpressionItemPriority.VALUE;
302314
}

0 commit comments

Comments
 (0)