< prev index next >

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

Print this page

  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.util;
 27 

 28 import jdk.internal.util.Preconditions;
 29 import jdk.internal.vm.annotation.ForceInline;

 30 
 31 import java.util.function.Supplier;
 32 
 33 /**
 34  * This class consists of {@code static} utility methods for operating
 35  * on objects, or checking certain conditions before operation.  These utilities
 36  * include {@code null}-safe or {@code null}-tolerant methods for computing the
 37  * hash code of an object, returning a string for an object, comparing two
 38  * objects, and checking if indexes or sub-range values are out of bounds.
 39  *
 40  * @since 1.7
 41  */
 42 public final class Objects {
 43     private Objects() {
 44         throw new AssertionError("No java.util.Objects instances for you!");
 45     }
 46 
 47     /**
 48      * {@return {@code true} if the arguments are equal to each other
 49      * and {@code false} otherwise}

160      *
161      * @implNote
162      * This method constructs a string for an object without calling
163      * any overridable methods of the object.
164      *
165      * @implSpec
166      * The method returns a string equivalent to:<br>
167      * {@code o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o))}
168      *
169      * @param o an object
170      * @throws NullPointerException if the argument is null
171      * @see Object#toString
172      * @see System#identityHashCode(Object)
173      * @since 19
174      */
175     public static String toIdentityString(Object o) {
176         requireNonNull(o);
177         return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
178     }
179 



























































































180     /**
181      * {@return 0 if the arguments are identical and {@code
182      * c.compare(a, b)} otherwise}
183      * Consequently, if both arguments are {@code null} 0
184      * is returned.
185      *
186      * <p>Note that if one of the arguments is {@code null}, a {@code
187      * NullPointerException} may or may not be thrown depending on
188      * what ordering policy, if any, the {@link Comparator Comparator}
189      * chooses to have for {@code null} values.
190      *
191      * @param <T> the type of the objects being compared
192      * @param a an object
193      * @param b an object to be compared with {@code a}
194      * @param c the {@code Comparator} to compare the first two arguments
195      * @see Comparable
196      * @see Comparator
197      */
198     public static <T> int compare(T a, T b, Comparator<? super T> c) {
199         return (a == b) ? 0 :  c.compare(a, b);

400      * inequalities is true:
401      * <ul>
402      *  <li>{@code fromIndex < 0}</li>
403      *  <li>{@code size < 0}</li>
404      *  <li>{@code fromIndex + size > length}, taking into account integer overflow</li>
405      *  <li>{@code length < 0}, which is implied from the former inequalities</li>
406      * </ul>
407      *
408      * @param fromIndex the lower-bound (inclusive) of the sub-interval
409      * @param size the size of the sub-range
410      * @param length the upper-bound (exclusive) of the range
411      * @return {@code fromIndex} if the sub-range within bounds of the range
412      * @throws IndexOutOfBoundsException if the sub-range is out of bounds
413      * @since 9
414      */
415     public static
416     int checkFromIndexSize(int fromIndex, int size, int length) {
417         return Preconditions.checkFromIndexSize(fromIndex, size, length, null);
418     }
419 











420     /**
421      * Checks if the {@code index} is within the bounds of the range from
422      * {@code 0} (inclusive) to {@code length} (exclusive).
423      *
424      * <p>The {@code index} is defined to be out of bounds if any of the
425      * following inequalities is true:
426      * <ul>
427      *  <li>{@code index < 0}</li>
428      *  <li>{@code index >= length}</li>
429      *  <li>{@code length < 0}, which is implied from the former inequalities</li>
430      * </ul>
431      *
432      * @param index the index
433      * @param length the upper-bound (exclusive) of the range
434      * @return {@code index} if it is within bounds of the range
435      * @throws IndexOutOfBoundsException if the {@code index} is out of bounds
436      * @since 16
437      */
438     @ForceInline
439     public static

  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.util;
 27 
 28 import jdk.internal.javac.PreviewFeature;
 29 import jdk.internal.util.Preconditions;
 30 import jdk.internal.vm.annotation.ForceInline;
 31 import jdk.internal.misc.Unsafe;
 32 
 33 import java.util.function.Supplier;
 34 
 35 /**
 36  * This class consists of {@code static} utility methods for operating
 37  * on objects, or checking certain conditions before operation.  These utilities
 38  * include {@code null}-safe or {@code null}-tolerant methods for computing the
 39  * hash code of an object, returning a string for an object, comparing two
 40  * objects, and checking if indexes or sub-range values are out of bounds.
 41  *
 42  * @since 1.7
 43  */
 44 public final class Objects {
 45     private Objects() {
 46         throw new AssertionError("No java.util.Objects instances for you!");
 47     }
 48 
 49     /**
 50      * {@return {@code true} if the arguments are equal to each other
 51      * and {@code false} otherwise}

162      *
163      * @implNote
164      * This method constructs a string for an object without calling
165      * any overridable methods of the object.
166      *
167      * @implSpec
168      * The method returns a string equivalent to:<br>
169      * {@code o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o))}
170      *
171      * @param o an object
172      * @throws NullPointerException if the argument is null
173      * @see Object#toString
174      * @see System#identityHashCode(Object)
175      * @since 19
176      */
177     public static String toIdentityString(Object o) {
178         requireNonNull(o);
179         return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
180     }
181 
182    /**
183     * {@return {@code true} if the specified object reference is an identity object,
184     * otherwise {@code false}}
185     *
186     * @param obj an object
187     * @throws NullPointerException if {@code obj} is {@code null}
188     */
189    @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
190 //    @IntrinsicCandidate
191     public static boolean isIdentityObject(Object obj) {
192         requireNonNull(obj);
193         return obj.getClass().isIdentity() ||  // Before Valhalla all classes are identity classes
194                 obj.getClass() == Object.class;
195     }
196 
197     /**
198      * Checks that the specified object reference is an identity object.
199      *
200      * @param obj the object reference to check for identity
201      * @param <T> the type of the reference
202      * @return {@code obj} if {@code obj} is an identity object
203      * @throws NullPointerException if {@code obj} is {@code null}
204      * @throws IdentityException if {@code obj} is not an identity object
205      * @since Valhalla
206      */
207     @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
208     @ForceInline
209     public static <T> T requireIdentity(T obj) {
210         Objects.requireNonNull(obj);
211         if (!isIdentityObject(obj))
212             throw new IdentityException(obj.getClass());
213         return obj;
214     }
215 
216     /**
217      * Checks that the specified object reference is an identity object.
218      *
219      * @param obj the object reference to check for identity
220      * @param message detail message to be used in the event that an
221      *        {@code IdentityException} is thrown; may be null
222      * @param <T> the type of the reference
223      * @return {@code obj} if {@code obj} is an identity object
224      * @throws NullPointerException if {@code obj} is {@code null}
225      * @throws IdentityException if {@code obj} is not an identity object
226      * @since Valhalla
227      */
228     @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
229     @ForceInline
230     public static <T> T requireIdentity(T obj, String message) {
231         Objects.requireNonNull(obj);
232         if (!isIdentityObject(obj))
233             throw new IdentityException(message);
234         return obj;
235     }
236 
237     /**
238      * Checks that the specified object reference is an identity object.
239      *
240      * @param obj the object reference to check for identity
241      * @param messageSupplier supplier of the detail message to be
242      *        used in the event that an {@code IdentityException} is thrown; may be null
243      * @param <T> the type of the reference
244      * @return {@code obj} if {@code obj} is an identity object
245      * @throws NullPointerException if {@code obj} is {@code null}
246      * @throws IdentityException if {@code obj} is not an identity object
247      * @since Valhalla
248      */
249     @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
250     @ForceInline
251     public static <T> T requireIdentity(T obj, Supplier<String> messageSupplier) {
252         Objects.requireNonNull(obj);
253         if (!isIdentityObject(obj))
254             throw new IdentityException(messageSupplier == null ?
255                     null : messageSupplier.get());
256         return obj;
257     }
258 
259    /**
260     * {@return {@code true} if the specified object is a {@linkplain Class#isValue value object},
261     * otherwise {@code false}}
262     *
263     * @param obj an object
264     * @throws NullPointerException if {@code obj} is {@code null}
265     */
266    @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
267 //    @IntrinsicCandidate
268     public static boolean isValueObject(Object obj) {
269         requireNonNull(obj, "obj");
270         return obj.getClass().isValue();
271     }
272 
273     /**
274      * {@return 0 if the arguments are identical and {@code
275      * c.compare(a, b)} otherwise}
276      * Consequently, if both arguments are {@code null} 0
277      * is returned.
278      *
279      * <p>Note that if one of the arguments is {@code null}, a {@code
280      * NullPointerException} may or may not be thrown depending on
281      * what ordering policy, if any, the {@link Comparator Comparator}
282      * chooses to have for {@code null} values.
283      *
284      * @param <T> the type of the objects being compared
285      * @param a an object
286      * @param b an object to be compared with {@code a}
287      * @param c the {@code Comparator} to compare the first two arguments
288      * @see Comparable
289      * @see Comparator
290      */
291     public static <T> int compare(T a, T b, Comparator<? super T> c) {
292         return (a == b) ? 0 :  c.compare(a, b);

493      * inequalities is true:
494      * <ul>
495      *  <li>{@code fromIndex < 0}</li>
496      *  <li>{@code size < 0}</li>
497      *  <li>{@code fromIndex + size > length}, taking into account integer overflow</li>
498      *  <li>{@code length < 0}, which is implied from the former inequalities</li>
499      * </ul>
500      *
501      * @param fromIndex the lower-bound (inclusive) of the sub-interval
502      * @param size the size of the sub-range
503      * @param length the upper-bound (exclusive) of the range
504      * @return {@code fromIndex} if the sub-range within bounds of the range
505      * @throws IndexOutOfBoundsException if the sub-range is out of bounds
506      * @since 9
507      */
508     public static
509     int checkFromIndexSize(int fromIndex, int size, int length) {
510         return Preconditions.checkFromIndexSize(fromIndex, size, length, null);
511     }
512 
513     /**
514      * Return the size of the object in the heap.
515      *
516      * @param o an object
517      * @return the objects's size
518      * @since Valhalla
519      */
520     public static long getObjectSize(Object o) {
521         return Unsafe.getUnsafe().getObjectSize(o);
522     }
523 
524     /**
525      * Checks if the {@code index} is within the bounds of the range from
526      * {@code 0} (inclusive) to {@code length} (exclusive).
527      *
528      * <p>The {@code index} is defined to be out of bounds if any of the
529      * following inequalities is true:
530      * <ul>
531      *  <li>{@code index < 0}</li>
532      *  <li>{@code index >= length}</li>
533      *  <li>{@code length < 0}, which is implied from the former inequalities</li>
534      * </ul>
535      *
536      * @param index the index
537      * @param length the upper-bound (exclusive) of the range
538      * @return {@code index} if it is within bounds of the range
539      * @throws IndexOutOfBoundsException if the {@code index} is out of bounds
540      * @since 16
541      */
542     @ForceInline
543     public static
< prev index next >