< prev index next > src/java.base/share/classes/java/util/Objects.java
Print this page
* questions.
*/
package java.util;
+ import jdk.internal.javac.PreviewFeature;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.ForceInline;
+ import jdk.internal.misc.Unsafe;
import java.util.function.Supplier;
/**
* This class consists of {@code static} utility methods for operating
public static String toIdentityString(Object o) {
requireNonNull(o);
return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
}
+ /**
+ * {@return {@code true} if the specified object reference is an identity object,
+ * otherwise {@code false}}
+ *
+ * @param obj an object
+ * @throws NullPointerException if {@code obj} is {@code null}
+ */
+ @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
+ // @IntrinsicCandidate
+ public static boolean hasIdentity(Object obj) {
+ requireNonNull(obj);
+ return obj.getClass().isIdentity() || // Before Valhalla all classes are identity classes
+ obj.getClass() == Object.class;
+ }
+
+ /**
+ * Checks that the specified object reference is an identity object.
+ *
+ * @param obj the object reference to check for identity
+ * @param <T> the type of the reference
+ * @return {@code obj} if {@code obj} is an identity object
+ * @throws NullPointerException if {@code obj} is {@code null}
+ * @throws IdentityException if {@code obj} is not an identity object
+ * @since Valhalla
+ */
+ @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
+ @ForceInline
+ public static <T> T requireIdentity(T obj) {
+ Objects.requireNonNull(obj);
+ if (!hasIdentity(obj))
+ throw new IdentityException(obj.getClass());
+ return obj;
+ }
+
+ /**
+ * Checks that the specified object reference is an identity object.
+ *
+ * @param obj the object reference to check for identity
+ * @param message detail message to be used in the event that an
+ * {@code IdentityException} is thrown; may be null
+ * @param <T> the type of the reference
+ * @return {@code obj} if {@code obj} is an identity object
+ * @throws NullPointerException if {@code obj} is {@code null}
+ * @throws IdentityException if {@code obj} is not an identity object
+ * @since Valhalla
+ */
+ @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
+ @ForceInline
+ public static <T> T requireIdentity(T obj, String message) {
+ Objects.requireNonNull(obj);
+ if (!hasIdentity(obj))
+ throw new IdentityException(message);
+ return obj;
+ }
+
+ /**
+ * Checks that the specified object reference is an identity object.
+ *
+ * @param obj the object reference to check for identity
+ * @param messageSupplier supplier of the detail message to be
+ * used in the event that an {@code IdentityException} is thrown; may be null
+ * @param <T> the type of the reference
+ * @return {@code obj} if {@code obj} is an identity object
+ * @throws NullPointerException if {@code obj} is {@code null}
+ * @throws IdentityException if {@code obj} is not an identity object
+ * @since Valhalla
+ */
+ @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
+ @ForceInline
+ public static <T> T requireIdentity(T obj, Supplier<String> messageSupplier) {
+ Objects.requireNonNull(obj);
+ if (!hasIdentity(obj))
+ throw new IdentityException(messageSupplier == null ?
+ null : messageSupplier.get());
+ return obj;
+ }
+
/**
* {@return 0 if the arguments are identical and {@code
* c.compare(a, b)} otherwise}
* Consequently, if both arguments are {@code null} 0
* is returned.
public static
int checkFromIndexSize(int fromIndex, int size, int length) {
return Preconditions.checkFromIndexSize(fromIndex, size, length, null);
}
+ /**
+ * Return the size of the object in the heap.
+ *
+ * @param o an object
+ * @return the objects's size
+ * @since Valhalla
+ */
+ public static long getObjectSize(Object o) {
+ return Unsafe.getUnsafe().getObjectSize(o);
+ }
+
/**
* Checks if the {@code index} is within the bounds of the range from
* {@code 0} (inclusive) to {@code length} (exclusive).
*
* <p>The {@code index} is defined to be out of bounds if any of the
< prev index next >