From 34c1616279efe08089960aafd3df486cf8a04820 Mon Sep 17 00:00:00 2001 From: pekow Date: Fri, 13 Jul 2018 12:18:05 -0700 Subject: [PATCH] Change HostAndPort equals() and hashCode() to stop depending on whether brackets were included Now they depend only on host and port. Useful for storing IPv6 addresses in Set and Map RELNOTES=`net.HostAndPort`: Changed equals() and hashCode() to stop depending on whether brackets were included. Now they depend only on host and port. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=204509648 --- .../google/common/net/HostAndPortTest.java | 20 +++++++++++-------- .../com/google/common/net/HostAndPort.java | 6 ++---- .../google/common/net/HostAndPortTest.java | 20 +++++++++++-------- .../com/google/common/net/HostAndPort.java | 6 ++---- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/android/guava-tests/test/com/google/common/net/HostAndPortTest.java b/android/guava-tests/test/com/google/common/net/HostAndPortTest.java index e88a8134cc97..5e7eb2f73d71 100644 --- a/android/guava-tests/test/com/google/common/net/HostAndPortTest.java +++ b/android/guava-tests/test/com/google/common/net/HostAndPortTest.java @@ -193,15 +193,19 @@ public void testGetPortOrDefault() { } public void testHashCodeAndEquals() { - HostAndPort hp1 = HostAndPort.fromString("foo::123"); - HostAndPort hp2 = HostAndPort.fromString("foo::123"); - HostAndPort hp3 = HostAndPort.fromString("[foo::123]"); - HostAndPort hp4 = HostAndPort.fromParts("[foo::123]", 80); - HostAndPort hp5 = HostAndPort.fromString("[foo::123]:80"); + HostAndPort hpNoPort1 = HostAndPort.fromString("foo::123"); + HostAndPort hpNoPort2 = HostAndPort.fromString("foo::123"); + HostAndPort hpNoPort3 = HostAndPort.fromString("[foo::123]"); + HostAndPort hpNoPort4 = HostAndPort.fromHost("[foo::123]"); + HostAndPort hpNoPort5 = HostAndPort.fromHost("foo::123"); + + HostAndPort hpWithPort1 = HostAndPort.fromParts("[foo::123]", 80); + HostAndPort hpWithPort2 = HostAndPort.fromParts("foo::123", 80); + HostAndPort hpWithPort3 = HostAndPort.fromString("[foo::123]:80"); + new EqualsTester() - .addEqualityGroup(hp1, hp2) - .addEqualityGroup(hp3) - .addEqualityGroup(hp4, hp5) + .addEqualityGroup(hpNoPort1, hpNoPort2, hpNoPort3, hpNoPort4, hpNoPort5) + .addEqualityGroup(hpWithPort1, hpWithPort2, hpWithPort3) .testEquals(); } diff --git a/android/guava/src/com/google/common/net/HostAndPort.java b/android/guava/src/com/google/common/net/HostAndPort.java index 4b4babe2a0a6..df8ded405349 100644 --- a/android/guava/src/com/google/common/net/HostAndPort.java +++ b/android/guava/src/com/google/common/net/HostAndPort.java @@ -283,16 +283,14 @@ public boolean equals(@NullableDecl Object other) { } if (other instanceof HostAndPort) { HostAndPort that = (HostAndPort) other; - return Objects.equal(this.host, that.host) - && this.port == that.port - && this.hasBracketlessColons == that.hasBracketlessColons; + return Objects.equal(this.host, that.host) && this.port == that.port; } return false; } @Override public int hashCode() { - return Objects.hashCode(host, port, hasBracketlessColons); + return Objects.hashCode(host, port); } /** Rebuild the host:port string, including brackets if necessary. */ diff --git a/guava-tests/test/com/google/common/net/HostAndPortTest.java b/guava-tests/test/com/google/common/net/HostAndPortTest.java index e88a8134cc97..5e7eb2f73d71 100644 --- a/guava-tests/test/com/google/common/net/HostAndPortTest.java +++ b/guava-tests/test/com/google/common/net/HostAndPortTest.java @@ -193,15 +193,19 @@ public void testGetPortOrDefault() { } public void testHashCodeAndEquals() { - HostAndPort hp1 = HostAndPort.fromString("foo::123"); - HostAndPort hp2 = HostAndPort.fromString("foo::123"); - HostAndPort hp3 = HostAndPort.fromString("[foo::123]"); - HostAndPort hp4 = HostAndPort.fromParts("[foo::123]", 80); - HostAndPort hp5 = HostAndPort.fromString("[foo::123]:80"); + HostAndPort hpNoPort1 = HostAndPort.fromString("foo::123"); + HostAndPort hpNoPort2 = HostAndPort.fromString("foo::123"); + HostAndPort hpNoPort3 = HostAndPort.fromString("[foo::123]"); + HostAndPort hpNoPort4 = HostAndPort.fromHost("[foo::123]"); + HostAndPort hpNoPort5 = HostAndPort.fromHost("foo::123"); + + HostAndPort hpWithPort1 = HostAndPort.fromParts("[foo::123]", 80); + HostAndPort hpWithPort2 = HostAndPort.fromParts("foo::123", 80); + HostAndPort hpWithPort3 = HostAndPort.fromString("[foo::123]:80"); + new EqualsTester() - .addEqualityGroup(hp1, hp2) - .addEqualityGroup(hp3) - .addEqualityGroup(hp4, hp5) + .addEqualityGroup(hpNoPort1, hpNoPort2, hpNoPort3, hpNoPort4, hpNoPort5) + .addEqualityGroup(hpWithPort1, hpWithPort2, hpWithPort3) .testEquals(); } diff --git a/guava/src/com/google/common/net/HostAndPort.java b/guava/src/com/google/common/net/HostAndPort.java index ff9081425950..c01b87a15cd5 100644 --- a/guava/src/com/google/common/net/HostAndPort.java +++ b/guava/src/com/google/common/net/HostAndPort.java @@ -283,16 +283,14 @@ public boolean equals(@Nullable Object other) { } if (other instanceof HostAndPort) { HostAndPort that = (HostAndPort) other; - return Objects.equal(this.host, that.host) - && this.port == that.port - && this.hasBracketlessColons == that.hasBracketlessColons; + return Objects.equal(this.host, that.host) && this.port == that.port; } return false; } @Override public int hashCode() { - return Objects.hashCode(host, port, hasBracketlessColons); + return Objects.hashCode(host, port); } /** Rebuild the host:port string, including brackets if necessary. */