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.
* 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);
+ }
}
/**
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);
}
/**
@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) {
}
}
- 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;