< prev index next >

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

Print this page

  1 /*
  2  * Copyright (c) 2009, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  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     /**

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);

  1 /*
  2  * Copyright (c) 2009, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  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 
 32 import java.util.function.Supplier;
 33 
 34 /**
 35  * This class consists of {@code static} utility methods for operating
 36  * on objects, or checking certain conditions before operation.  These utilities
 37  * include {@code null}-safe or {@code null}-tolerant methods for computing the
 38  * hash code of an object, returning a string for an object, comparing two
 39  * objects, and checking if indexes or sub-range values are out of bounds.
 40  *
 41  * @since 1.7
 42  */
 43 public final class Objects {
 44     private Objects() {
 45         throw new AssertionError("No java.util.Objects instances for you!");
 46     }
 47 
 48     /**

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