Use Double.compare() in comparators (findbugs)

This commit is contained in:
zzz
2018-10-08 13:34:15 +00:00
parent 524c375944
commit 6fccfc990a
3 changed files with 12 additions and 29 deletions

View File

@ -115,11 +115,7 @@ class SybilRenderer {
reasons.add(reason); reasons.add(reason);
} }
public int compareTo(Points r) { public int compareTo(Points r) {
if (points > r.points) return Double.compare(points, r.points);
return 1;
if (points < r.points)
return -1;
return 0;
} }
} }

View File

@ -23,11 +23,13 @@ class InverseCapacityComparator implements Comparator<PeerProfile>, Serializable
double rval = right.getCapacityValue(); double rval = right.getCapacityValue();
double lval = left.getCapacityValue(); double lval = left.getCapacityValue();
int rv = Double.compare(rval, lval);
if (lval == rval) { if (rv == 0) {
rval = right.getSpeedValue(); rval = right.getSpeedValue();
lval = left.getSpeedValue(); lval = left.getSpeedValue();
if (lval == rval) { rv = Double.compare(rval, lval);
if (rv == 0) {
// note the following call inverts right and left (see: classname) // note the following call inverts right and left (see: classname)
return DataHelper.compareTo(right.getPeer().getData(), left.getPeer().getData()); return DataHelper.compareTo(right.getPeer().getData(), left.getPeer().getData());
} else { } else {
@ -35,18 +37,6 @@ class InverseCapacityComparator implements Comparator<PeerProfile>, Serializable
} }
} }
boolean rightBigger = rval > lval; return rv;
//if (_log.shouldLog(Log.DEBUG))
// _log.debug("The capacity of " + right.getPeer().toBase64()
// + " and " + left.getPeer().toBase64() + " marks " + (rightBigger ? "right" : "left")
// + " as larger: r=" + right.getCapacityValue()
// + " l="
// + left.getCapacityValue());
if (rightBigger)
return 1;
else
return -1;
} }
} }

View File

@ -15,19 +15,16 @@ class SpeedComparator implements Comparator<PeerProfile>, Serializable {
double lval = left.getSpeedValue(); double lval = left.getSpeedValue();
double rval = right.getSpeedValue(); double rval = right.getSpeedValue();
int rv = Double.compare(lval, rval);
if (lval > rval) if (rv != 0)
return 1; return rv;
if (lval < rval)
return -1;
// we don't wan't to return 0 so profiles don't vanish in the TreeSet // we don't wan't to return 0 so profiles don't vanish in the TreeSet
lval = left.getCapacityValue(); lval = left.getCapacityValue();
rval = right.getCapacityValue(); rval = right.getCapacityValue();
if (lval > rval) rv = Double.compare(lval, rval);
return 1; if (rv != 0)
if (lval < rval) return rv;
return -1;
return DataHelper.compareTo(right.getPeer().getData(), left.getPeer().getData()); return DataHelper.compareTo(right.getPeer().getData(), left.getPeer().getData());
} }
} }