I have this compareTo function that continues to, while not always, throw an error regarding its general contract, which if you have done sorting via the Comparable class, have probably hit at some point.
public int compareTo(FollowableEntity otherEntity) {
if(followTarget == null || otherEntity == null || otherEntity.followTarget == null || !otherEntity.follows) return 0;
if(this.followTarget != otherEntity.followTarget) return 0;
if(this.getDistanceSqToEntity(followTarget) == otherEntity.getDistanceToEntity(followTarget) && this.getDistanceSqToEntity(otherEntity) < this.getDistanceSqToEntity(followTarget)) return 1;
if(this.getDistanceSqToEntity(followTarget) == otherEntity.getDistanceToEntity(followTarget) && this.getDistanceSqToEntity(otherEntity) > this.getDistanceSqToEntity(followTarget)) return -1;
if(this.getDistanceSqToEntity(followTarget) < otherEntity.getDistanceSqToEntity(followTarget) && (this.getDistanceSqToEntity(followTarget) < this.getDistanceSqToEntity(otherEntity))) return -1;
if(this.getDistanceSqToEntity(followTarget) < otherEntity.getDistanceSqToEntity(followTarget) && (this.getDistanceSqToEntity(followTarget) > this.getDistanceSqToEntity(otherEntity))) return 1;
if(this.getDistanceSqToEntity(followTarget) > otherEntity.getDistanceSqToEntity(followTarget)) return 1;
return 0;
}
FollowableEntity has double position values: posX, posY, posZ.
(FollowableEntity(obj)).getDistanceSqToEntity(FollowableEntity) returns the squared distance between the entities as a double. (x1 - x)^2 + (y1 - y)^2 + (z1 - z)^2.
The first two conditions should logically never happen upon comparing, but I put them there anyway.
I'm not sure what conditions cause the error to be thrown, as the only times it is, there happen to be dozens of entities swirling around each other to reach their target.
Here is a portion of the log, my logging utility is rather inefficient.
17:23:37 - Caused by: java.lang.IllegalArgumentException: Comparison method violates its general contract!
17:23:37 - at java.util.ComparableTimSort.mergeLo(ComparableTimSort.java:714)
17:23:37 - at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:451)
17:23:37 - at java.util.ComparableTimSort.mergeCollapse(ComparableTimSort.java:376)
17:23:37 - at java.util.ComparableTimSort.sort(ComparableTimSort.java:182)
17:23:37 - at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
17:23:37 - at java.util.Arrays.sort(Arrays.java:472)
17:23:37 - at java.util.Collections.sort(Collections.java:155)
After that it points to the line that calls Collections.sort(), but doesn't tell me anything about the compareTo function.
I faced contract violations with Comparable
some time ago, and it was related to returning contradictory results; chances are that in some cases your method is returning 1 (or -1) for both object1.compareTo(object2)
and object2.compareTo(object1)
You can find a similar problem here.-
why does my compare method throw exception -- Comparison method violates its general contract!