ICU-10268 Serialize FormatWidth enums by their ordinal values.

X-SVN-Rev: 34858
This commit is contained in:
Travis Keep 2014-01-10 16:55:55 +00:00
parent a50418ca17
commit 1ebcd33989
2 changed files with 16 additions and 31 deletions

View 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:
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);
private static FormatWidth fromFormatWidthOrdinal(int ordinal) {
FormatWidth[] values = FormatWidth.values();
if (ordinal < 0 || ordinal >= values.length) {
return FormatWidth.WIDE;
}
return values[ordinal];
}
}

View 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();