< 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 hasIdentity(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 (!hasIdentity(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 (!hasIdentity(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 (!hasIdentity(obj))
254             throw new IdentityException(messageSupplier == null ?
255                     null : messageSupplier.get());
256         return obj;
257     }
258 
259     /**
260      * {@return 0 if the arguments are identical and {@code
261      * c.compare(a, b)} otherwise}
262      * Consequently, if both arguments are {@code null} 0
263      * is returned.
264      *
265      * <p>Note that if one of the arguments is {@code null}, a {@code
266      * NullPointerException} may or may not be thrown depending on
267      * what ordering policy, if any, the {@link Comparator Comparator}
268      * chooses to have for {@code null} values.
269      *
270      * @param <T> the type of the objects being compared
271      * @param a an object
272      * @param b an object to be compared with {@code a}
273      * @param c the {@code Comparator} to compare the first two arguments
274      * @see Comparable
275      * @see Comparator
276      */
277     public static <T> int compare(T a, T b, Comparator<? super T> c) {
278         return (a == b) ? 0 :  c.compare(a, b);

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