ChoiceFormat.setChoices 关于格式参数类型和文档的混淆
来自 java.text.ChoiceFormat
API:
setChoices(double[]limits, String[]formats)
:设置格式化时使用的选项。参数:
限制
- 包含[...]格式
- 是您要用于每个限制的格式。它们可以是Format
对象或String
。使用对象Y
进行格式化时,如果该对象是NumberFormat
,则调用((NumberFormat) Y).format(X)
。否则调用Y.toString()
。
我很难理解 formats
参数的文档:如果将 Format/NumberFormat
对象声明为 setChoices
,那么如何将其传递给 setChoices
代码>字符串[]格式?
请注意,有趣的是,setChoices
的 getter 对应项声明如下:
double[] getLimits()
Object[] getFormats()
-- 不是String []
!!!
这是 API 中的错误吗? setter 是否应该被声明为 setChoices(double[], Object[])
,还是我不明白如何正确使用 setChoices
?
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 eitherFormat
objects orString
s. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以检查 源代码
在引用字符串/格式化程序二元性的注释中到处都提到了,但是实现仅复制字符串
,例如格式化双精度数:
在返回中,您清楚地看到它只是从字符串数组复制,并且没有尝试格式化完成了。
我只是认为该功能被“遗忘”了。
You can check the source code
Everywhere mention is made in comments refering to the string/formatter duality, however the implementation only copies strings
e.g. formatting a double :
In the return you clearly see it just copies from the stringarray and no attempt to format is done.
I just think that functionality was 'forgotten'.
它看起来绝对像一个错误。格式直接分配给 String[] 实例变量。 源代码。
It definitely looks like a bug. formats is assigned directly to a String[] instance variable. Source code.
此问题已被报告并接受为 Bug 6960866。
String[]
永远不能包含Number/NumberFormat
实例;这违背了所有 OOP 子类型原则。如果您查看源代码,
private
字段被声明为String[] choiceFormats
,因此只需声明setChoices(double[], Object [])
不是一个简单的修复,反而会破坏代码。事实上,看看代码的其余部分,没有像文档声称的那样的功能:没有instanceof Number
测试,没有(NumberFormat)
在任何地方进行强制转换代码。因此,考虑到源代码的当前状态,该错误位于文档中,该文档声称的功能既不可能也不实际实现。
如果有这样的功能就太好了,而且可能应该存在,但目前还没有,所以这也可以被视为源代码中的一个错误,缺少实施。
参考文献
java.text.ChoiceFormat
源代码(OpenJDK版本)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
private
field 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 Number
test, 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.ChoiceFormat
source code (OpenJDK version)