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);
}
}
}
- // 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];
}
}
/*
*******************************************************************************
- * Copyright (C) 2013, International Business Machines Corporation and *
+ * Copyright (C) 2013-2014, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
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();