From the java.text.ChoiceFormat API:
setChoices(double[] limits, String[] formats): Set the choices to be used in formatting.Parameters:
limits– contains […]formats– are the formats you want to use for each limit. They can be eitherFormatobjects orStrings. When formatting with objectY, if the object is aNumberFormat, then((NumberFormat) Y).format(X)is called. OtherwiseY.toString()is called.
I’m having difficulties understanding the documentation for the formats parameter: how can you possibly pass a Format/NumberFormat object to setChoices if it’s declared String[] formats?
Note that interestingly, the getters counterpart of setChoices are declared as follows:
double[] getLimits()Object[] getFormats()— notString[]!!!
Is this a bug in the API? Should the setter have been declared setChoices(double[], Object[]) instead, or am I not understanding how to use setChoices correctly?
This has been reported and accepted as Bug 6960866.
A
String[]can never contain aninstanceof Number/NumberFormat; that goes against every OOP subtyping principles.If you look at the source code, the
privatefield is declared asString[] choiceFormats, so simply declaringsetChoices(double[], Object[])is not an easy fix, and would instead break the code. In fact, looking at the rest of the code, there is no functionality like what the documentation claims: there is noinstanceof Numbertest, no(NumberFormat)cast anywhere in the code.Thus, given the current state of the source code, the bug is in the documentation, which claims functionality which is neither possible nor actually implemented.
Such functionality would be very nice to have, and probably should exist, but currently it doesn’t, so this can also be seen as a bug in the source code, which is missing the implementation.
References
java.text.ChoiceFormatsource code (OpenJDK version)