This is an automated email from the ASF dual-hosted git repository.
danhaywood pushed a commit to branch maintenance-branch
in repository https://gitbox.apache.org/repos/asf/causeway.git
The following commit(s) were added to refs/heads/maintenance-branch by this
push:
new f356368840 CAUSEWAY-3803: splits out config for big decimal grouping,
display vs editing
new 79de5b2d0c Merge branch 'CAUSEWAY-3803' into maintenance-branch
f356368840 is described below
commit f356368840cf5245316f35dad0ed169d28bb97dd
Author: Dan Haywood <[email protected]>
AuthorDate: Wed Jul 10 18:56:29 2024 +0100
CAUSEWAY-3803: splits out config for big decimal grouping, display vs
editing
---
.../core/config/CausewayConfiguration.java | 71 ++++++++++++++++++++++
.../valuesemantics/BigDecimalValueSemantics.java | 15 ++++-
2 files changed, 83 insertions(+), 3 deletions(-)
diff --git
a/core/config/src/main/java/org/apache/causeway/core/config/CausewayConfiguration.java
b/core/config/src/main/java/org/apache/causeway/core/config/CausewayConfiguration.java
index 6ade9786e6..8375028efb 100644
---
a/core/config/src/main/java/org/apache/causeway/core/config/CausewayConfiguration.java
+++
b/core/config/src/main/java/org/apache/causeway/core/config/CausewayConfiguration.java
@@ -3554,9 +3554,72 @@ public class CausewayConfiguration {
* <p>
* Is only used if the minimum scale has not been specified
explicitly by some other means, typically
* either {@link Digits#fraction()} or an ORM semantic such as the
(JPA) {@link Column#scale()}.
+ *
+ * @deprecated - use {@link Display#getMinScale()} instead
*/
+ @Deprecated(since = "2.2.0")
private Integer minScale = null;
+ @DeprecatedConfigurationProperty(replacement =
"causeway.value-types.big-decimal.display.min-scale", reason = "Moved")
+ public Integer getMinScale() {
+ return minScale;
+ }
+
+ private final Editing editing = new Editing();
+ @Data
+ public static class Editing {
+ /**
+ * A common use of {@link java.math.BigDecimal} is as a money
value. In some locales (eg English), the
+ * "," (comma) is the grouping (thousands) separator
wihle the "." (period) acts as a
+ * decimal point, but in others (eg France, Italy) it is the
other way around.
+ *
+ * <p>
+ * Surprisingly perhaps, a string such as "123,99", when
parsed ((by {@link java.text.DecimalFormat})
+ * in an English locale, is not rejected but instead is
evaluated as the value 12399L. That's almost
+ * certainly not what the end-user would have expected,
and results in a money value 100x too large.
+ * </p>
+ *
+ * <p>
+ * The purpose of this configuration property is to remove
the confusion by simply disallowing the
+ * thousands separator from being part of the input string.
+ * </p>
+ *
+ * <p>
+ * For maximum safety, allowing the grouping separator is
disallowed, but the alternate (original)
+ * behaviour can be reinstated by setting this config
property back to <code>true</code>.
+ * </p>
+ *
+ * <p>
+ * The same configuration property is also used for
rendering the value.
+ * </p>
+ *
+ * @see Display#isUseGroupingSeparator()
+ */
+ private boolean useGroupingSeparator = false;
+ }
+
+ private final Display display = new Display();
+ @Data
+ public static class Display {
+
+ /**
+ * The minimum scale to use for all {@link
java.math.BigDecimal}s.
+ *
+ * <p>
+ * Is only used if the minimum scale has not been specified
explicitly by some other means, typically
+ * either {@link Digits#fraction()} or an ORM semantic such as
the (JPA) {@link Column#scale()}.
+ */
+ private Integer minScale = null;
+
+ /**
+ * Whether to use a grouping (thousands) separator (eg the
"," (comma) in the English locale)
+ * when rendering a big decimal.
+ *
+ * @see Editing#isUseGroupingSeparator()
+ */
+ private boolean useGroupingSeparator = true;
+ }
+
/**
* A common use of {@link java.math.BigDecimal} is as a money
value. In some locales (eg English), the
* "," (comma) is the grouping (thousands) separator
wihle the "." (period) acts as a
@@ -3581,8 +3644,16 @@ public class CausewayConfiguration {
* <p>
* The same configuration property is also used for rendering
the value.
* </p>
+ *
+ * @deprecated - use {@link Editing#isUseGroupingSeparator()}
instead.
*/
+ @Deprecated(since = "2.2.0")
private boolean useGroupingSeparator = false;
+
+ @DeprecatedConfigurationProperty(replacement =
"causeway.value-types.big-decimal.editing.use-grouping-separator")
+ public boolean isUseGroupingSeparator() {
+ return useGroupingSeparator;
+ }
}
private final Kroki kroki = new Kroki();
diff --git
a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/valuesemantics/BigDecimalValueSemantics.java
b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/valuesemantics/BigDecimalValueSemantics.java
index 0df8eade1e..47c7d137cf 100644
---
a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/valuesemantics/BigDecimalValueSemantics.java
+++
b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/valuesemantics/BigDecimalValueSemantics.java
@@ -129,13 +129,17 @@ implements
@Override
public BigDecimal parseTextRepresentation(final
ValueSemanticsProvider.Context context, final String text) {
- val parsePolicy =
causewayConfiguration.getValueTypes().getBigDecimal().isUseGroupingSeparator()
+ val parsePolicy =
isUseGroupingSeparatorFrom(causewayConfiguration.getValueTypes().getBigDecimal())
? GroupingSeparatorPolicy.ALLOW
: GroupingSeparatorPolicy.DISALLOW;
return super.parseDecimal(context, text, parsePolicy)
.orElse(null);
}
+ private boolean
isUseGroupingSeparatorFrom(CausewayConfiguration.ValueTypes.BigDecimal
bigDecimalConfig) {
+ return bigDecimalConfig.getEditing().isUseGroupingSeparator() ||
bigDecimalConfig.isUseGroupingSeparator();
+ }
+
@Override
public int typicalLength() {
return 10;
@@ -145,7 +149,7 @@ implements
protected void configureDecimalFormat(
final Context context, final DecimalFormat format, final
FormatUsageFor usedFor) {
-
format.setGroupingUsed(causewayConfiguration.getValueTypes().getBigDecimal().isUseGroupingSeparator());
+
format.setGroupingUsed(causewayConfiguration.getValueTypes().getBigDecimal().getDisplay().isUseGroupingSeparator());
if(context==null) {
return;
@@ -172,12 +176,17 @@ implements
format.setMinimumFractionDigits(optionalInt.getAsInt());
} else {
// otherwise, apply a minScale if configured.
-
Optional.ofNullable(causewayConfiguration.getValueTypes().getBigDecimal().getMinScale())
+
minScaleFrom(causewayConfiguration.getValueTypes().getBigDecimal())
.ifPresent(format::setMinimumFractionDigits);
}
}
}
+ private static Optional<Integer> minScaleFrom(final
CausewayConfiguration.ValueTypes.BigDecimal bigDecimalConfig) {
+ return Optional.ofNullable(bigDecimalConfig.getDisplay().getMinScale())
+ .or(() ->
Optional.ofNullable(bigDecimalConfig.getMinScale()));
+ }
+
@Override
public Can<BigDecimal> getExamples() {
return Can.of(