/* * Copyright 2009 Sun Microsystems, Inc. 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. Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */ package java.util; /** * This class consists of {@code static} utility methods for operating * on objects. 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, and comparing two objects. * * @since 1.7 */ public 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 and if exactly one argument is {@code null}, {@code * false} is returned. 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 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 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; } /** * 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 0 if the arguments are identical and {@code * c.compare(a, b)} otherwise. * Consequently, if both arguments are {@code null} 0 * is returned. * *

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 the type of the objects being compared * @param a an object * @param b an object to be compared with {@code a} * @param c the {@code Comparator} to compare the first two arguments * @return 0 if the arguments are identical and {@code * c.compare(a, b)} otherwise. * @see Comparable * @see Comparator */ public static int compare(T a, T b, Comparator c) { return (a == b) ? 0 : c.compare(a, b); } }