Skip to content

Commit

Permalink
8311162: Simplify and modernize equals and hashCode for java.net
Browse files Browse the repository at this point in the history
Reviewed-by: dfuchs, michaelm, msheppar
  • Loading branch information
pavelrappo committed Jun 30, 2023
1 parent 430d6b6 commit e3a7e02
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 31 deletions.
26 changes: 10 additions & 16 deletions src/java.base/share/classes/java/net/NetworkInterface.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -28,6 +28,7 @@
import java.util.Arrays;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
Expand Down Expand Up @@ -586,18 +587,13 @@ public boolean isVirtual() {
* {@code false} otherwise.
* @see java.net.InetAddress#getAddress()
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof NetworkInterface that)) {
return false;
}
if (this.name != null ) {
if (!this.name.equals(that.name)) {
return false;
}
} else {
if (that.name != null) {
return false;
}
if (!Objects.equals(this.name, that.name)) {
return false;
}

if (this.addrs == null) {
Expand All @@ -612,13 +608,10 @@ public boolean equals(Object obj) {
return false;
}

InetAddress[] thatAddrs = that.addrs;
int count = thatAddrs.length;

for (int i=0; i<count; i++) {
for (InetAddress thisAddr : this.addrs) {
boolean found = false;
for (int j=0; j<count; j++) {
if (addrs[i].equals(thatAddrs[j])) {
for (InetAddress thatAddr : that.addrs) {
if (thisAddr.equals(thatAddr)) {
found = true;
break;
}
Expand All @@ -630,8 +623,9 @@ public boolean equals(Object obj) {
return true;
}

@Override
public int hashCode() {
return name == null? 0: name.hashCode();
return Objects.hashCode(name);
}

public String toString() {
Expand Down
15 changes: 7 additions & 8 deletions src/java.base/share/classes/java/net/Proxy.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,6 +25,8 @@

package java.net;

import java.util.Objects;

/**
* This class represents a proxy setting, typically a type (http, socks) and
* a socket address.
Expand Down Expand Up @@ -145,23 +147,20 @@ public String toString() {
* {@code false} otherwise.
* @see java.net.InetSocketAddress#equals(java.lang.Object)
*/
@Override
public final boolean equals(Object obj) {
if (!(obj instanceof Proxy p))
return false;
if (p.type() == type()) {
if (address() == null) {
return (p.address() == null);
} else
return address().equals(p.address());
return Objects.equals(address(), p.address());
}
return false;
}

/**
* Returns a hashcode for this Proxy.
*
* @return a hash code value for this Proxy.
* {@return a hash code value for this Proxy}
*/
@Override
public final int hashCode() {
if (address() == null)
return type().hashCode();
Expand Down
13 changes: 6 additions & 7 deletions src/java.base/share/classes/java/net/URLPermission.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -32,6 +32,7 @@
import java.util.Collections;
import java.security.Permission;
import java.util.Locale;
import java.util.Objects;

/**
* Represents permission to access a resource or set of resources defined by a
Expand Down Expand Up @@ -376,6 +377,7 @@ public boolean implies(Permission p) {
* Returns true if, this.getActions().equals(p.getActions())
* and p's url equals this's url. Returns false otherwise.
*/
@Override
public boolean equals(Object p) {
if (!(p instanceof URLPermission that)) {
return false;
Expand All @@ -389,22 +391,19 @@ public boolean equals(Object p) {
if (!this.authority.equals(that.authority)) {
return false;
}
if (this.path != null) {
return this.path.equals(that.path);
} else {
return that.path == null;
}
return Objects.equals(this.path, that.path);
}

/**
* Returns a hashcode calculated from the hashcode of the
* actions String and the url string.
*/
@Override
public int hashCode() {
return getActions().hashCode()
+ scheme.hashCode()
+ authority.hashCode()
+ (path == null ? 0 : path.hashCode());
+ Objects.hashCode(path);
}


Expand Down

1 comment on commit e3a7e02

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.