/* * Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.util; import jdk.internal.util.Preconditions; import java.util.function.Supplier; /** * This class consists of {@code static} utility methods for operating * on objects, or checking certain conditions before operation. These utilities * include {@code null}-safe or {@code null}-tolerant methods for computing the * hash code of an object, returning a string for an object, comparing two * objects, and checking if indexes or sub-range values are out of bounds. * * @since 1.7 */ public final class Objects { private Objects() { throw new AssertionError("No java.util.Objects instances for you!"); } /** * Returns {@code true} if the arguments are equal to each other * and {@code false} otherwise. * Consequently, if both arguments are {@code null}, {@code true} * is returned. Otherwise, if the first argument is not {@code * null}, equality is determined by calling the {@link * Object#equals equals} method of the first argument with the * second argument of this method. Otherwise, {@code false} is * returned. * * @param a an object * @param b an object to be compared with {@code a} for equality * @return {@code true} if the arguments are equal to each other * and {@code false} otherwise * @see Object#equals(Object) */ public static boolean equals(Object a, Object b) { return (a == b) || (a != null && a.equals(b)); } /** * Returns {@code true} if the arguments are deeply equal to each other * and {@code false} otherwise. * * Two {@code null} values are deeply equal. If both arguments are * arrays, the algorithm in {@link Arrays#deepEquals(Object[], * Object[]) Arrays.deepEquals} is used to determine equality. * Otherwise, equality is determined by using the {@link * Object#equals equals} method of the first argument. * * @param a an object * @param b an object to be compared with {@code a} for deep equality * @return {@code true} if the arguments are deeply equal to each other * and {@code false} otherwise * @see Arrays#deepEquals(Object[], Object[]) * @see Objects#equals(Object, Object) */ public static boolean deepEquals(Object a, Object b) { if (a == b) return true; else if (a == null || b == null) return false; else return Arrays.deepEquals0(a, b); } /** * Returns the hash code of a non-{@code null} argument and 0 for * a {@code null} argument. * * @param o an object * @return the hash code of a non-{@code null} argument and 0 for * a {@code null} argument * @see Object#hashCode */ public static int hashCode(Object o) { return o != null ? o.hashCode() : 0; } /** * Generates a hash code for a sequence of input values. The hash * code is generated as if all the input values were placed into an * array, and that array were hashed by calling {@link * Arrays#hashCode(Object[])}. * *
This method is useful for implementing {@link * Object#hashCode()} on objects containing multiple fields. For * example, if an object that has three fields, {@code x}, {@code * y}, and {@code z}, one could write: * *
* * Warning: When a single object reference is supplied, the returned * value does not equal the hash code of that object reference. This * value can be computed by calling {@link #hashCode(Object)}. * * @param values the values to be hashed * @return a hash value of the sequence of input values * @see Arrays#hashCode(Object[]) * @see List#hashCode */ public static int hash(Object... values) { return Arrays.hashCode(values); } /** * Returns the result of calling {@code toString} for a non-{@code * null} argument and {@code "null"} for a {@code null} argument. * * @param o an object * @return the result of calling {@code toString} for a non-{@code * null} argument and {@code "null"} for a {@code null} argument * @see Object#toString * @see String#valueOf(Object) */ public static String toString(Object o) { return String.valueOf(o); } /** * Returns the result of calling {@code toString} on the first * argument if the first argument is not {@code null} and returns * the second argument otherwise. * * @param o an object * @param nullDefault string to return if the first argument is * {@code null} * @return the result of calling {@code toString} on the first * argument if it is not {@code null} and the second argument * otherwise. * @see Objects#toString(Object) */ public static String toString(Object o, String nullDefault) { return (o != null) ? o.toString() : nullDefault; } /** * Returns 0 if the arguments are identical and {@code * c.compare(a, b)} otherwise. * Consequently, if both arguments are {@code null} 0 * is returned. * ** @Override public int hashCode() { * return Objects.hash(x, y, z); * } *
Note that if one of the arguments is {@code null}, a {@code
* NullPointerException} may or may not be thrown depending on
* what ordering policy, if any, the {@link Comparator Comparator}
* chooses to have for {@code null} values.
*
* @param Unlike the method {@link #requireNonNull(Object, String)},
* this method allows creation of the message to be deferred until
* after the null check is made. While this may confer a
* performance advantage in the non-null case, when deciding to
* call this method care should be taken that the costs of
* creating the message supplier are less than the cost of just
* creating the string message directly.
*
* @param obj the object reference to check for nullity
* @param messageSupplier supplier of the detail message to be
* used in the event that a {@code NullPointerException} is thrown
* @param The {@code index} is defined to be out of bounds if any of the
* following inequalities is true:
* The sub-range is defined to be out of bounds if any of the following
* inequalities is true:
* The sub-range is defined to be out of bounds if any of the following
* inequalities is true:
* The {@code index} is defined to be out of bounds if any of the
* following inequalities is true:
* The sub-range is defined to be out of bounds if any of the following
* inequalities is true:
* The sub-range is defined to be out of bounds if any of the following
* inequalities is true:
*
*
* @param obj the object reference to check for nullity
* @param
* public Foo(Bar bar) {
* this.bar = Objects.requireNonNull(bar);
* }
*
*
* @param obj the object reference to check for nullity
* @param message detail message to be used in the event that a {@code
* NullPointerException} is thrown
* @param
* public Foo(Bar bar, Baz baz) {
* this.bar = Objects.requireNonNull(bar, "bar must not be null");
* this.baz = Objects.requireNonNull(baz, "baz must not be null");
* }
*
*
*
* @param index the index
* @param length the upper-bound (exclusive) of the range
* @return {@code index} if it is within bounds of the range
* @throws IndexOutOfBoundsException if the {@code index} is out of bounds
* @since 9
*/
// Android-removed: @ForceInline is an unsupported attribute.
//@ForceInline
public static
int checkIndex(int index, int length) {
return Preconditions.checkIndex(index, length, null);
}
/**
* Checks if the sub-range from {@code fromIndex} (inclusive) to
* {@code toIndex} (exclusive) is within the bounds of range from {@code 0}
* (inclusive) to {@code length} (exclusive).
*
*
*
*
* @param fromIndex the lower-bound (inclusive) of the sub-range
* @param toIndex the upper-bound (exclusive) of the sub-range
* @param length the upper-bound (exclusive) the range
* @return {@code fromIndex} if the sub-range within bounds of the range
* @throws IndexOutOfBoundsException if the sub-range is out of bounds
* @since 9
*/
public static
int checkFromToIndex(int fromIndex, int toIndex, int length) {
return Preconditions.checkFromToIndex(fromIndex, toIndex, length, null);
}
/**
* Checks if the sub-range from {@code fromIndex} (inclusive) to
* {@code fromIndex + size} (exclusive) is within the bounds of range from
* {@code 0} (inclusive) to {@code length} (exclusive).
*
*
*
*
* @param fromIndex the lower-bound (inclusive) of the sub-interval
* @param size the size of the sub-range
* @param length the upper-bound (exclusive) of the range
* @return {@code fromIndex} if the sub-range within bounds of the range
* @throws IndexOutOfBoundsException if the sub-range is out of bounds
* @since 9
*/
public static
int checkFromIndexSize(int fromIndex, int size, int length) {
return Preconditions.checkFromIndexSize(fromIndex, size, length, null);
}
/**
* Checks if the {@code index} is within the bounds of the range from
* {@code 0} (inclusive) to {@code length} (exclusive).
*
*
*
*
* @param index the index
* @param length the upper-bound (exclusive) of the range
* @return {@code index} if it is within bounds of the range
* @throws IndexOutOfBoundsException if the {@code index} is out of bounds
* @since 16
*/
// Android-removed: @ForceInline is an unsupported attribute.
//@ForceInline
public static
long checkIndex(long index, long length) {
return Preconditions.checkIndex(index, length, null);
}
/**
* Checks if the sub-range from {@code fromIndex} (inclusive) to
* {@code toIndex} (exclusive) is within the bounds of range from {@code 0}
* (inclusive) to {@code length} (exclusive).
*
*
*
*
* @param fromIndex the lower-bound (inclusive) of the sub-range
* @param toIndex the upper-bound (exclusive) of the sub-range
* @param length the upper-bound (exclusive) the range
* @return {@code fromIndex} if the sub-range within bounds of the range
* @throws IndexOutOfBoundsException if the sub-range is out of bounds
* @since 16
*/
public static
long checkFromToIndex(long fromIndex, long toIndex, long length) {
return Preconditions.checkFromToIndex(fromIndex, toIndex, length, null);
}
/**
* Checks if the sub-range from {@code fromIndex} (inclusive) to
* {@code fromIndex + size} (exclusive) is within the bounds of range from
* {@code 0} (inclusive) to {@code length} (exclusive).
*
*
*
*
* @param fromIndex the lower-bound (inclusive) of the sub-interval
* @param size the size of the sub-range
* @param length the upper-bound (exclusive) of the range
* @return {@code fromIndex} if the sub-range within bounds of the range
* @throws IndexOutOfBoundsException if the sub-range is out of bounds
* @since 16
*/
public static
long checkFromIndexSize(long fromIndex, long size, long length) {
return Preconditions.checkFromIndexSize(fromIndex, size, length, null);
}
}