ChoiceFormat.setChoices 关于格式参数类型和文档的混淆

发布于 2024-09-05 00:21:19 字数 1465 浏览 3 评论 0原文

来自 java.text.ChoiceFormatAPI:

setChoices(double[]limits, String[]formats):设置格式化时使用的选项。

参数:

  • 限制 - 包含[...]
  • 格式 - 是您要用于每个限制的格式。它们可以是Format对象或String。使用对象 Y 进行格式化时,如果该对象是 NumberFormat,则调用 ((NumberFormat) Y).format(X)。否则调用Y.toString()

我很难理解 formats 参数的文档:如果将 Format/NumberFormat 对象声明为 setChoices ,那么如何将其传递给 setChoices代码>字符串[]格式?

请注意,有趣的是,setChoices 的 getter 对应项声明如下:

这是 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 either Format objects or Strings. When formatting with object Y, if the object is a NumberFormat, then ((NumberFormat) Y).format(X) is called. Otherwise Y.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:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终难愈 2024-09-12 00:21:19

您可以检查 源代码

在引用字符串/格式化程序二元性的注释中到处都提到了,但是实现仅复制字符串

,例如格式化双精度数:

        public StringBuffer format(double number, StringBuffer toAppendTo,
                FieldPosition status) {
            // find the number
            int i;
            for (i = 0; i < choiceLimits.length; ++i) {
                if (!(number >= choiceLimits[i])) {
                    // same as number < choiceLimits, except catchs NaN
                    break;
                }
            }
            --i;
            if (i < 0)
                i = 0;
            // return either a formatted number, or a string
            return toAppendTo.append(choiceFormats[i]);
        }

在返回中,您清楚地看到它只是从字符串数组复制,并且没有尝试格式化完成了。

我只是认为该功能被“遗忘”了。

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 :

        public StringBuffer format(double number, StringBuffer toAppendTo,
                FieldPosition status) {
            // find the number
            int i;
            for (i = 0; i < choiceLimits.length; ++i) {
                if (!(number >= choiceLimits[i])) {
                    // same as number < choiceLimits, except catchs NaN
                    break;
                }
            }
            --i;
            if (i < 0)
                i = 0;
            // return either a formatted number, or a string
            return toAppendTo.append(choiceFormats[i]);
        }

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'.

橘寄 2024-09-12 00:21:19

它看起来绝对像一个错误。格式直接分配给 String[] 实例变量。 源代码

It definitely looks like a bug. formats is assigned directly to a String[] instance variable. Source code.

温暖的光 2024-09-12 00:21:19

此问题已被报告并接受为 Bug 6960866

String[] 永远不能包含 Number/NumberFormat 实例;这违背了所有 OOP 子类型原则。

如果您查看源代码,private 字段被声明为 String[] choiceFormats,因此只需声明 setChoices(double[], Object []) 不是一个简单的修复,反而会破坏代码。事实上,看看代码的其余部分,没有像文档声称的那样的功能:没有 instanceof Number 测试,没有 (NumberFormat) 在任何地方进行强制转换代码。

因此,考虑到源代码的当前状态,该错误位于文档中,该文档声称的功能既不可能也不实际实现。

如果有这样的功能就太好了,而且可能应该存在,但目前还没有,所以这也可以被视为源代码中的一个错误,缺少实施。

参考文献

This has been reported and accepted as Bug 6960866.

A String[] can never contain an instanceof Number/NumberFormat; that goes against every OOP subtyping principles.

If you look at the source code, the private field is declared as String[] choiceFormats, so simply declaring setChoices(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 no instanceof 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文