From ff30d8c3c306d660a58a9bad60e7256d372bc6e8 Mon Sep 17 00:00:00 2001 From: Dan Egnor Date: Fri, 20 Nov 2009 16:10:56 -0800 Subject: Fix bugs created by caching NumberFormat & DecimalFormatSymbols objects --- luni/src/main/java/java/util/Formatter.java | 26 +++++++++------------- .../org/apache/harmony/luni/util/LocaleCache.java | 14 +++++++----- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/luni/src/main/java/java/util/Formatter.java b/luni/src/main/java/java/util/Formatter.java index 9198886..dcd6c85 100644 --- a/luni/src/main/java/java/util/Formatter.java +++ b/luni/src/main/java/java/util/Formatter.java @@ -1164,9 +1164,11 @@ public final class Formatter implements Closeable, Flushable { private static String lineSeparator; - private NumberFormat numberFormat; - - private DecimalFormatSymbols decimalFormatSymbols; + // BEGIN android-changed + // These objects are mutated during use, so can't be cached safely. + // private NumberFormat numberFormat; + // private DecimalFormatSymbols decimalFormatSymbols; + // END android-changed private DateTimeUtil dateTimeUtil; @@ -1176,21 +1178,15 @@ public final class Formatter implements Closeable, Flushable { } private NumberFormat getNumberFormat() { - if (null == numberFormat) { - // BEGIN android-changed - numberFormat = LocaleCache.getNumberFormat(locale); - // END android-changed - } - return numberFormat; + // BEGIN android-changed + return LocaleCache.getNumberFormat(locale); + // END android-changed } private DecimalFormatSymbols getDecimalFormatSymbols() { - if (null == decimalFormatSymbols) { - // BEGIN android-changed - decimalFormatSymbols = LocaleCache.getDecimalFormatSymbols(locale); - // END android-changed - } - return decimalFormatSymbols; + // BEGIN android-changed + return LocaleCache.getDecimalFormatSymbols(locale); + // END android-changed } /* diff --git a/luni/src/main/java/org/apache/harmony/luni/util/LocaleCache.java b/luni/src/main/java/org/apache/harmony/luni/util/LocaleCache.java index c591faf..5318e38 100644 --- a/luni/src/main/java/org/apache/harmony/luni/util/LocaleCache.java +++ b/luni/src/main/java/org/apache/harmony/luni/util/LocaleCache.java @@ -55,27 +55,29 @@ public class LocaleCache { } /** - * Returns a NumberFormat object initialized with the specified - * Locale, re-using a previously returned object if possible. + * Returns a NumberFormat object for the specified Locale. */ public static NumberFormat getNumberFormat(Locale locale) { LocaleCache lc = getLocaleCache(locale); if (lc.numberFormat == null) { lc.numberFormat = NumberFormat.getInstance(locale); } - return lc.numberFormat; + + // NumberFormat is mutable, so return a new clone each time. + return (NumberFormat) lc.numberFormat.clone(); } /** - * Returns a DecimalFormatSymbols object initialized with the specified - * Locale, re-using a previously returned object if possible. + * Returns a DecimalFormatSymbols object for the specified Locale. */ public static DecimalFormatSymbols getDecimalFormatSymbols(Locale locale) { LocaleCache lc = getLocaleCache(locale); if (lc.decimalFormatSymbols == null) { lc.decimalFormatSymbols = new DecimalFormatSymbols(locale); } - return lc.decimalFormatSymbols; + + // DecimalFormatSymbols is mutable, so return a new clone each time. + return (DecimalFormatSymbols) lc.decimalFormatSymbols.clone(); } } -- cgit v1.1