Merge "Cherry-pick: ICU-20350 Fix DecimalFormatSymbols.setPatternForCurrencySpacing affecting the value across instances"
diff --git a/android_icu4j/src/main/java/android/icu/text/DecimalFormatSymbols.java b/android_icu4j/src/main/java/android/icu/text/DecimalFormatSymbols.java
index e6c6a24..c86515c 100644
--- a/android_icu4j/src/main/java/android/icu/text/DecimalFormatSymbols.java
+++ b/android_icu4j/src/main/java/android/icu/text/DecimalFormatSymbols.java
@@ -1068,8 +1068,10 @@
             throw new IllegalArgumentException("unknown currency spacing: " + itemType);
         }
         if (beforeCurrency) {
+            currencySpcBeforeSym = currencySpcBeforeSym.clone();
             currencySpcBeforeSym[itemType] = pattern;
         } else {
+            currencySpcAfterSym = currencySpcAfterSym.clone();
             currencySpcAfterSym[itemType] = pattern;
         }
     }
diff --git a/android_icu4j/src/main/tests/android/icu/dev/test/format/IntlTestDecimalFormatSymbols.java b/android_icu4j/src/main/tests/android/icu/dev/test/format/IntlTestDecimalFormatSymbols.java
index 19f2494..1752250 100644
--- a/android_icu4j/src/main/tests/android/icu/dev/test/format/IntlTestDecimalFormatSymbols.java
+++ b/android_icu4j/src/main/tests/android/icu/dev/test/format/IntlTestDecimalFormatSymbols.java
@@ -403,4 +403,25 @@
             assertEquals("JDK Locale and ICU Locale should produce the same object", dfs, dfs2);
         }
     }
+
+    @Test
+    public void testSetPatternForCurrencySpacing_notSharedByInstances() {
+        for (int type = DecimalFormatSymbols.CURRENCY_SPC_CURRENCY_MATCH; type <= DecimalFormatSymbols.CURRENCY_SPC_INSERT; type++) {
+            DecimalFormatSymbols dfs1 = DecimalFormatSymbols.getInstance(Locale.US);
+            DecimalFormatSymbols dfs2 = DecimalFormatSymbols.getInstance(Locale.US);
+            final String pattern = "foobar";
+            // before
+            dfs1.setPatternForCurrencySpacing(type, false, pattern);
+            assertEquals("setPatternForCurrencySpacing() must set the pattern", pattern,
+                    dfs1.getPatternForCurrencySpacing(type, false));
+            assertNotEquals("DFS instances must not share same pattern", pattern,
+                    dfs2.getPatternForCurrencySpacing(type, false));
+            // after
+            dfs1.setPatternForCurrencySpacing(type, true, pattern);
+            assertEquals("setPatternForCurrencySpacing() must set the pattern", pattern,
+                    dfs1.getPatternForCurrencySpacing(type, true));
+            assertNotEquals("DFS instances must not share same pattern", pattern,
+                    dfs2.getPatternForCurrencySpacing(type, true));
+        }
+    }
 }
diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormatSymbols.java b/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormatSymbols.java
index a01ac9a..8ad220f 100644
--- a/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormatSymbols.java
+++ b/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormatSymbols.java
@@ -1156,8 +1156,10 @@
             throw new IllegalArgumentException("unknown currency spacing: " + itemType);
         }
         if (beforeCurrency) {
+            currencySpcBeforeSym = currencySpcBeforeSym.clone();
             currencySpcBeforeSym[itemType] = pattern;
         } else {
+            currencySpcAfterSym = currencySpcAfterSym.clone();
             currencySpcAfterSym[itemType] = pattern;
         }
     }
diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/IntlTestDecimalFormatSymbols.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/IntlTestDecimalFormatSymbols.java
index 1042d3f..012946f 100644
--- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/IntlTestDecimalFormatSymbols.java
+++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/IntlTestDecimalFormatSymbols.java
@@ -400,4 +400,25 @@
             assertEquals("JDK Locale and ICU Locale should produce the same object", dfs, dfs2);
         }
     }
+
+    @Test
+    public void testSetPatternForCurrencySpacing_notSharedByInstances() {
+        for (int type = DecimalFormatSymbols.CURRENCY_SPC_CURRENCY_MATCH; type <= DecimalFormatSymbols.CURRENCY_SPC_INSERT; type++) {
+            DecimalFormatSymbols dfs1 = DecimalFormatSymbols.getInstance(Locale.US);
+            DecimalFormatSymbols dfs2 = DecimalFormatSymbols.getInstance(Locale.US);
+            final String pattern = "foobar";
+            // before
+            dfs1.setPatternForCurrencySpacing(type, false, pattern);
+            assertEquals("setPatternForCurrencySpacing() must set the pattern", pattern,
+                    dfs1.getPatternForCurrencySpacing(type, false));
+            assertNotEquals("DFS instances must not share same pattern", pattern,
+                    dfs2.getPatternForCurrencySpacing(type, false));
+            // after
+            dfs1.setPatternForCurrencySpacing(type, true, pattern);
+            assertEquals("setPatternForCurrencySpacing() must set the pattern", pattern,
+                    dfs1.getPatternForCurrencySpacing(type, true));
+            assertNotEquals("DFS instances must not share same pattern", pattern,
+                    dfs2.getPatternForCurrencySpacing(type, true));
+        }
+    }
 }