diff --git a/src/main/java/sirius/kernel/commons/AdvancedDateParser.java b/src/main/java/sirius/kernel/commons/AdvancedDateParser.java
index da2b76d7..9cb9ddab 100644
--- a/src/main/java/sirius/kernel/commons/AdvancedDateParser.java
+++ b/src/main/java/sirius/kernel/commons/AdvancedDateParser.java
@@ -27,7 +27,7 @@
* A flexible parser for dates in various formats.
*
* It can parse formats like DD.MM.YYYY, DD-MM-YYYY, MM/DD/YYYY or ISO dates like YYYY-MM-DDTHH:MM:SS along with some
- * modifiers as decribed below.
+ * modifiers as described below.
*
* A valid expression is defined by the following grammar:
*
diff --git a/src/main/java/sirius/kernel/commons/Value.java b/src/main/java/sirius/kernel/commons/Value.java
index 15bbaed0..6b0f0c65 100644
--- a/src/main/java/sirius/kernel/commons/Value.java
+++ b/src/main/java/sirius/kernel/commons/Value.java
@@ -528,12 +528,13 @@ public Value first() {
public T coerce(Class targetClazz, T defaultValue) {
if (Boolean.class.equals(targetClazz) || boolean.class.equals(targetClazz)) {
if (isEmptyString()) {
- return (T) Boolean.FALSE;
+ return defaultValue != null ? defaultValue : (T) Boolean.FALSE;
}
if (data instanceof Boolean) {
return (T) data;
}
- return (T) NLS.parseMachineString(Boolean.class, String.valueOf(data));
+ String stringValue = String.valueOf(data).trim();
+ return (T) parseWithoutNLS(stringValue).orElseGet(() -> NLS.parseMachineString(Boolean.class, stringValue));
}
if (data == null) {
return defaultValue;
@@ -802,7 +803,7 @@ public String asSmartRoundedString() {
*
* @param defaultValue the value to be used if the wrapped value cannot be converted to a boolean.
* @return true if the wrapped value is true
- * or if the string representation of it is {@code "true"}. Returns false otherwise,
+ * or if the string representation of it is {@code "true"} or {@code "1"}. Returns false otherwise,
* especially if the wrapped value is null
*/
public boolean asBoolean(boolean defaultValue) {
@@ -813,16 +814,26 @@ public boolean asBoolean(boolean defaultValue) {
return booleanValue;
}
- // fast-track for common cases without the need to involve NLS framework
- if ("true".equalsIgnoreCase(String.valueOf(data))) {
- return true;
+ String stringValue = String.valueOf(data).trim();
+ return parseWithoutNLS(stringValue).orElseGet(() -> {
+ return Objects.requireNonNullElse(NLS.parseUserString(Boolean.class, stringValue), defaultValue);
+ });
+ }
+
+ /**
+ * Fast-track for common boolean cases without the need to involve NLS framework
+ *
+ * @param value the value to parse
+ * @return an optional boolean value
+ */
+ private Optional parseWithoutNLS(String value) {
+ if ("true".equalsIgnoreCase(value) || "1".equals(value)) {
+ return Optional.of(true);
}
- if ("false".equalsIgnoreCase(String.valueOf(data))) {
- return false;
+ if ("false".equalsIgnoreCase(value) || "0".equals(value)) {
+ return Optional.of(false);
}
-
- return Objects.requireNonNullElse(NLS.parseUserString(Boolean.class, String.valueOf(data).trim()),
- defaultValue);
+ return Optional.empty();
}
/**
diff --git a/src/test/kotlin/sirius/kernel/commons/ValueTest.kt b/src/test/kotlin/sirius/kernel/commons/ValueTest.kt
index b7d2731f..448b6085 100644
--- a/src/test/kotlin/sirius/kernel/commons/ValueTest.kt
+++ b/src/test/kotlin/sirius/kernel/commons/ValueTest.kt
@@ -114,12 +114,16 @@ class ValueTest {
@Test
fun `Test asBoolean with default`() {
assertEquals(false, Value.of("").asBoolean(false))
- assertEquals(false, Value.of("false").asBoolean(false))
- assertEquals(true, Value.of("true").asBoolean(false))
assertEquals(false, Value.of(false).asBoolean(false))
+ assertEquals(false, Value.of("false").asBoolean(false))
+ assertEquals(false, Value.of(0).asBoolean(false))
+ assertEquals(false, Value.of("0").asBoolean(false))
+ assertEquals(false, Value.of(NLS.get("NLS.no")).asBoolean(false))
assertEquals(true, Value.of(true).asBoolean(false))
+ assertEquals(true, Value.of("true").asBoolean(false))
+ assertEquals(true, Value.of(1).asBoolean(false))
+ assertEquals(true, Value.of("1").asBoolean(false))
assertEquals(true, Value.of(NLS.get("NLS.yes")).asBoolean(false))
- assertEquals(false, Value.of(NLS.get("NLS.no")).asBoolean(false))
}
@Test
@@ -131,14 +135,38 @@ class ValueTest {
}
@Test
- fun `Test coerce boolean and without default`() {
+ fun `Test coerce boolean without default`() {
assertEquals(false, Value.of("").coerce(Boolean::class.java, null))
assertEquals(false, Value.of("false").coerce(Boolean::class.java, null))
assertEquals(true, Value.of("true").coerce(Boolean::class.java, null))
+ assertEquals(false, Value.of("0").coerce(Boolean::class.java, null))
+ assertEquals(true, Value.of("1").coerce(Boolean::class.java, null))
assertEquals(false, Value.of(false).coerce(Boolean::class.java, null))
assertEquals(true, Value.of(true).coerce(Boolean::class.java, null))
}
+ @Test
+ fun `Test coerce boolean with default true`() {
+ assertEquals(true, Value.of("").coerce(Boolean::class.java, true))
+ assertEquals(false, Value.of("false").coerce(Boolean::class.java, true))
+ assertEquals(true, Value.of("true").coerce(Boolean::class.java, true))
+ assertEquals(false, Value.of("0").coerce(Boolean::class.java, true))
+ assertEquals(true, Value.of("1").coerce(Boolean::class.java, true))
+ assertEquals(false, Value.of(false).coerce(Boolean::class.java, true))
+ assertEquals(true, Value.of(true).coerce(Boolean::class.java, true))
+ }
+
+ @Test
+ fun `Test coerce boolean with default false`() {
+ assertEquals(false, Value.of("").coerce(Boolean::class.java, false))
+ assertEquals(false, Value.of("false").coerce(Boolean::class.java, false))
+ assertEquals(true, Value.of("true").coerce(Boolean::class.java, false))
+ assertEquals(false, Value.of("0").coerce(Boolean::class.java, false))
+ assertEquals(true, Value.of("1").coerce(Boolean::class.java, false))
+ assertEquals(false, Value.of(false).coerce(Boolean::class.java, false))
+ assertEquals(true, Value.of(true).coerce(Boolean::class.java, false))
+ }
+
@Test
fun `Boxing and retrieving an amount works`() {
assertEquals(Amount.of(0.00001), Value.of(Amount.of(0.00001)).getAmount())