mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8311162: Simplify and modernize equals and hashCode for java.net
Reviewed-by: dfuchs, michaelm, msheppar
This commit is contained in:
parent
430d6b61c5
commit
e3a7e020d2
3 changed files with 23 additions and 31 deletions
|
@ -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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* 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.Arrays;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Spliterator;
|
import java.util.Spliterator;
|
||||||
import java.util.Spliterators;
|
import java.util.Spliterators;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
@ -586,18 +587,13 @@ public final class NetworkInterface {
|
||||||
* {@code false} otherwise.
|
* {@code false} otherwise.
|
||||||
* @see java.net.InetAddress#getAddress()
|
* @see java.net.InetAddress#getAddress()
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (!(obj instanceof NetworkInterface that)) {
|
if (!(obj instanceof NetworkInterface that)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (this.name != null ) {
|
if (!Objects.equals(this.name, that.name)) {
|
||||||
if (!this.name.equals(that.name)) {
|
return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (that.name != null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.addrs == null) {
|
if (this.addrs == null) {
|
||||||
|
@ -612,13 +608,10 @@ public final class NetworkInterface {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
InetAddress[] thatAddrs = that.addrs;
|
for (InetAddress thisAddr : this.addrs) {
|
||||||
int count = thatAddrs.length;
|
|
||||||
|
|
||||||
for (int i=0; i<count; i++) {
|
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
for (int j=0; j<count; j++) {
|
for (InetAddress thatAddr : that.addrs) {
|
||||||
if (addrs[i].equals(thatAddrs[j])) {
|
if (thisAddr.equals(thatAddr)) {
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -630,8 +623,9 @@ public final class NetworkInterface {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return name == null? 0: name.hashCode();
|
return Objects.hashCode(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -25,6 +25,8 @@
|
||||||
|
|
||||||
package java.net;
|
package java.net;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class represents a proxy setting, typically a type (http, socks) and
|
* This class represents a proxy setting, typically a type (http, socks) and
|
||||||
* a socket address.
|
* a socket address.
|
||||||
|
@ -145,23 +147,20 @@ public class Proxy {
|
||||||
* {@code false} otherwise.
|
* {@code false} otherwise.
|
||||||
* @see java.net.InetSocketAddress#equals(java.lang.Object)
|
* @see java.net.InetSocketAddress#equals(java.lang.Object)
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public final boolean equals(Object obj) {
|
public final boolean equals(Object obj) {
|
||||||
if (!(obj instanceof Proxy p))
|
if (!(obj instanceof Proxy p))
|
||||||
return false;
|
return false;
|
||||||
if (p.type() == type()) {
|
if (p.type() == type()) {
|
||||||
if (address() == null) {
|
return Objects.equals(address(), p.address());
|
||||||
return (p.address() == null);
|
|
||||||
} else
|
|
||||||
return address().equals(p.address());
|
|
||||||
}
|
}
|
||||||
return false;
|
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() {
|
public final int hashCode() {
|
||||||
if (address() == null)
|
if (address() == null)
|
||||||
return type().hashCode();
|
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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* 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.util.Collections;
|
||||||
import java.security.Permission;
|
import java.security.Permission;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents permission to access a resource or set of resources defined by a
|
* 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())
|
* Returns true if, this.getActions().equals(p.getActions())
|
||||||
* and p's url equals this's url. Returns false otherwise.
|
* and p's url equals this's url. Returns false otherwise.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public boolean equals(Object p) {
|
public boolean equals(Object p) {
|
||||||
if (!(p instanceof URLPermission that)) {
|
if (!(p instanceof URLPermission that)) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -389,22 +391,19 @@ public final class URLPermission extends Permission {
|
||||||
if (!this.authority.equals(that.authority)) {
|
if (!this.authority.equals(that.authority)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (this.path != null) {
|
return Objects.equals(this.path, that.path);
|
||||||
return this.path.equals(that.path);
|
|
||||||
} else {
|
|
||||||
return that.path == null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a hashcode calculated from the hashcode of the
|
* Returns a hashcode calculated from the hashcode of the
|
||||||
* actions String and the url string.
|
* actions String and the url string.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return getActions().hashCode()
|
return getActions().hashCode()
|
||||||
+ scheme.hashCode()
|
+ scheme.hashCode()
|
||||||
+ authority.hashCode()
|
+ authority.hashCode()
|
||||||
+ (path == null ? 0 : path.hashCode());
|
+ Objects.hashCode(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue