-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 #8582
- Loading branch information
Showing
6 changed files
with
132 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
56 changes: 56 additions & 0 deletions
56
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,56 @@ | ||
package io.quarkus.qute; | ||
|
||
import io.quarkus.qute.Results.Result; | ||
import java.lang.reflect.Array; | ||
import java.math.BigDecimal; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
public final class Booleans { | ||
|
||
private static final Long LONG_ZERO = 0L; | ||
private static final Integer INTEGER_ZERO = 0; | ||
private static final Short SHORT_ZERO = 0; | ||
private static final Byte BYTE_ZERO = 0; | ||
private static final Double DOUBLE_ZERO = 0.0; | ||
private static final Float FLOAT_ZERO = 0.0F; | ||
|
||
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; | ||
} | ||
return INTEGER_ZERO.equals(number) || LONG_ZERO.equals(number) | ||
|| SHORT_ZERO.equals(number) || BYTE_ZERO.equals(number) | ||
|| DOUBLE_ZERO.equals(number) || FLOAT_ZERO.equals(number); | ||
} | ||
|
||
} |
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
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