Skip to content

Commit

Permalink
Fix use of thread-unsafe DecimalFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Feb 7, 2023
1 parent 997b958 commit 0274e38
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public void setTransformation(double translateX, double translateY, double rotat
stream.write(formatCoordinate(-translateY));
if (rotate != 0) {
stream.write(") rotate(");
stream.write(ANGLE_FORMAT.format(-rotate / Math.PI * 180));
stream.write(ANGLE_FORMAT.get().format(-rotate / Math.PI * 180));
}
if (scaleX != 1 || scaleY != 1) {
stream.write(") scale(");
Expand Down Expand Up @@ -284,15 +284,18 @@ public void saveAs(Path path) throws IOException {
}
}

private static final DecimalFormat NUMBER_FORMAT = new DecimalFormat("#.###", new DecimalFormatSymbols(Locale.UK));
private static final DecimalFormat ANGLE_FORMAT = new DecimalFormat("#.#####", new DecimalFormatSymbols(Locale.UK));
private static final ThreadLocal<DecimalFormat> NUMBER_FORMAT
= ThreadLocal.withInitial(() -> new DecimalFormat("#.###", new DecimalFormatSymbols(Locale.UK)));

private static final ThreadLocal<DecimalFormat> ANGLE_FORMAT
= ThreadLocal.withInitial(() -> new DecimalFormat("#.#####", new DecimalFormatSymbols(Locale.UK)));

private static String formatNumber(double value) {
return NUMBER_FORMAT.format(value);
return NUMBER_FORMAT.get().format(value);
}

private static String formatCoordinate(double value) {
return NUMBER_FORMAT.format(value * MM_TO_PT);
return NUMBER_FORMAT.get().format(value * MM_TO_PT);
}

private static String formatColor(int color) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,18 +585,17 @@ private void drawCorners(double x, double y, double width, double height) throws
graphics.strokePath(CORNER_STROKE_WIDTH, 0, Canvas.LineStyle.Solid, false);
}

private static final DecimalFormat amountDisplayFormat;

static {
amountDisplayFormat = new DecimalFormat("###,##0.00");
private static final ThreadLocal<DecimalFormat> AMOUNT_DISPLAY_FORMAT = ThreadLocal.withInitial(() -> {
DecimalFormat format = new DecimalFormat("###,##0.00");
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(' ');
amountDisplayFormat.setDecimalFormatSymbols(symbols);
}
format.setDecimalFormatSymbols(symbols);
return format;
});

private static String formatAmountForDisplay(BigDecimal amount) {
return amountDisplayFormat.format(amount);
return AMOUNT_DISPLAY_FORMAT.get().format(amount);
}

private static String formatAddressForDisplay(Address address, boolean withCountryCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,17 @@ private void appendDataField(String value) {
textBuilder.append('\n').append(value);
}

private static final DecimalFormat amountFieldFormat;

static {
amountFieldFormat = new DecimalFormat("0.00");
private static final ThreadLocal<DecimalFormat> AMOUNT_FIELD_FORMAT = ThreadLocal.withInitial(() -> {
DecimalFormat format = new DecimalFormat("0.00");
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
symbols.setDecimalSeparator('.');
amountFieldFormat.setDecimalFormatSymbols(symbols);
amountFieldFormat.setParseBigDecimal(true);
}
format.setDecimalFormatSymbols(symbols);
format.setParseBigDecimal(true);
return format;
});

private static String formatAmountForCode(BigDecimal amount) {
return amountFieldFormat.format(amount);
return AMOUNT_FIELD_FORMAT.get().format(amount);
}

// According to a letter from SIX dated August 5, 2020, only the major number (leading "02") should be checked
Expand Down Expand Up @@ -157,7 +156,7 @@ public static Bill decode(String text) {

if (lines[18].length() > 0) {
ParsePosition position = new ParsePosition(0);
BigDecimal amount = (BigDecimal) amountFieldFormat.parse(lines[18], position);
BigDecimal amount = (BigDecimal) AMOUNT_FIELD_FORMAT.get().parse(lines[18], position);
if (position.getIndex() == lines[18].length())
bill.setAmount(amount);
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,15 @@ private static Integer getIntValue(String intText) {
}
}

private static final DecimalFormat SWICO_NUMBER_FORMAT;

static {
SWICO_NUMBER_FORMAT = new DecimalFormat("0.###", new DecimalFormatSymbols(Locale.UK));
SWICO_NUMBER_FORMAT.setParseBigDecimal(true);
}
private static final ThreadLocal<DecimalFormat> SWICO_NUMBER_FORMAT = ThreadLocal.withInitial(() -> {
DecimalFormat format = new DecimalFormat("0.###", new DecimalFormatSymbols(Locale.UK));
format.setParseBigDecimal(true);
return format;
});

private static BigDecimal getDecimalValue(String decimalText) {
ParsePosition position = new ParsePosition(0);
BigDecimal decimal = (BigDecimal) SWICO_NUMBER_FORMAT.parse(decimalText, position);
BigDecimal decimal = (BigDecimal) SWICO_NUMBER_FORMAT.get().parse(decimalText, position);
return (position.getIndex() == decimalText.length()) ? decimal : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ private static String s1Date(LocalDate date) {
return date.format(SWICO_DATE_FORMAT);
}

private static final DecimalFormat SWICO_NUMBER_FORMAT
= new DecimalFormat("0.###", new DecimalFormatSymbols(Locale.UK));
private static final ThreadLocal<DecimalFormat> SWICO_NUMBER_FORMAT
= ThreadLocal.withInitial(() -> new DecimalFormat("0.###", new DecimalFormatSymbols(Locale.UK)));

private static String s1Number(BigDecimal num) {
return SWICO_NUMBER_FORMAT.format(num);
return SWICO_NUMBER_FORMAT.get().format(num);
}

private static void appendRateDetailTupleList(StringBuilder sb, List<RateDetail> list) {
Expand Down

0 comments on commit 0274e38

Please sign in to comment.