From 417c81f54d3ca31bb8b4d2c116df6fa21271d771 Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Thu, 6 Feb 2014 12:22:53 +0100 Subject: [PATCH] Use Guavas primitive compare methods, where appropriate. Makes the code easier to read. Passes all unit tests. --- .../main/java/com/google/bitcoin/core/PeerGroup.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java index 576f334f..14108bde 100644 --- a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java +++ b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java @@ -28,6 +28,8 @@ import com.google.bitcoin.utils.Threading; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.collect.Sets; +import com.google.common.primitives.Ints; +import com.google.common.primitives.Longs; import com.google.common.util.concurrent.*; import net.jcip.annotations.GuardedBy; import org.slf4j.Logger; @@ -297,7 +299,7 @@ public class PeerGroup extends AbstractExecutionThreadService implements Transac int result = backoffMap.get(a).compareTo(backoffMap.get(b)); // Sort by port if otherwise equals - for testing if (result == 0) - result = Integer.valueOf(a.getPort()).compareTo(b.getPort()); + result = Ints.compare(a.getPort(), b.getPort()); return result; } }); @@ -1488,13 +1490,7 @@ public class PeerGroup extends AbstractExecutionThreadService implements Transac // Sort by ping time. Collections.sort(candidates2, new Comparator() { public int compare(PeerAndPing peerAndPing, PeerAndPing peerAndPing2) { - if (peerAndPing.pingTime < peerAndPing2.pingTime) - return -1; - else if (peerAndPing.pingTime == peerAndPing2.pingTime) - return 0; - else - return 1; - + return Longs.compare(peerAndPing.pingTime, peerAndPing2.pingTime); } }); return candidates2.get(0).peer;