Skip to content

Commit

Permalink
Change HostAndPort equals() and hashCode() to stop depending on wheth…
Browse files Browse the repository at this point in the history
…er 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
  • Loading branch information
slix authored and ronshapiro committed Jul 16, 2018
1 parent 08adcec commit 34c1616
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
Expand Up @@ -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();
}

Expand Down
6 changes: 2 additions & 4 deletions android/guava/src/com/google/common/net/HostAndPort.java
Expand Up @@ -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. */
Expand Down
20 changes: 12 additions & 8 deletions guava-tests/test/com/google/common/net/HostAndPortTest.java
Expand Up @@ -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();
}

Expand Down
6 changes: 2 additions & 4 deletions guava/src/com/google/common/net/HostAndPort.java
Expand Up @@ -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. */
Expand Down

0 comments on commit 34c1616

Please sign in to comment.