From 0424c08c7acd2f44b9ff623617960df025a76601 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Fri, 21 Apr 2017 13:56:17 -0500 Subject: [PATCH] PeerAddress: Add standard hashCode and equals method Cherry pick https://github.com/bisq-network/bitcoinj/commit/b9d840c1b17e6fc544d81a51f8e3e2b0d5182361 --- .../java/org/bitcoinj/core/PeerAddress.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/core/PeerAddress.java b/core/src/main/java/org/bitcoinj/core/PeerAddress.java index 70469190661..13df7c160d4 100644 --- a/core/src/main/java/org/bitcoinj/core/PeerAddress.java +++ b/core/src/main/java/org/bitcoinj/core/PeerAddress.java @@ -17,7 +17,6 @@ package org.bitcoinj.core; -import com.google.common.base.Objects; import com.google.common.net.InetAddresses; import org.bitcoinj.net.AddressChecker; import org.bitcoinj.net.OnionCat; @@ -304,16 +303,27 @@ public String toString() { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - PeerAddress other = (PeerAddress) o; - return other.addr.equals(addr) && other.port == port && other.time == time && other.services.equals(services); - //TODO: including services and time could cause same peer to be added multiple times in collections + + PeerAddress that = (PeerAddress) o; + + if (port != that.port) return false; + if (time != that.time) return false; + if (addr != null ? !addr.equals(that.addr) : that.addr != null) return false; + if (hostname != null ? !hostname.equals(that.hostname) : that.hostname != null) return false; + return !(services != null ? !services.equals(that.services) : that.services != null); + } @Override public int hashCode() { - return Objects.hashCode(addr, port, time, services); + int result = addr != null ? addr.hashCode() : 0; + result = 31 * result + (hostname != null ? hostname.hashCode() : 0); + result = 31 * result + port; + result = 31 * result + (services != null ? services.hashCode() : 0); + result = 31 * result + (int) (time ^ (time >>> 32)); + return result; } - + public InetSocketAddress toSocketAddress() { // Reconstruct the InetSocketAddress properly if (hostname != null) {