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.
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.
421 * inequalities is true:
422 * <ul>
423 * <li>{@code fromIndex < 0}</li>
424 * <li>{@code size < 0}</li>
425 * <li>{@code fromIndex + size > length}, taking into account integer overflow</li>
426 * <li>{@code length < 0}, which is implied from the former inequalities</li>
427 * </ul>
428 *
429 * @param fromIndex the lower-bound (inclusive) of the sub-interval
430 * @param size the size of the sub-range
431 * @param length the upper-bound (exclusive) of the range
432 * @return {@code fromIndex} if the sub-range within bounds of the range
433 * @throws IndexOutOfBoundsException if the sub-range is out of bounds
434 * @since 9
435 */
436 public static
437 int checkFromIndexSize(int fromIndex, int size, int length) {
438 return Preconditions.checkFromIndexSize(fromIndex, size, length, null);
439 }
440
441 /**
442 * Return the size of the object in the heap.
443 *
444 * @param o an object
445 * @return the objects's size
446 * @since Valhalla
447 */
448 public static long getObjectSize(Object o) {
449 return Unsafe.getUnsafe().getObjectSize(o);
450 }
451
452 /**
453 * Checks if the {@code index} is within the bounds of the range from
454 * {@code 0} (inclusive) to {@code length} (exclusive).
455 *
456 * <p>The {@code index} is defined to be out of bounds if any of the
457 * following inequalities is true:
458 * <ul>
459 * <li>{@code index < 0}</li>
460 * <li>{@code index >= length}</li>
461 * <li>{@code length < 0}, which is implied from the former inequalities</li>
462 * </ul>
463 *
464 * @param index the index
465 * @param length the upper-bound (exclusive) of the range
466 * @return {@code index} if it is within bounds of the range
467 * @throws IndexOutOfBoundsException if the {@code index} is out of bounds
468 * @since 16
469 */
470 @ForceInline
471 public static
|