This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new a4c27a0be0 DataFormatter: make getFormat and createFormat protected 
(#1427)
a4c27a0be0 is described below

commit a4c27a0be027dcbc60d3ad7c1c3a25d36eddbc34
Author: PJ Fanning <[email protected]>
AuthorDate: Sat Oct 17 12:52:16 2026 +0100

    DataFormatter: make getFormat and createFormat protected (#1427)
    
    * DataFormatter: make getFormat and createFormat protected
    
    Lets subclasses change the Format used for some format strings, e.g. to
    handle formats that DataFormatter does not support, while leaving the
    rest to DataFormatter.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    * Use Locale.ROOT symbols in the DataFormatter subclass tests
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    ---------
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../org/apache/poi/ss/usermodel/DataFormatter.java | 34 ++++++++++++++++++++--
 .../apache/poi/ss/usermodel/TestDataFormatter.java | 33 +++++++++++++++++++++
 2 files changed, 65 insertions(+), 2 deletions(-)

diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java 
b/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
index 61a3b13335..983ec2a955 100644
--- a/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
+++ b/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
@@ -395,7 +395,24 @@ public class DataFormatter {
         return false;
     }
 
-    private Format getFormat(double cellValue, int formatIndex, String 
formatStrIn, boolean use1904Windowing) {
+    /**
+     * Returns the Format to use for a value with the given format.
+     * <p>
+     * Formats with several sections or conditions are evaluated with {@link 
CellFormat} for this value
+     * and are not cached. Other formats are looked up in the cache of formats 
(see {@link #addFormat}),
+     * and otherwise created with {@link #createFormat(double, int, String)} 
and cached.
+     * <p>
+     * Subclasses can override this to change the Format used for some format 
strings.
+     * For date formats, the returned Format is given a {@link java.util.Date} 
to format.
+     *
+     * @param cellValue the value to format
+     * @param formatIndex the index of the format
+     * @param formatStrIn the format string
+     * @param use1904Windowing whether the 1904 date system is used
+     * @return the Format to use, or null if the value should be shown with 
{@link String#valueOf(double)}
+     * @since 6.0.0
+     */
+    protected Format getFormat(double cellValue, int formatIndex, String 
formatStrIn, boolean use1904Windowing) {
         if (formatStrIn == null) {
             throw new IllegalArgumentException("Missing input format for value 
" + cellValue + " and index " + formatIndex);
         }
@@ -480,7 +497,20 @@ public class DataFormatter {
         return createFormat(cell.getNumericCellValue(), formatIndex, 
formatStr);
     }
 
-    private Format createFormat(double cellValue, int formatIndex, String 
sFormat) {
+    /**
+     * Creates a Format for the given format string. The result is not cached 
by this method,
+     * {@link #getFormat(double, int, String, boolean)} caches it.
+     * <p>
+     * Subclasses can override this to change how some format strings are 
handled.
+     *
+     * @param cellValue the value to format
+     * @param formatIndex the index of the format
+     * @param sFormat the format string
+     * @return the Format to use, a default Format if the pattern cannot be 
parsed,
+     *  or null if the value should be shown with {@link 
String#valueOf(double)}
+     * @since 6.0.0
+     */
+    protected Format createFormat(double cellValue, int formatIndex, String 
sFormat) {
         checkForLocaleChange();
 
         String formatStr = convertExclamationEscapes(sFormat);
diff --git 
a/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java 
b/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java
index a3f5792753..462d48f849 100644
--- a/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java
+++ b/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java
@@ -25,6 +25,9 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.IOException;
 import java.text.DateFormat;
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.text.Format;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
@@ -1535,4 +1538,34 @@ class TestDataFormatter {
         assertThrows(IllegalArgumentException.class, () -> 
formatter.setBuiltinFormatOverride(164, german40));
         assertThrows(IllegalArgumentException.class, () -> 
formatter.setBuiltinFormatOverride(-1, german40));
     }
+
+    @Test
+    void testSubclassCanOverrideCreateFormat() {
+        DataFormatter formatter = new DataFormatter(Locale.US) {
+            @Override
+            protected Format createFormat(double cellValue, int formatIndex, 
String sFormat) {
+                if ("[custom]".equals(sFormat)) {
+                    return new DecimalFormat("'custom '0.0", 
DecimalFormatSymbols.getInstance(Locale.ROOT));
+                }
+                return super.createFormat(cellValue, formatIndex, sFormat);
+            }
+        };
+        assertEquals("custom 1.5", formatter.formatRawCellContents(1.5, 200, 
"[custom]"));
+        assertEquals("1.50", formatter.formatRawCellContents(1.5, 201, 
"0.00"));
+    }
+
+    @Test
+    void testSubclassCanOverrideGetFormat() {
+        DataFormatter formatter = new DataFormatter(Locale.US) {
+            @Override
+            protected Format getFormat(double cellValue, int formatIndex, 
String formatStrIn, boolean use1904Windowing) {
+                if ("[units]0".equals(formatStrIn)) {
+                    return new DecimalFormat("0' units'", 
DecimalFormatSymbols.getInstance(Locale.ROOT));
+                }
+                return super.getFormat(cellValue, formatIndex, formatStrIn, 
use1904Windowing);
+            }
+        };
+        assertEquals("2 units", formatter.formatRawCellContents(1.5, 200, 
"[units]0"));
+        assertEquals("1.50", formatter.formatRawCellContents(1.5, 201, 
"0.00"));
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to