8312089: Simplify and modernize equals, hashCode, and compareTo in java.nio and implementation code

Reviewed-by: alanb, vtewari
This commit is contained in:
Pavel Rappo 2023-07-17 22:27:48 +00:00
parent 6a09992dbd
commit 5cc71f817f
14 changed files with 62 additions and 123 deletions

View File

@ -954,15 +954,15 @@ public abstract class Charset
* @return A negative integer, zero, or a positive integer as this charset
* is less than, equal to, or greater than the specified charset
*/
@Override
public final int compareTo(Charset that) {
return (name().compareToIgnoreCase(that.name()));
}
/**
* Computes a hashcode for this charset.
*
* @return An integer hashcode
* {@return the hashcode for this charset}
*/
@Override
public final int hashCode() {
return name().hashCode();
}
@ -976,19 +976,17 @@ public abstract class Charset
* @return {@code true} if, and only if, this charset is equal to the
* given object
*/
@Override
public final boolean equals(Object ob) {
if (!(ob instanceof Charset))
return false;
if (this == ob)
return true;
return name.equals(((Charset)ob).name());
return ob instanceof Charset other && name.equals(other.name());
}
/**
* Returns a string describing this charset.
*
* @return A string describing this charset
* {@return a string describing this charset}
*/
@Override
public final String toString() {
return name();
}

View File

@ -984,6 +984,7 @@ public interface Path
* @return {@code true} if, and only if, the given object is a {@code Path}
* that is identical to this {@code Path}
*/
@Override
boolean equals(Object other);
/**
@ -995,10 +996,11 @@ public interface Path
*
* @return the hash-code value for this path
*/
@Override
int hashCode();
/**
* Returns the string representation of this path.
* {@return the string representation of this path}
*
* <p> If this path was created by converting a path string using the
* {@link FileSystem#getPath getPath} method then the path string returned
@ -1006,8 +1008,7 @@ public interface Path
*
* <p> The returned path string uses the default name {@link
* FileSystem#getSeparator separator} to separate names in the path.
*
* @return the string representation of this path
*/
@Override
String toString();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 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
@ -357,10 +357,6 @@ public final class AclEntry {
return true;
}
private static int hash(int h, Object o) {
return h * 127 + o.hashCode();
}
/**
* Returns the hash-code value for this ACL entry.
*
@ -370,14 +366,12 @@ public final class AclEntry {
@Override
public int hashCode() {
// return cached hash if available
if (hash != 0)
return hash;
int h = type.hashCode();
h = hash(h, who);
h = hash(h, perms);
h = hash(h, flags);
hash = h;
return hash;
int h = hash;
if (h == 0) {
h = Objects.hash(type, who, perms, flags);
hash = h;
}
return h;
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 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
@ -291,7 +291,7 @@ public final class FileTime
*/
@Override
public boolean equals(Object obj) {
return (obj instanceof FileTime) ? compareTo((FileTime)obj) == 0 : false;
return obj instanceof FileTime other && compareTo(other) == 0;
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 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
@ -44,22 +44,19 @@ public class FileKey {
return fk;
}
@Override
public int hashCode() {
return (int)(st_dev ^ (st_dev >>> 32)) +
(int)(st_ino ^ (st_ino >>> 32));
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof FileKey))
return false;
FileKey other = (FileKey)obj;
if ((this.st_dev != other.st_dev) ||
(this.st_ino != other.st_ino)) {
return false;
}
return true;
return obj instanceof FileKey other
&& (this.st_dev == other.st_dev)
&& (this.st_ino == other.st_ino);
}
private native void init(FileDescriptor fd) throws IOException;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 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
@ -48,10 +48,9 @@ class UnixFileKey {
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof UnixFileKey))
return false;
UnixFileKey other = (UnixFileKey)obj;
return (this.st_dev == other.st_dev) && (this.st_ino == other.st_ino);
return obj instanceof UnixFileKey other
&& (this.st_dev == other.st_dev)
&& (this.st_ino == other.st_ino);
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 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
@ -234,9 +234,8 @@ abstract class UnixFileStore
public boolean equals(Object ob) {
if (ob == this)
return true;
if (!(ob instanceof UnixFileStore))
if (!(ob instanceof UnixFileStore other))
return false;
UnixFileStore other = (UnixFileStore)ob;
return (this.dev == other.dev) &&
Arrays.equals(this.entry.dir(), other.entry.dir()) &&
this.entry.name().equals(other.entry.name());

View File

@ -37,10 +37,12 @@ import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.spi.FileSystemProvider;
import java.util.Arrays;
import java.util.Objects;
import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.util.ArraysSupport;
import static sun.nio.fs.UnixConstants.*;
import static sun.nio.fs.UnixNativeDispatcher.*;
@ -706,43 +708,17 @@ class UnixPath implements Path {
// compare bytes
int thisPos = offsets[thisOffsetCount - thatOffsetCount];
int thatPos = that.offsets[0];
if ((thatLen - thatPos) != (thisLen - thisPos))
return false;
while (thatPos < thatLen) {
if (this.path[thisPos++] != that.path[thatPos++])
return false;
}
return true;
return Arrays.equals(this.path, thisPos, thisLen, that.path, thatPos, thatLen);
}
@Override
public int compareTo(Path other) {
int len1 = path.length;
int len2 = ((UnixPath) other).path.length;
int n = Math.min(len1, len2);
byte v1[] = path;
byte v2[] = ((UnixPath) other).path;
int k = 0;
while (k < n) {
int c1 = v1[k] & 0xff;
int c2 = v2[k] & 0xff;
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
return Arrays.compareUnsigned(path, ((UnixPath) other).path);
}
@Override
public boolean equals(Object ob) {
if (ob instanceof UnixPath path) {
return compareTo(path) == 0;
}
return false;
return ob instanceof UnixPath p && compareTo(p) == 0;
}
@Override
@ -750,9 +726,8 @@ class UnixPath implements Path {
// OK if two or more threads compute hash
int h = hash;
if (h == 0) {
for (int i = 0; i< path.length; i++) {
h = 31*h + (path[i] & 0xff);
}
h = ArraysSupport.vectorizedHashCode(path, 0, path.length, 0,
/* unsigned bytes */ ArraysSupport.T_BOOLEAN);
hash = h;
}
return h;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 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
@ -81,9 +81,8 @@ public class UnixUserPrincipals {
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof User))
if (!(obj instanceof User other))
return false;
User other = (User)obj;
if ((this.id != other.id) ||
(this.isGroup != other.isGroup)) {
return false;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 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
@ -45,24 +45,21 @@ public class FileKey {
return fk;
}
@Override
public int hashCode() {
return (int)(dwVolumeSerialNumber ^ (dwVolumeSerialNumber >>> 32)) +
(int)(nFileIndexHigh ^ (nFileIndexHigh >>> 32)) +
(int)(nFileIndexLow ^ (nFileIndexHigh >>> 32));
(int)(nFileIndexLow ^ (nFileIndexLow >>> 32));
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof FileKey))
return false;
FileKey other = (FileKey)obj;
if ((this.dwVolumeSerialNumber != other.dwVolumeSerialNumber) ||
(this.nFileIndexHigh != other.nFileIndexHigh) ||
(this.nFileIndexLow != other.nFileIndexLow)) {
return false;
}
return true;
return obj instanceof FileKey other
&& this.dwVolumeSerialNumber == other.dwVolumeSerialNumber
&& this.nFileIndexHigh == other.nFileIndexHigh
&& this.nFileIndexLow == other.nFileIndexLow;
}
private native void init(FileDescriptor fd) throws IOException;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 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
@ -812,10 +812,7 @@ class WindowsPath implements Path {
@Override
public boolean equals(Object obj) {
if (obj instanceof WindowsPath path) {
return compareTo(path) == 0;
}
return false;
return obj instanceof WindowsPath other && compareTo(other) == 0;
}
@Override
@ -823,7 +820,7 @@ class WindowsPath implements Path {
// OK if two or more threads compute hash
int h = hash;
if (h == 0) {
for (int i = 0; i< path.length(); i++) {
for (int i = 0; i < path.length(); i++) {
h = 31*h + Character.toUpperCase(path.charAt(i));
}
hash = h;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 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
@ -80,10 +80,8 @@ class WindowsUserPrincipals {
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof WindowsUserPrincipals.User))
return false;
WindowsUserPrincipals.User other = (WindowsUserPrincipals.User)obj;
return this.sidString.equals(other.sidString);
return obj instanceof WindowsUserPrincipals.User other
&& this.sidString.equals(other.sidString);
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 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
@ -231,12 +231,10 @@ class WindowsWatchService
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof FileKey))
return false;
FileKey other = (FileKey)obj;
if (this.volSerialNumber != other.volSerialNumber) return false;
if (this.fileIndexHigh != other.fileIndexHigh) return false;
return this.fileIndexLow == other.fileIndexLow;
return obj instanceof FileKey other
&& this.volSerialNumber == other.volSerialNumber
&& this.fileIndexHigh == other.fileIndexHigh
&& this.fileIndexLow == other.fileIndexLow;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 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
@ -636,20 +636,7 @@ final class ZipPath implements Path {
@Override
public int compareTo(Path other) {
final ZipPath o = checkPath(other);
int len1 = this.path.length;
int len2 = o.path.length;
int n = Math.min(len1, len2);
int k = 0;
while (k < n) {
int c1 = this.path[k] & 0xff;
int c2 = o.path[k] & 0xff;
if (c1 != c2)
return c1 - c2;
k++;
}
return len1 - len2;
return Arrays.compareUnsigned(this.path, o.path);
}
public WatchKey register(