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

@ -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;