Skip to content

Commit

Permalink
Merge tag 'dev-42.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
sabieber committed Apr 29, 2024
2 parents 081e457 + 559d1d6 commit 25aca46
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 63 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/enforce-pr-label.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Enforce PR Labels
on:
pull_request:
types: [opened, labeled, unlabeled, synchronize]
jobs:
label:
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:
- uses: mheap/github-action-required-labels@v5
with:
mode: minimum
count: 1
labels: "💣 BREAKING CHANGE, 🧬 Enhancement, 🐛 Bugfix, 🛠️ Maintenance, ⬆️ Dependencies"
add_comment: true
message: "This PR can only be merged after at least one of our categorizing labels has been added: {{ provided }}"
- uses: mheap/github-action-required-labels@v5
with:
mode: exactly
count: 0
labels: "🎁 Next version, 🖐 Keep open, 🕔 Wait for sirius"
add_comment: true
message: "This PR can only be merged after all blocking labels have been removed: {{ provided }}"
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* A flexible parser for dates in various formats.
* <p>
* 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.
* <p>
* A valid expression is defined by the following grammar:
* <ul>
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/sirius/kernel/commons/Amount.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;

Expand All @@ -30,7 +31,7 @@
* <p>
* Adds some extended computations as well as locale aware formatting options to perform "exact" computations on
* numeric value. The internal representation is <tt>BigDecimal</tt> and uses MathContext.DECIMAL128 for
* numerical operations. Also the scale of each value is fixed to 5 decimal places after the comma, since this is
* numerical operations. Also, the scale of each value is fixed to 5 decimal places after the comma, since this is
* enough for most business applications and rounds away any rounding errors introduced by doubles.
* <p>
* A textual representation can be created by calling one of the <tt>toString</tt> methods or by supplying
Expand All @@ -39,7 +40,7 @@
* Note that {@link #toMachineString()} to be used to obtain a technical representation suitable for file formats
* like XML etc. This is also used by {@link NLS#toMachineString(Object)}. The default representation uses two
* decimal digits. However, if the amount has bed {@link #round(int, RoundingMode) rounded}, the given amount
* of decimals will be used in all subesquent call to {@link #toMachineString()}. Therefore, this can be used to
* of decimals will be used in all subsequent call to {@link #toMachineString()}. Therefore, this can be used to
* control the exact formatting (e.g. when writing XML or JSON).
* <p>
* Being able to be <i>empty</i>, this class handles <tt>null</tt> values gracefully, which simplifies many operations.
Expand Down Expand Up @@ -247,6 +248,22 @@ public BigDecimal getAmount() {
return value;
}

/**
* Unwraps the internally used <tt>BigDecimal</tt> like {@link #getAmount()}, but also strips trailing zeros from
* the decimal part.
*
* @return the amount with trailing zeros stripped of the decimal part
*/
@Nullable
public BigDecimal fetchAmountWithoutTrailingZeros() {
return Optional.ofNullable(value)
.map(BigDecimal::stripTrailingZeros)
.map(bigDecimal -> bigDecimal.scale() < 0 ?
bigDecimal.setScale(0, RoundingMode.UNNECESSARY) :
bigDecimal)
.orElse(null);
}

/**
* Unwraps the internally used <tt>BigDecimal</tt> with rounding like in {@link #toMachineString()} applied.
* This is used for Jackson Object Mapping.
Expand Down
33 changes: 22 additions & 11 deletions src/main/java/sirius/kernel/commons/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,13 @@ public Value first() {
public <T> T coerce(Class<T> 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;
Expand Down Expand Up @@ -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 <tt>true</tt> if the wrapped value is <tt>true</tt>
* or if the string representation of it is {@code "true"}. Returns <tt>false</tt> otherwise,
* or if the string representation of it is {@code "true"} or {@code "1"}. Returns <tt>false</tt> otherwise,
* especially if the wrapped value is <tt>null</tt>
*/
public boolean asBoolean(boolean defaultValue) {
Expand All @@ -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<Boolean> 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();
}

/**
Expand Down
Loading

0 comments on commit 25aca46

Please sign in to comment.