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.
|
||||
*
|
||||
* 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() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue