8311162: Simplify and modernize equals and hashCode for java.net
Reviewed-by: dfuchs, michaelm, msheppar
This commit is contained in:
parent
430d6b61c5
commit
e3a7e020d2
@ -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
|
||||
@ -28,6 +28,7 @@ package java.net;
|
||||
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;
|
||||
@ -586,18 +587,13 @@ public final class NetworkInterface {
|
||||
* {@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) {
|
||||
@ -612,13 +608,10 @@ public final class NetworkInterface {
|
||||
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;
|
||||
}
|
||||
@ -630,8 +623,9 @@ public final class NetworkInterface {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name == null? 0: name.hashCode();
|
||||
return Objects.hashCode(name);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
|
@ -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
|
||||
@ -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.
|
||||
@ -145,23 +147,20 @@ public class Proxy {
|
||||
* {@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();
|
||||
|
@ -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
|
||||
@ -32,6 +32,7 @@ import java.util.ArrayList;
|
||||
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
|
||||
@ -376,6 +377,7 @@ public final class URLPermission extends Permission {
|
||||
* 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;
|
||||
@ -389,22 +391,19 @@ public final class URLPermission extends Permission {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user