0

I have the following compare function:

    @Override
    public int compareTo(OptimizedMatch another) {
        long c = this.compareTime - another.compareTime;
        if(c>0){
            return 1;
        } else if(c<0){
            return -1;
        }
        return 0;
    }

whereas compareTime is calculated in this way

this.compareTime = this.dateStamp + (LENGTH_OF_DAY - (this.dateTimeStamp - this.dateStamp));

Why am I getting this error?! I am comparing two long values, shouldnt that be just standard?

EDIT: I found the error, it was my IDE (Eclipse) being buggy. After cleaning my project and restarting it works.

A. Steenbergen
  • 3,360
  • 4
  • 34
  • 51

1 Answers1

1

There might be something wrong with your equals() method.

also, isn't

this.dateStamp + (LENGTH_OF_DAY - (this.dateTimeStamp - this.dateStamp))

equivalent to

2*this.dateStamp + LENGTH_OF_DAY - this.dateTimeStamp

I don't know what those fields do, but it looks like it's not what you want to do there.

koljaTM
  • 10,064
  • 2
  • 40
  • 42
  • You are correct, I just didn't shorten the formula yet to make it more obvious what it is supposed to do. The fields are all standard longs, so all normal operators should work fine in theory, no? – A. Steenbergen Nov 30 '13 at 22:14
  • Yes, I was just confused. The problem is in the equals() most likely, as I wrote and the linked question suggests. If compareTo returns 0 and the objects are not equal (e.g. if you use the standard equals implementation), java complains. – koljaTM Dec 02 '13 at 10:04