]> granicus.if.org Git - icu/commitdiff
ICU-10268 Serialize FormatWidth enums by their ordinal values.
authorTravis Keep <keep94@gmail.com>
Fri, 10 Jan 2014 16:55:55 +0000 (16:55 +0000)
committerTravis Keep <keep94@gmail.com>
Fri, 10 Jan 2014 16:55:55 +0000 (16:55 +0000)
X-SVN-Rev: 34858

icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/MeasureUnitTest.java

index 9553cf673bad74b84e858850798defbd0dfa3e23..85bee39a52de5729f3ccaf43e7a3b868ee0abd14 100644 (file)
@@ -860,7 +860,7 @@ public class MeasureFormat extends UFormat {
         public void writeExternal(ObjectOutput out) throws IOException {
             out.writeByte(0); // version
             out.writeUTF(locale.toLanguageTag());
-            out.writeByte(toFormatWidthOrdinal(formatWidth));
+            out.writeByte(formatWidth.ordinal());
             out.writeObject(numberFormat);
             out.writeByte(subClass);
             out.writeObject(keyValues);
@@ -912,36 +912,12 @@ public class MeasureFormat extends UFormat {
             }
         }
     }
-    // The next two methods must be maintained in lock step. 
-    // The int value associated with each FormatWidth value is the same for all time
-    // and must never change lest serialization breaks.
+    
     private static FormatWidth fromFormatWidthOrdinal(int ordinal) {
-        switch (ordinal) {
-        case 0:
-            return FormatWidth.WIDE;
-        case 1:
-            return FormatWidth.SHORT;
-        case 2:
-            return FormatWidth.NARROW;
-        case 3:
-            return FormatWidth.NUMERIC;
-        default:
+        FormatWidth[] values = FormatWidth.values();
+        if (ordinal < 0 || ordinal >= values.length) {
             return FormatWidth.WIDE;
         }
-    }
-    
-    private static int toFormatWidthOrdinal(FormatWidth fw) {
-        switch (fw) {
-        case WIDE:
-            return 0;
-        case SHORT:
-            return 1;
-        case NARROW:
-            return 2;
-        case NUMERIC:
-            return 3;
-        default:
-            throw new IllegalStateException("Unable to serialize Format Width " + fw);
-        }
+        return values[ordinal];
     }
 }
index ecc45b82dd514e9bc1439d1e4ada8f1e39c10a2b..e09aec1a5bf102c7dd5bda19b6da7f369c694356 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *******************************************************************************
- * Copyright (C) 2013, International Business Machines Corporation and         *
+ * Copyright (C) 2013-2014, International Business Machines Corporation and         *
  * others. All Rights Reserved.                                                *
  *******************************************************************************
  */
@@ -403,6 +403,15 @@ public class MeasureUnitTest extends TestFmwk {
         checkStreamingEquality(MeasureFormat.getCurrencyFormat(ULocale.ITALIAN));
     }
     
+    public void TestSerialFormatWidthEnum() {
+        // FormatWidth enum values must map to the same ordinal values for all time in order for
+        // serialization to work.
+        assertEquals("FormatWidth.WIDE", 0, FormatWidth.WIDE.ordinal());
+        assertEquals("FormatWidth.SHORT", 1, FormatWidth.SHORT.ordinal());
+        assertEquals("FormatWidth.NARROW", 2, FormatWidth.NARROW.ordinal());
+        assertEquals("FormatWidth.NUMERIC", 3, FormatWidth.NUMERIC.ordinal());
+    }
+    
     public <T extends Serializable> void checkStreamingEquality(T item) {
         try {
           ByteArrayOutputStream byteOut = new ByteArrayOutputStream();