【发布时间】:2013-06-07 14:52:09
【问题描述】:
我的一个应用程序曾经抛出一个 IllegalArgumentException,指出比较方法违反了它的一般合同。 我找到了一些详细说明问题的来源,例如 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6804124 和 http://www.oracle.com/technetwork/java/javase/compatibility-417013.html#source,并希望在我的应用程序中解决此问题。
但我无法重现该问题,因此无法知道我的修复是否正确。
在我努力复现的过程中,我尽量简化问题,并想出了一个看起来像这样的小类:
public class Sortee implements Comparable<Sortee>
{
/** a value to sort by */
public final int _x;
public Sortee(int x)
{
_x = x;
}
public int compareTo(Sortee o)
{
return 1;
}
}
我还创建了一个等价的比较器:
public class SorteeIncorrectComparator implements Comparator<Sortee>
{
public int compare(Sortee a, Sortee b)
{
return 1;
}
}
在另一个类中,我创建了一个 Sortee 对象列表并调用 Collections.sort() 变体来引发 IllegalStateException:
private static void sort()
{
List<Sortee> sortees = createSortees();
Collections.shuffle( sortees );
Collections.sort( sortees, new SorteeIncorrectComparator() );
Collections.shuffle( sortees );
Collections.sort( sortees );
}
但永远不会引发 IllegalStateException。
我已经在 Linux 和 Windows 以及在 Windows 上使用 Java 1.7.0_21、23.21-b01 的 eclipse 中尝试过它 并检查属性 java.util.Arrays.useLegacyMergeSort 没有设置。
我认为在 compare 方法中总是返回 1 应该会破坏契约,因为它既不交换也不传递。
为什么我从来没有收到 IllegalStateException?
【问题讨论】:
-
我不确定我是否看到编写全新代码以重现其他代码中出现的问题的好处。为什么不发布引发异常的代码,让我们发现错误?
标签: java comparator comparable illegalstateexception