From: Travis Keep Date: Fri, 10 Jan 2014 16:55:55 +0000 (+0000) Subject: ICU-10268 Serialize FormatWidth enums by their ordinal values. X-Git-Tag: milestone-59-0-1~2288 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1ebcd33989352ef6ede09d79af5cf1c5f722cc7a;p=icu ICU-10268 Serialize FormatWidth enums by their ordinal values. X-SVN-Rev: 34858 --- diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java index 9553cf673ba..85bee39a52d 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java @@ -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]; } } diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/MeasureUnitTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/MeasureUnitTest.java index ecc45b82dd5..e09aec1a5bf 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/MeasureUnitTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/MeasureUnitTest.java @@ -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 void checkStreamingEquality(T item) { try { ByteArrayOutputStream byteOut = new ByteArrayOutputStream();