I got hit with the (apparently infamous) IllegalArgumentException using Collections.sort() in Java7
Thanks to SO I understand the cause, which is basically (cough) poor code.
The thing is, I cannot reproduce the Exception myself. I did some jdk source code digging, and located which class is throwing that exception. The idea is to create the according Test Case.
Here is the code, by the way
< pride level=”0″ >
@Override
public int compareTo( Symbol other) {
if( this.lastUse == 0) {
if( other.lastUse != 0) return (int)( -DateMicros.ONE_DAY);
} else if( other.lastUse == 0) {
return ( int)DateMicros.ONE_DAY;
}
return ( int)( this.lastUse - other.lastUse);
}
< /pride >
On top of this, “lastUse” gets assigned timestamps in microseconds and milliseconds (yes mixed) which gives a superb int overflow spillage
The actual question is:
What Values Would make This Code Crash? to get a proper test case down the road.
Update with stack trace:
at java.util.ComparableTimSort.mergeHi(Unknown Source)
at java.util.ComparableTimSort.mergeAt(Unknown Source)
at java.util.ComparableTimSort.mergeCollapse(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
Hard to tell without looking at the stacktrace itself.
But the docs mentions that the method itself can throw the exception.
Update
It looks like because your object does not fulfull the
Comparableconsistency requirement: