< prev index next >

src/java.base/share/classes/java/util/Objects.java

Print this page
*** 23,12 ***
--- 23,14 ---
   * questions.
   */
  
  package java.util;
  
+ import jdk.internal.misc.ValhallaFeatures;
  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

*** 186,10 ***
--- 188,96 ---
      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}
+     */
+ //    @IntrinsicCandidate
+     public static boolean isIdentityObject(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
+      */
+     @ForceInline
+     public static <T> T requireIdentity(T obj) {
+         Objects.requireNonNull(obj);
+         if (!isIdentityObject(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
+      */
+     @ForceInline
+     public static <T> T requireIdentity(T obj, String message) {
+         Objects.requireNonNull(obj);
+         if (!isIdentityObject(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
+      */
+     @ForceInline
+     public static <T> T requireIdentity(T obj, Supplier<String> messageSupplier) {
+         Objects.requireNonNull(obj);
+         if (!isIdentityObject(obj))
+             throw new IdentityException(messageSupplier == null ?
+                     null : messageSupplier.get());
+         return obj;
+     }
+ 
+    /**
+     * {@return {@code true} if the specified object is a {@linkplain Class#isValue value object},
+     * otherwise {@code false}}
+     *
+     * @param obj an object
+     * @throws NullPointerException if {@code obj} is {@code null}
+     */
+ //    @IntrinsicCandidate
+     public static boolean isValueObject(Object obj) {
+         requireNonNull(obj, "obj");
+         return obj.getClass().isValue();
+     }
+ 
      /**
       * Returns 0 if the arguments are identical and {@code
       * c.compare(a, b)} otherwise.
       * Consequently, if both arguments are {@code null} 0
       * is returned.

*** 435,10 ***
--- 523,21 ---
      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 >