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;
}
/**