< 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.

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











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

469      * <p>The sub-range is defined to be out of bounds if any of the following
470      * inequalities is true:
471      * <ul>
472      *  <li>{@code fromIndex < 0}</li>
473      *  <li>{@code size < 0}</li>
474      *  <li>{@code fromIndex + size > length}, taking into account integer overflow</li>
475      *  <li>{@code length < 0}, which is implied from the former inequalities</li>
476      * </ul>
477      *
478      * @param fromIndex the lower-bound (inclusive) of the sub-interval
479      * @param size the size of the sub-range
480      * @param length the upper-bound (exclusive) of the range
481      * @return {@code fromIndex} if the sub-range within bounds of the range
482      * @throws IndexOutOfBoundsException if the sub-range is out of bounds
483      * @since 16
484      */
485     public static
486     long checkFromIndexSize(long fromIndex, long size, long length) {
487         return Preconditions.checkFromIndexSize(fromIndex, size, length, null);
488     }











489 }

 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.

395      * inequalities is true:
396      * <ul>
397      *  <li>{@code fromIndex < 0}</li>
398      *  <li>{@code size < 0}</li>
399      *  <li>{@code fromIndex + size > length}, taking into account integer overflow</li>
400      *  <li>{@code length < 0}, which is implied from the former inequalities</li>
401      * </ul>
402      *
403      * @param fromIndex the lower-bound (inclusive) of the sub-interval
404      * @param size the size of the sub-range
405      * @param length the upper-bound (exclusive) of the range
406      * @return {@code fromIndex} if the sub-range within bounds of the range
407      * @throws IndexOutOfBoundsException if the sub-range is out of bounds
408      * @since 9
409      */
410     public static
411     int checkFromIndexSize(int fromIndex, int size, int length) {
412         return Preconditions.checkFromIndexSize(fromIndex, size, length, null);
413     }
414 
415     /**
416      * Return the size of the object in the heap.
417      *
418      * @param o an object
419      * @return the objects's size
420      * @since Valhalla
421      */
422     public static long getObjectSize(Object o) {
423         return Unsafe.getUnsafe().getObjectSize(o);
424     }
425 
426     /**
427      * Checks if the {@code index} is within the bounds of the range from
428      * {@code 0} (inclusive) to {@code length} (exclusive).
429      *
430      * <p>The {@code index} is defined to be out of bounds if any of the
431      * following inequalities is true:
432      * <ul>
433      *  <li>{@code index < 0}</li>
434      *  <li>{@code index >= length}</li>
435      *  <li>{@code length < 0}, which is implied from the former inequalities</li>
436      * </ul>
437      *
438      * @param index the index
439      * @param length the upper-bound (exclusive) of the range
440      * @return {@code index} if it is within bounds of the range
441      * @throws IndexOutOfBoundsException if the {@code index} is out of bounds
442      * @since 16
443      */
444     @ForceInline
445     public static

481      * <p>The sub-range is defined to be out of bounds if any of the following
482      * inequalities is true:
483      * <ul>
484      *  <li>{@code fromIndex < 0}</li>
485      *  <li>{@code size < 0}</li>
486      *  <li>{@code fromIndex + size > length}, taking into account integer overflow</li>
487      *  <li>{@code length < 0}, which is implied from the former inequalities</li>
488      * </ul>
489      *
490      * @param fromIndex the lower-bound (inclusive) of the sub-interval
491      * @param size the size of the sub-range
492      * @param length the upper-bound (exclusive) of the range
493      * @return {@code fromIndex} if the sub-range within bounds of the range
494      * @throws IndexOutOfBoundsException if the sub-range is out of bounds
495      * @since 16
496      */
497     public static
498     long checkFromIndexSize(long fromIndex, long size, long length) {
499         return Preconditions.checkFromIndexSize(fromIndex, size, length, null);
500     }
501     /**
502      * {@return a new instance of an unspecified class}
503      * The object has a unique identity; no other references to it exist.
504      * It can be used for synchronization, or where a placeholder Object is needed.
505      * Use this method to avoid relying on the {@linkplain Object#Object() Object constructor}.
506      *
507      * @since 17
508      */
509     public static Object newIdentity() {
510         return new Object() {};
511     }
512 }
< prev index next >