[jdk17] RFR: 8269096: Add java.util.Objects.newIdentity method [v5]
Tagir F.Valeev
tvaleev at openjdk.java.net
Tue Jun 29 05:50:01 UTC 2021
On Mon, 28 Jun 2021 19:45:34 GMT, Roger Riggs <rriggs at openjdk.org> wrote:
>> Add java.util.Objects.newIdentity to supply a unique object with identity.
>> This is a replacement code can be used today for the traditional new Object() idiom, which will be deprecated under Project Valhalla.
>> Refer to [JEP 401: Primitive Objects (Preview)](https://openjdk.java.net/jeps/401) for background.
>
> Roger Riggs has updated the pull request incrementally with one additional commit since the last revision:
>
> Add test synchronizing on return value of Objecst.newIdentity()
Probably it would be better to have an inner class named like `Identity` instead of anonymous class? When debugging or analyzing memory dumps, it would be more user-friendly to see `Objects$Identity` than `Objects$1`.
Probably, not the part of this feature request, but it would be nice to add another method with string parameter, like `Objects.newIdentity("MY SENTINEL")`. The string should be stored in the field and returned from toString(). Again, this would make it easier to find where the object comes from during debugging or memory dump analysis. For the record, here's what we have in IntelliJ IDEA sources (Apache 2.0 licensed):
public final class ObjectUtils {
private ObjectUtils() { }
...
/**
* Creates a new object which could be used as sentinel value (special value to distinguish from any other object). It does not equal
* to any other object. Usually should be assigned to the static final field.
*
* @param name an object name, returned from {@link #toString()} to simplify the debugging or heap dump analysis
* (guaranteed to be stored as sentinel object field). If sentinel is assigned to the static final field,
* it's recommended to supply that field name (possibly qualified with the class name).
* @return a new sentinel object
*/
public static @NotNull Object sentinel(@NotNull @NonNls String name) {
return new Sentinel(name);
}
private static final class Sentinel {
private final String myName;
Sentinel(@NotNull String name) {
myName = name;
}
@Override
public String toString() {
return myName;
}
}
-------------
PR: https://git.openjdk.java.net/jdk17/pull/112
More information about the core-libs-dev
mailing list