Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDK-8269172: Add java.util.Objects.newIdentity method #459

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -423,20 +423,6 @@ public static long getObjectSize(Object o) {
return Unsafe.getUnsafe().getObjectSize(o);
}

/**
* Returns a new Object implementing the {@code IdentityObject} interface.
* The object is a unique {@link IdentityObject} suitable for all purposes
* for which {@code new Object{}} was used including synchronization,
* mutexes and unique placeholders.
*
* @return a new Object implementing the IdentityObject interface
* @since Valhalla
*/
public static IdentityObject newIdentity() {
// Return a new instance of an anonymous inner class.
return new IdentityObject() { };
}

/**
* Checks if the {@code index} is within the bounds of the range from
* {@code 0} (inclusive) to {@code length} (exclusive).
@@ -512,4 +498,15 @@ long checkFromToIndex(long fromIndex, long toIndex, long length) {
long checkFromIndexSize(long fromIndex, long size, long length) {
return Preconditions.checkFromIndexSize(fromIndex, size, length, null);
}
/**
* {@return a new instance of an unspecified class}
* The object has a unique identity; no other references to it exist.
* It can be used for synchronization, or where a placeholder Object is needed.
* Use this method to avoid relying on the {@linkplain Object#Object() Object constructor}.
*
* @since 17
*/
public static Object newIdentity() {
return new Object() {};
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2021, 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,6 +45,7 @@ public static void main(String... args) {
errors += testIsNull();
errors += testNonNull();
errors += testNonNullOf();
errors += testNewIdentity();
if (errors > 0 )
throw new RuntimeException();
}
@@ -275,4 +276,19 @@ private static int testNonNullOf() {
}
return errors;
}

private static int testNewIdentity() {
int errors = 0;

Object o1 = Objects.newIdentity();
Object o2 = Objects.newIdentity();

if (o1 == null || o2 == null)
errors += 1;

if (o1 == o2)
errors += 1;

return errors;
}
}