You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2019/03/27 23:04:40 UTC

svn commit: r1856449 - in /poi/trunk/src: java/org/apache/poi/ss/usermodel/DataFormatter.java testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java

Author: fanningpj
Date: Wed Mar 27 23:04:40 2019
New Revision: 1856449

URL: http://svn.apache.org/viewvc?rev=1856449&view=rev
Log:
[bug-63291] support concurrent date formatting with same DataFormatter

Modified:
    poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java
    poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java

Modified: poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java?rev=1856449&r1=1856448&r2=1856449&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java Wed Mar 27 23:04:40 2019
@@ -309,7 +309,7 @@ public class DataFormatter implements Ob
         return getFormat(cell.getNumericCellValue(), formatIndex, formatStr);
     }
 
-    private Format getFormat(double cellValue, int formatIndex, String formatStrIn) {
+    private synchronized Format getFormat(double cellValue, int formatIndex, String formatStrIn) {
         localeChangedObservable.checkForLocaleChange();
 
         // Might be better to separate out the n p and z formats, falling back to p when n and z are not set.
@@ -794,7 +794,10 @@ public class DataFormatter implements Ob
      *  supplied Date and format
      */
     private String performDateFormatting(Date d, Format dateFormat) {
-       return (dateFormat != null ? dateFormat : defaultDateformat).format(d);
+        Format df = dateFormat != null ? dateFormat : defaultDateformat;
+        synchronized (df) {
+            return df.format(d);
+        }
     }
 
     /**
@@ -815,14 +818,16 @@ public class DataFormatter implements Ob
             return null;
         }
         Format dateFormat = getFormat(cell, cfEvaluator);
-        if(dateFormat instanceof ExcelStyleDateFormatter) {
-           // Hint about the raw excel value
-           ((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted(
-                 cell.getNumericCellValue()
-           );
+        synchronized (dateFormat) {
+            if(dateFormat instanceof ExcelStyleDateFormatter) {
+                // Hint about the raw excel value
+                ((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted(
+                        cell.getNumericCellValue()
+                );
+            }
+            Date d = cell.getDateCellValue();
+            return performDateFormatting(d, dateFormat);
         }
-        Date d = cell.getDateCellValue();
-        return performDateFormatting(d, dateFormat);
     }
 
     /**

Modified: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java?rev=1856449&r1=1856448&r2=1856449&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java Wed Mar 27 23:04:40 2019
@@ -939,31 +939,28 @@ public class TestDataFormatter {
 
     @Test
     public void testConcurrentCellFormat() throws Exception {
-        int formatIndex = 105;
-        String formatString = "[$-F400]m/d/yy h:mm:ss\\ AM/PM;[$-F400]m/d/yy h:mm:ss\\ AM/PM;_-* \"\"??_-;_-@_-";
-
         DataFormatter formatter = new DataFormatter();
-        doFormatTestSequential(formatter, 43551.50990171296, "3/27/19 12:14:15 PM", formatIndex, formatString);
-        doFormatTestSequential(formatter, 36104.424780092595, "11/5/98 10:11:41 AM", formatIndex, formatString);
-
-        doFormatTestConcurrent(formatter, 43551.50990171296, "3/27/19 12:14:15 PM", formatIndex, formatString);
-        doFormatTestConcurrent(formatter, 36104.424780092595, "11/5/98 10:11:41 AM", formatIndex, formatString);
+        doFormatTestSequential(formatter);
+        doFormatTestConcurrent(formatter);
     }
 
-    private void doFormatTestSequential(DataFormatter formatter, double n, String expected, int formatIndex,
-                                        String formatString) {
+    private void doFormatTestSequential(DataFormatter formatter) {
         for (int i = 0; i < 1_000; i++) {
-            assertTrue(doFormatTest(formatter, n, expected, formatIndex, formatString, i));
+            assertTrue(doFormatTest(formatter, 43551.50990171296, "3/27/19 12:14:15 PM", i));
+            assertTrue(doFormatTest(formatter, 36104.424780092595, "11/5/98 10:11:41 AM", i));
         }
     }
 
-    private void doFormatTestConcurrent(DataFormatter formatter, double n, String expected, int formatIndex,
-                                        String formatString) throws Exception {
+    private void doFormatTestConcurrent(DataFormatter formatter) throws Exception {
         ArrayList<CompletableFuture<Boolean>> futures = new ArrayList<>();
         for (int i = 0; i < 1_000; i++) {
             final int iteration = i;
             CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(
-                    () -> { return doFormatTest(formatter, n, expected, formatIndex, formatString, iteration); });
+                    () -> {
+                        boolean r1 = doFormatTest(formatter, 43551.50990171296, "3/27/19 12:14:15 PM", iteration);
+                        boolean r2 = doFormatTest(formatter, 36104.424780092595, "11/5/98 10:11:41 AM", iteration);
+                        return r1 && r2;
+                    });
             futures.add(future);
         }
         for (CompletableFuture<Boolean> future : futures) {
@@ -971,8 +968,9 @@ public class TestDataFormatter {
         }
     }
 
-    private static boolean doFormatTest(DataFormatter formatter, double n, String expected, int formatIndex,
-                              String formatString, int iteration) {
+    private static boolean doFormatTest(DataFormatter formatter, double n, String expected, int iteration) {
+        int formatIndex = 105;
+        String formatString = "[$-F400]m/d/yy h:mm:ss\\ AM/PM;[$-F400]m/d/yy h:mm:ss\\ AM/PM;_-* \"\"??_-;_-@_-";
         String actual = formatter.formatRawCellContents(n, formatIndex, formatString);
         assertEquals("Failed on iteration " + iteration, expected, actual);
         return true;



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org