最近碰到一件很有趣的事情,工作中需要处理有关一周中某个工作日的信息,正好Java8提供了一个DayOfWeek现成的类,奇怪为什么这么基本的类Java8才提供,这是后话。
既然要用DayOfWeek这个类,就先看看它的文档: A day-of-week, such as 'Tuesday'. DayOfWeek is an enum representing the 7 days of the week - Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday. docs.oracle.com |
DayOfWeek是个枚举类,下面是它的常量总结: Enum Constant Summary Enum Constants Enum Constant and Description FRIDAY The singleton instance for the day-of-week of Friday. MONDAY The singleton instance for the day-of-week of Monday. SATURDAY The singleton instance for the day-of-week of Saturday. SUNDAY The singleton instance for the day-of-week of Sunday. THURSDAY The singleton instance for the day-of-week of Thursday. TUESDAY The singleton instance for the day-of-week of Tuesday. WEDNESDAY The singleton instance for the day-of-week of Wednesday. 奇怪,怎么文档中常量是从Friday开始,难道Friday的顺序数是1? 继续往下读文档,顺序数1是Monday,而不是Friday。 再继续读了一下DayOfWeek源码,没错,一个星期的7天是从Monday到Sunday顺序的,并且从Monday的顺序数1排到Sunday的顺序数7. 这是怎么回事?
仔细看了一下DayOfWeek的“Constant Summary”, 发现从FRIDAY到WEDNESDAY
是字母排序,又看了它的“Method Summary”, 也是字母排序,莫非Java文档的概述部分都是字母排序
原因在此:
原来缺省的javadoc输出内容和格式的工具doclet使用了Collections.sort()来
排序常量, 域, 成员和方法的Summaries,源码见下面链接:
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/com/sun/tools/doclets/internal/toolkit/builders/EnumConstantBuilder.java?av=h#EnumConstantBuilder
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/com/sun/tools/doclets/internal/toolkit/builders/FieldBuilder.java?av=h#FieldBuilder
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/com/sun/tools/doclets/internal/toolkit/builders/MemberSummaryBuilder.java?av=h#MemberSummaryBuilder
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/com/sun/tools/doclets/internal/toolkit/builders/MethodBuilder.java?av=h#MethodBuilder
这肯定是有意为之, 因为这种字母排序方便了读者对相应变量方法的查找, 也利于自动查找。
然而从另一方面而言, 也会造成像DayOfWeek这种让读者感觉奇怪的常量说明, 只不过类似这种情形大家见的不多罢了。