为什么 Joda Interval 没有具有可比性?

Why wasn't Joda Interval made comparable?

我一直在尝试生成一个 TreeMap<Interval, Set<Object>> 用于将个人实体存储在年龄组中,其中 Interval 描述了年龄组的边界。当我尝试这样做时出现以下错误。

java.lang.ClassCastException: org.joda.time.Interval cannot be cast to java.lang.Comparable

Interval 换成 DateTime 效果很好,但是必须使用地图的键作为输入来创建 Interval

为什么 Joda Interval 不具有可比性?

间隔可以通过多种方式进行比较。 Interval 的 javadoc 明确指出:

The duration is represented separately by ReadableDuration. As a result, intervals are not comparable. To compare the length of two intervals, you should compare their durations.

An interval can also be converted to a ReadablePeriod.

要获得更长的解释,请考虑以下间隔并定义它们的排序方式:

10:00 - 12:00
11:00 - 13:00
11:00 - 12:00
 9:00 - 10:00
 9:00 - 13:00

它们应该按持续时间(时间跨度)排序吗?

11:00 - 12:00  (1 hours)
 9:00 - 10:00  (1 hours)
10:00 - 12:00  (2 hours)
11:00 - 13:00  (2 hours)
 9:00 - 13:00  (4 hours)

还是按开始时间?

 9:00 - 10:00
 9:00 - 13:00
10:00 - 12:00
11:00 - 13:00
11:00 - 12:00

或者按开始时间,然后持续时间?

 9:00 - 10:00  (1 hours)
 9:00 - 13:00  (4 hours)
10:00 - 12:00  (2 hours)
11:00 - 12:00  (1 hours)
11:00 - 13:00  (2 hours)

或者按结束时间,然后持续时间?

 9:00 - 10:00  (1 hours)
11:00 - 12:00  (1 hours)
10:00 - 12:00  (2 hours)
11:00 - 13:00  (2 hours)
 9:00 - 13:00  (4 hours)

如您所见,“自然排序”没有一个明确的定义,Comparable 定义的是:

This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.