< prev index next >

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

Print this page

 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      * Returns {@code true} if the arguments are equal to each other
 49      * and {@code false} otherwise.

171      *
172      * @implNote
173      * This method constructs a string for an object without calling
174      * any overridable methods of the object.
175      *
176      * @implSpec
177      * The method returns a string equivalent to:<br>
178      * {@code o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o))}
179      *
180      * @param o an object
181      * @throws NullPointerException if the argument is null
182      * @see Object#toString
183      * @see System#identityHashCode(Object)
184      * @since 19
185      */
186     public static String toIdentityString(Object o) {
187         requireNonNull(o);
188         return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
189     }
190 




























































191     /**
192      * Returns 0 if the arguments are identical and {@code
193      * c.compare(a, b)} otherwise.
194      * Consequently, if both arguments are {@code null} 0
195      * is returned.
196      *
197      * <p>Note that if one of the arguments is {@code null}, a {@code
198      * NullPointerException} may or may not be thrown depending on
199      * what ordering policy, if any, the {@link Comparator Comparator}
200      * chooses to have for {@code null} values.
201      *
202      * @param <T> the type of the objects being compared
203      * @param a an object
204      * @param b an object to be compared with {@code a}
205      * @param c the {@code Comparator} to compare the first two arguments
206      * @return 0 if the arguments are identical and {@code
207      * c.compare(a, b)} otherwise.
208      * @see Comparable
209      * @see Comparator
210      */

420      * inequalities is true:
421      * <ul>
422      *  <li>{@code fromIndex < 0}</li>
423      *  <li>{@code size < 0}</li>
424      *  <li>{@code fromIndex + size > length}, taking into account integer overflow</li>
425      *  <li>{@code length < 0}, which is implied from the former inequalities</li>
426      * </ul>
427      *
428      * @param fromIndex the lower-bound (inclusive) of the sub-interval
429      * @param size the size of the sub-range
430      * @param length the upper-bound (exclusive) of the range
431      * @return {@code fromIndex} if the sub-range within bounds of the range
432      * @throws IndexOutOfBoundsException if the sub-range is out of bounds
433      * @since 9
434      */
435     public static
436     int checkFromIndexSize(int fromIndex, int size, int length) {
437         return Preconditions.checkFromIndexSize(fromIndex, size, length, null);
438     }
439 











440     /**
441      * Checks if the {@code index} is within the bounds of the range from
442      * {@code 0} (inclusive) to {@code length} (exclusive).
443      *
444      * <p>The {@code index} is defined to be out of bounds if any of the
445      * following inequalities is true:
446      * <ul>
447      *  <li>{@code index < 0}</li>
448      *  <li>{@code index >= length}</li>
449      *  <li>{@code length < 0}, which is implied from the former inequalities</li>
450      * </ul>
451      *
452      * @param index the index
453      * @param length the upper-bound (exclusive) of the range
454      * @return {@code index} if it is within bounds of the range
455      * @throws IndexOutOfBoundsException if the {@code index} is out of bounds
456      * @since 16
457      */
458     @ForceInline
459     public static

 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 import jdk.internal.misc.Unsafe;
 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     /**
 49      * Returns {@code true} if the arguments are equal to each other
 50      * and {@code false} otherwise.

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

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