This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git
The following commit(s) were added to refs/heads/master by this push:
new bd1eeece Add DoubleFormat.Builder.get() as Builder now implements
Supplier
bd1eeece is described below
commit bd1eeece7b86c08584c04d0323630c6ec0eb84d9
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Apr 7 09:29:20 2024 -0400
Add DoubleFormat.Builder.get() as Builder now implements Supplier
---
src/changes/changes.xml | 1 +
.../apache/commons/text/numbers/DoubleFormat.java | 17 +++++++-
.../commons/text/jmh/DoubleFormatPerformance.java | 20 ++++------
.../commons/text/numbers/DoubleFormatTest.java | 45 +++++++++++++---------
4 files changed, 50 insertions(+), 33 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index e284df29..c72dca73 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -51,6 +51,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add StringLookupFactory.propertiesStringLookup(Path...) and deprecated
propertiesStringLookup().</action>
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add StringLookupFactory.xmlStringLookup(Map, Path...) and deprecated
xmlStringLookup() and xmlStringLookup(Map).</action>
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add StringLookupFactory.builder() for fencing Path resolution of the
file, properties and XML lookups.</action>
+ <action type="add" dev="ggregory" due-to="Gary
Gregory">Add DoubleFormat.Builder.get() as Builder now implements
Supplier.</action>
<!-- FIX -->
<action issue="TEXT-232" type="fix" dev="ggregory" due-to="Arnout Engelen,
Gary Gregory">WordUtils.containsAllWords​() may throw
PatternSyntaxException.</action>
<action issue="TEXT-175" type="fix" dev="ggregory" due-to="David Lavati,
seanfabs, Gary Gregory, Bruno P. Kinoshita">Fix regression for determining
whitespace in WordUtils #519.</action>
diff --git a/src/main/java/org/apache/commons/text/numbers/DoubleFormat.java
b/src/main/java/org/apache/commons/text/numbers/DoubleFormat.java
index fb74d61e..4b9916ce 100644
--- a/src/main/java/org/apache/commons/text/numbers/DoubleFormat.java
+++ b/src/main/java/org/apache/commons/text/numbers/DoubleFormat.java
@@ -20,6 +20,7 @@ import java.text.DecimalFormatSymbols;
import java.util.Objects;
import java.util.function.DoubleFunction;
import java.util.function.Function;
+import java.util.function.Supplier;
/**
* Enum containing standard double format types with methods to produce
@@ -330,7 +331,7 @@ public enum DoubleFormat {
/**
* Builds configured format functions for standard double format types.
*/
- public static final class Builder {
+ public static final class Builder implements
Supplier<DoubleFunction<String>> {
/** Default value for the plain format max decimal exponent. */
private static final int DEFAULT_PLAIN_FORMAT_MAX_DECIMAL_EXPONENT = 6;
@@ -447,9 +448,11 @@ public enum DoubleFormat {
* Builds a new double format function.
*
* @return format function
+ * @deprecated Use {@link #get()}.
*/
+ @Deprecated
public DoubleFunction<String> build() {
- return factory.apply(this);
+ return get();
}
/**
@@ -520,6 +523,16 @@ public enum DoubleFormat {
.minusSign(symbols.getMinusSign()).exponentSeparator(symbols.getExponentSeparator()).infinity(symbols.getInfinity()).nan(symbols.getNaN());
}
+ /**
+ * Builds a new double format function.
+ *
+ * @return format function
+ */
+ @Override
+ public DoubleFunction<String> get() {
+ return factory.apply(this);
+ }
+
/**
* Sets the character used to separate groups of thousands. Default
value is {@code ','}.
*
diff --git
a/src/test/java/org/apache/commons/text/jmh/DoubleFormatPerformance.java
b/src/test/java/org/apache/commons/text/jmh/DoubleFormatPerformance.java
index f6d91279..738ae4c6 100644
--- a/src/test/java/org/apache/commons/text/jmh/DoubleFormatPerformance.java
+++ b/src/test/java/org/apache/commons/text/jmh/DoubleFormatPerformance.java
@@ -207,11 +207,10 @@ public class DoubleFormatPerformance {
*/
@Benchmark
public void doubleFormatEngineering(final DoubleInput input, final
Blackhole bh) {
- final DoubleFunction<String> fmt = DoubleFormat.ENGINEERING.builder()
+ runDoubleFunction(input, bh, DoubleFormat.ENGINEERING.builder()
.maxPrecision(6)
.alwaysIncludeExponent(true)
- .build();
- runDoubleFunction(input, bh, fmt);
+ .get());
}
/** Benchmark testing the {@link DoubleFormat#PLAIN} format.
@@ -220,10 +219,9 @@ public class DoubleFormatPerformance {
*/
@Benchmark
public void doubleFormatPlain(final DoubleInput input, final Blackhole bh)
{
- final DoubleFunction<String> fmt = DoubleFormat.PLAIN.builder()
+ runDoubleFunction(input, bh, DoubleFormat.PLAIN.builder()
.minDecimalExponent(-3)
- .build();
- runDoubleFunction(input, bh, fmt);
+ .get());
}
/** Benchmark testing the {@link DoubleFormat#PLAIN} format with
@@ -233,11 +231,10 @@ public class DoubleFormatPerformance {
*/
@Benchmark
public void doubleFormatPlainGrouped(final DoubleInput input, final
Blackhole bh) {
- final DoubleFunction<String> fmt = DoubleFormat.PLAIN.builder()
+ runDoubleFunction(input, bh, DoubleFormat.PLAIN.builder()
.minDecimalExponent(-3)
.groupThousands(true)
- .build();
- runDoubleFunction(input, bh, fmt);
+ .get());
}
/** Benchmark testing the {@link DoubleFormat#SCIENTIFIC} format.
@@ -246,11 +243,10 @@ public class DoubleFormatPerformance {
*/
@Benchmark
public void doubleFormatScientific(final DoubleInput input, final
Blackhole bh) {
- final DoubleFunction<String> fmt = DoubleFormat.SCIENTIFIC.builder()
+ runDoubleFunction(input, bh, DoubleFormat.SCIENTIFIC.builder()
.maxPrecision(4)
.alwaysIncludeExponent(true)
- .build();
- runDoubleFunction(input, bh, fmt);
+ .get());
}
/** Benchmark testing the {@link Double#toString()} method.
diff --git
a/src/test/java/org/apache/commons/text/numbers/DoubleFormatTest.java
b/src/test/java/org/apache/commons/text/numbers/DoubleFormatTest.java
index 9f7161aa..59026f4e 100644
--- a/src/test/java/org/apache/commons/text/numbers/DoubleFormatTest.java
+++ b/src/test/java/org/apache/commons/text/numbers/DoubleFormatTest.java
@@ -97,7 +97,7 @@ public class DoubleFormatTest {
* @param type format type
*/
private static void checkFormatAccuracyWithDefaults(final DoubleFormat
type) {
- final DoubleFunction<String> fmt = type.builder().build();
+ final DoubleFunction<String> fmt = type.builder().get();
checkDefaultFormatSpecial(fmt);
@@ -191,8 +191,8 @@ public class DoubleFormatTest {
// Example of different Double.toString representations across JDKs
// JDK 17: -9.3540047119774374E17
// JDK 21: -9.354004711977437E17
- Arguments.of(DoubleFormat.PLAIN.builder().build(),
-9.3540047119774374E17),
- Arguments.of(DoubleFormat.SCIENTIFIC.builder().build(),
-9.3540047119774374E17)
+ Arguments.of(DoubleFormat.PLAIN.builder().get(),
-9.3540047119774374E17),
+ Arguments.of(DoubleFormat.SCIENTIFIC.builder().get(),
-9.3540047119774374E17)
);
}
@@ -231,10 +231,10 @@ public class DoubleFormatTest {
void testCustomDigitString() {
// arrange
final String digits = "abcdefghij";
- final DoubleFunction<String> plain =
DoubleFormat.PLAIN.builder().digits(digits).build();
- final DoubleFunction<String> sci =
DoubleFormat.SCIENTIFIC.builder().digits(digits).build();
- final DoubleFunction<String> eng =
DoubleFormat.ENGINEERING.builder().digits(digits).build();
- final DoubleFunction<String> mixed =
DoubleFormat.MIXED.builder().digits(digits).build();
+ final DoubleFunction<String> plain =
DoubleFormat.PLAIN.builder().digits(digits).get();
+ final DoubleFunction<String> sci =
DoubleFormat.SCIENTIFIC.builder().digits(digits).get();
+ final DoubleFunction<String> eng =
DoubleFormat.ENGINEERING.builder().digits(digits).get();
+ final DoubleFunction<String> mixed =
DoubleFormat.MIXED.builder().digits(digits).get();
// act/assert
checkFormat(plain, 9876543210.0, "jihgfedcba.a");
@@ -256,7 +256,7 @@ public class DoubleFormatTest {
.infinity("inf")
.nan("nan")
.minusSign('!')
- .build();
+ .get();
// act/assert
checkFormat(fmt, Double.NaN, "nan");
@@ -297,7 +297,7 @@ public class DoubleFormatTest {
void testEngineering_defaults() {
// act
final DoubleFunction<String> fmt = DoubleFormat.ENGINEERING.builder()
- .build();
+ .get();
// act/assert
checkDefaultFormatSpecial(fmt);
@@ -339,7 +339,7 @@ public class DoubleFormatTest {
.maxPrecision(6)
.alwaysIncludeExponent(true)
.formatSymbols(DecimalFormatSymbols.getInstance(loc))
- .build());
+ .get());
}
@Test
@@ -381,7 +381,7 @@ public class DoubleFormatTest {
.infinity("inf")
.nan("nan")
.minusSign('!')
- .build();
+ .get();
// act/assert
checkFormat(fmt, Double.NaN, "nan");
@@ -421,8 +421,10 @@ public class DoubleFormatTest {
@Test
void testMixed_defaults() {
// arrange
- final DoubleFunction<String> fmt =
DoubleFormat.MIXED.builder().build();
+ testMixed_defaults(DoubleFormat.MIXED.builder().get());
+ }
+ private void testMixed_defaults(final DoubleFunction<String> fmt) {
// act/assert
checkDefaultFormatSpecial(fmt);
@@ -456,6 +458,11 @@ public class DoubleFormatTest {
checkFormat(fmt, Math.E, "2.718281828459045");
}
+ @Test
+ void testMixed_defaultsDeprecated() {
+ testMixed_defaults(DoubleFormat.MIXED.builder().build());
+ }
+
@Test
void testPlain_custom() {
// arrange
@@ -469,7 +476,7 @@ public class DoubleFormatTest {
.infinity("inf")
.nan("nan")
.minusSign('!')
- .build();
+ .get();
// act/assert
checkFormat(fmt, Double.NaN, "nan");
@@ -510,7 +517,7 @@ public class DoubleFormatTest {
void testPlain_defaults() {
// arrange
final DoubleFunction<String> fmt = DoubleFormat.PLAIN.builder()
- .build();
+ .get();
// act/assert
checkFormat(fmt, 0.00001, "0.00001");
@@ -549,12 +556,12 @@ public class DoubleFormatTest {
checkLocalizedFormats("0.0##", loc -> DoubleFormat.PLAIN.builder()
.minDecimalExponent(-3)
.formatSymbols(DecimalFormatSymbols.getInstance(loc))
- .build());
+ .get());
checkLocalizedFormats("#,##0.0##", loc -> DoubleFormat.PLAIN.builder()
.minDecimalExponent(-3)
.groupThousands(true)
.formatSymbols(DecimalFormatSymbols.getInstance(loc))
- .build());
+ .get());
}
@Test
@@ -570,7 +577,7 @@ public class DoubleFormatTest {
.infinity("inf")
.nan("nan")
.minusSign('!')
- .build();
+ .get();
// act/assert
checkFormat(fmt, Double.NaN, "nan");
@@ -610,7 +617,7 @@ public class DoubleFormatTest {
@Test
void testScientific_defaults() {
// arrange
- final DoubleFunction<String> fmt =
DoubleFormat.SCIENTIFIC.builder().build();
+ final DoubleFunction<String> fmt =
DoubleFormat.SCIENTIFIC.builder().get();
// act/assert
checkDefaultFormatSpecial(fmt);
@@ -652,6 +659,6 @@ public class DoubleFormatTest {
.maxPrecision(4)
.alwaysIncludeExponent(true)
.formatSymbols(DecimalFormatSymbols.getInstance(loc))
- .build());
+ .get());
}
}