jaroslav@682: /* jaroslav@682: * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. jaroslav@682: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@682: * jaroslav@682: * This code is free software; you can redistribute it and/or modify it jaroslav@682: * under the terms of the GNU General Public License version 2 only, as jaroslav@682: * published by the Free Software Foundation. Oracle designates this jaroslav@682: * particular file as subject to the "Classpath" exception as provided jaroslav@682: * by Oracle in the LICENSE file that accompanied this code. jaroslav@682: * jaroslav@682: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@682: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@682: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@682: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@682: * accompanied this code). jaroslav@682: * jaroslav@682: * You should have received a copy of the GNU General Public License version jaroslav@682: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@682: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@682: * jaroslav@682: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@682: * or visit www.oracle.com if you need additional information or have any jaroslav@682: * questions. jaroslav@682: */ jaroslav@682: jaroslav@682: package java.util; jaroslav@682: jaroslav@682: /** jaroslav@682: * This class consists of {@code static} utility methods for operating jaroslav@682: * on objects. These utilities include {@code null}-safe or {@code jaroslav@682: * null}-tolerant methods for computing the hash code of an object, jaroslav@682: * returning a string for an object, and comparing two objects. jaroslav@682: * jaroslav@682: * @since 1.7 jaroslav@682: */ jaroslav@682: public final class Objects { jaroslav@682: private Objects() { jaroslav@682: throw new AssertionError("No java.util.Objects instances for you!"); jaroslav@682: } jaroslav@682: jaroslav@682: /** jaroslav@682: * Returns {@code true} if the arguments are equal to each other jaroslav@682: * and {@code false} otherwise. jaroslav@682: * Consequently, if both arguments are {@code null}, {@code true} jaroslav@682: * is returned and if exactly one argument is {@code null}, {@code jaroslav@682: * false} is returned. Otherwise, equality is determined by using jaroslav@682: * the {@link Object#equals equals} method of the first jaroslav@682: * argument. jaroslav@682: * jaroslav@682: * @param a an object jaroslav@682: * @param b an object to be compared with {@code a} for equality jaroslav@682: * @return {@code true} if the arguments are equal to each other jaroslav@682: * and {@code false} otherwise jaroslav@682: * @see Object#equals(Object) jaroslav@682: */ jaroslav@682: public static boolean equals(Object a, Object b) { jaroslav@682: return (a == b) || (a != null && a.equals(b)); jaroslav@682: } jaroslav@682: jaroslav@682: /** jaroslav@682: * Returns {@code true} if the arguments are deeply equal to each other jaroslav@682: * and {@code false} otherwise. jaroslav@682: * jaroslav@682: * Two {@code null} values are deeply equal. If both arguments are jaroslav@682: * arrays, the algorithm in {@link Arrays#deepEquals(Object[], jaroslav@682: * Object[]) Arrays.deepEquals} is used to determine equality. jaroslav@682: * Otherwise, equality is determined by using the {@link jaroslav@682: * Object#equals equals} method of the first argument. jaroslav@682: * jaroslav@682: * @param a an object jaroslav@682: * @param b an object to be compared with {@code a} for deep equality jaroslav@682: * @return {@code true} if the arguments are deeply equal to each other jaroslav@682: * and {@code false} otherwise jaroslav@682: * @see Arrays#deepEquals(Object[], Object[]) jaroslav@682: * @see Objects#equals(Object, Object) jaroslav@682: */ jaroslav@682: public static boolean deepEquals(Object a, Object b) { jaroslav@682: if (a == b) jaroslav@682: return true; jaroslav@682: else if (a == null || b == null) jaroslav@682: return false; jaroslav@682: else jaroslav@682: return Arrays.deepEquals0(a, b); jaroslav@682: } jaroslav@682: jaroslav@682: /** jaroslav@682: * Returns the hash code of a non-{@code null} argument and 0 for jaroslav@682: * a {@code null} argument. jaroslav@682: * jaroslav@682: * @param o an object jaroslav@682: * @return the hash code of a non-{@code null} argument and 0 for jaroslav@682: * a {@code null} argument jaroslav@682: * @see Object#hashCode jaroslav@682: */ jaroslav@682: public static int hashCode(Object o) { jaroslav@682: return o != null ? o.hashCode() : 0; jaroslav@682: } jaroslav@682: jaroslav@682: /** jaroslav@682: * Generates a hash code for a sequence of input values. The hash jaroslav@682: * code is generated as if all the input values were placed into an jaroslav@682: * array, and that array were hashed by calling {@link jaroslav@682: * Arrays#hashCode(Object[])}. jaroslav@682: * jaroslav@682: *

This method is useful for implementing {@link jaroslav@682: * Object#hashCode()} on objects containing multiple fields. For jaroslav@682: * example, if an object that has three fields, {@code x}, {@code jaroslav@682: * y}, and {@code z}, one could write: jaroslav@682: * jaroslav@682: *

jaroslav@682:     * @Override public int hashCode() {
jaroslav@682:     *     return Objects.hash(x, y, z);
jaroslav@682:     * }
jaroslav@682:     * 
jaroslav@682: * jaroslav@682: * Warning: When a single object reference is supplied, the returned jaroslav@682: * value does not equal the hash code of that object reference. This jaroslav@682: * value can be computed by calling {@link #hashCode(Object)}. jaroslav@682: * jaroslav@682: * @param values the values to be hashed jaroslav@682: * @return a hash value of the sequence of input values jaroslav@682: * @see Arrays#hashCode(Object[]) jaroslav@682: * @see List#hashCode jaroslav@682: */ jaroslav@682: public static int hash(Object... values) { jaroslav@682: return Arrays.hashCode(values); jaroslav@682: } jaroslav@682: jaroslav@682: /** jaroslav@682: * Returns the result of calling {@code toString} for a non-{@code jaroslav@682: * null} argument and {@code "null"} for a {@code null} argument. jaroslav@682: * jaroslav@682: * @param o an object jaroslav@682: * @return the result of calling {@code toString} for a non-{@code jaroslav@682: * null} argument and {@code "null"} for a {@code null} argument jaroslav@682: * @see Object#toString jaroslav@682: * @see String#valueOf(Object) jaroslav@682: */ jaroslav@682: public static String toString(Object o) { jaroslav@682: return String.valueOf(o); jaroslav@682: } jaroslav@682: jaroslav@682: /** jaroslav@682: * Returns the result of calling {@code toString} on the first jaroslav@682: * argument if the first argument is not {@code null} and returns jaroslav@682: * the second argument otherwise. jaroslav@682: * jaroslav@682: * @param o an object jaroslav@682: * @param nullDefault string to return if the first argument is jaroslav@682: * {@code null} jaroslav@682: * @return the result of calling {@code toString} on the first jaroslav@682: * argument if it is not {@code null} and the second argument jaroslav@682: * otherwise. jaroslav@682: * @see Objects#toString(Object) jaroslav@682: */ jaroslav@682: public static String toString(Object o, String nullDefault) { jaroslav@682: return (o != null) ? o.toString() : nullDefault; jaroslav@682: } jaroslav@682: jaroslav@682: /** jaroslav@682: * Returns 0 if the arguments are identical and {@code jaroslav@682: * c.compare(a, b)} otherwise. jaroslav@682: * Consequently, if both arguments are {@code null} 0 jaroslav@682: * is returned. jaroslav@682: * jaroslav@682: *

Note that if one of the arguments is {@code null}, a {@code jaroslav@682: * NullPointerException} may or may not be thrown depending on jaroslav@682: * what ordering policy, if any, the {@link Comparator Comparator} jaroslav@682: * chooses to have for {@code null} values. jaroslav@682: * jaroslav@682: * @param the type of the objects being compared jaroslav@682: * @param a an object jaroslav@682: * @param b an object to be compared with {@code a} jaroslav@682: * @param c the {@code Comparator} to compare the first two arguments jaroslav@682: * @return 0 if the arguments are identical and {@code jaroslav@682: * c.compare(a, b)} otherwise. jaroslav@682: * @see Comparable jaroslav@682: * @see Comparator jaroslav@682: */ jaroslav@682: public static int compare(T a, T b, Comparator c) { jaroslav@682: return (a == b) ? 0 : c.compare(a, b); jaroslav@682: } jaroslav@682: jaroslav@682: /** jaroslav@682: * Checks that the specified object reference is not {@code null}. This jaroslav@682: * method is designed primarily for doing parameter validation in methods jaroslav@682: * and constructors, as demonstrated below: jaroslav@682: *

jaroslav@682:      * public Foo(Bar bar) {
jaroslav@682:      *     this.bar = Objects.requireNonNull(bar);
jaroslav@682:      * }
jaroslav@682:      * 
jaroslav@682: * jaroslav@682: * @param obj the object reference to check for nullity jaroslav@682: * @param the type of the reference jaroslav@682: * @return {@code obj} if not {@code null} jaroslav@682: * @throws NullPointerException if {@code obj} is {@code null} jaroslav@682: */ jaroslav@682: public static T requireNonNull(T obj) { jaroslav@682: if (obj == null) jaroslav@682: throw new NullPointerException(); jaroslav@682: return obj; jaroslav@682: } jaroslav@682: jaroslav@682: /** jaroslav@682: * Checks that the specified object reference is not {@code null} and jaroslav@682: * throws a customized {@link NullPointerException} if it is. This method jaroslav@682: * is designed primarily for doing parameter validation in methods and jaroslav@682: * constructors with multiple parameters, as demonstrated below: jaroslav@682: *
jaroslav@682:      * public Foo(Bar bar, Baz baz) {
jaroslav@682:      *     this.bar = Objects.requireNonNull(bar, "bar must not be null");
jaroslav@682:      *     this.baz = Objects.requireNonNull(baz, "baz must not be null");
jaroslav@682:      * }
jaroslav@682:      * 
jaroslav@682: * jaroslav@682: * @param obj the object reference to check for nullity jaroslav@682: * @param message detail message to be used in the event that a {@code jaroslav@682: * NullPointerException} is thrown jaroslav@682: * @param the type of the reference jaroslav@682: * @return {@code obj} if not {@code null} jaroslav@682: * @throws NullPointerException if {@code obj} is {@code null} jaroslav@682: */ jaroslav@682: public static T requireNonNull(T obj, String message) { jaroslav@682: if (obj == null) jaroslav@682: throw new NullPointerException(message); jaroslav@682: return obj; jaroslav@682: } jaroslav@682: }