forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Qute if section - consider truthy/falsy values during evaluation
- resolves quarkusio#8582
- Loading branch information
Showing
7 changed files
with
176 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
independent-projects/qute/core/src/main/java/io/quarkus/qute/Booleans.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.quarkus.qute; | ||
|
||
import io.quarkus.qute.Results.Result; | ||
import java.lang.reflect.Array; | ||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
public final class Booleans { | ||
|
||
private Booleans() { | ||
} | ||
|
||
/** | ||
* A value is considered falsy if it's null, {@link Result#NOT_FOUND}, {code false}, an empty collection, an empty map, an | ||
* empty array, an empty string/char sequence or a number equal to zero. | ||
* | ||
* @param value | ||
* @return {@code true} if the value is falsy | ||
*/ | ||
public static boolean isFalsy(Object value) { | ||
if (value == null || Results.Result.NOT_FOUND.equals(value)) { | ||
return true; | ||
} else if (value instanceof Boolean) { | ||
return !(Boolean) value; | ||
} else if (value instanceof Collection) { | ||
return ((Collection<?>) value).isEmpty(); | ||
} else if (value instanceof Map) { | ||
return ((Map<?, ?>) value).isEmpty(); | ||
} else if (value.getClass().isArray()) { | ||
return Array.getLength(value) == 0; | ||
} else if (value instanceof CharSequence) { | ||
return ((CharSequence) value).length() == 0; | ||
} else if (value instanceof Number) { | ||
return isZero((Number) value); | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean isZero(Number number) { | ||
if (number instanceof BigDecimal) { | ||
return BigDecimal.ZERO.compareTo((BigDecimal) number) == 0; | ||
} else if (number instanceof BigInteger) { | ||
return BigInteger.ZERO.equals(number); | ||
} | ||
if (number instanceof Float || number instanceof Double) { | ||
return number.doubleValue() == 0.0; | ||
} | ||
// byte, short, integer, long and any other number | ||
return number.longValue() == 0L; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
independent-projects/qute/core/src/test/java/io/quarkus/qute/BooleansTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.quarkus.qute; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class BooleansTest { | ||
|
||
@Test | ||
public void testIsFalsy() { | ||
assertTrue(Booleans.isFalsy(null)); | ||
assertTrue(Booleans.isFalsy(false)); | ||
assertTrue(Booleans.isFalsy("")); | ||
assertTrue(Booleans.isFalsy(new StringBuffer())); | ||
assertTrue(Booleans.isFalsy(0f)); | ||
assertTrue(Booleans.isFalsy(0)); | ||
assertTrue(Booleans.isFalsy(0.0)); | ||
assertTrue(Booleans.isFalsy(new BigDecimal("0.0"))); | ||
assertTrue(Booleans.isFalsy(new BigInteger("0"))); | ||
assertTrue(Booleans.isFalsy(new ArrayList<>())); | ||
assertTrue(Booleans.isFalsy(new HashSet<>())); | ||
assertTrue(Booleans.isFalsy(new HashMap<>())); | ||
assertTrue(Booleans.isFalsy(new String[0])); | ||
// truthy values | ||
assertFalse(Booleans.isFalsy(new Object())); | ||
assertFalse(Booleans.isFalsy(true)); | ||
assertFalse(Booleans.isFalsy("foo")); | ||
assertFalse(Booleans.isFalsy(new StringBuffer().append("foo"))); | ||
assertFalse(Booleans.isFalsy(1f)); | ||
assertFalse(Booleans.isFalsy(4)); | ||
assertFalse(Booleans.isFalsy(0.2)); | ||
assertFalse(Booleans.isFalsy(new BigDecimal("0.1"))); | ||
assertFalse(Booleans.isFalsy(new BigInteger("10"))); | ||
assertFalse(Booleans.isFalsy(Collections.singletonList("foo"))); | ||
assertFalse(Booleans.isFalsy(Collections.singletonMap("foo", "bar"))); | ||
assertFalse(Booleans.isFalsy(new String[] { "foo" })); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters