001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.base;
016
017import com.google.common.annotations.GwtCompatible;
018import java.util.Arrays;
019import org.checkerframework.checker.nullness.compatqual.NullableDecl;
020
021/**
022 * Helper functions that can operate on any {@code Object}.
023 *
024 * <p>See the Guava User Guide on <a
025 * href="https://github.com/google/guava/wiki/CommonObjectUtilitiesExplained">writing {@code Object}
026 * methods with {@code Objects}</a>.
027 *
028 * @author Laurence Gonsalves
029 * @since 2.0
030 */
031@GwtCompatible
032public final class Objects extends ExtraObjectsMethodsForWeb {
033  private Objects() {}
034
035  /**
036   * Determines whether two possibly-null objects are equal. Returns:
037   *
038   * <ul>
039   *   <li>{@code true} if {@code a} and {@code b} are both null.
040   *   <li>{@code true} if {@code a} and {@code b} are both non-null and they are equal according to
041   *       {@link Object#equals(Object)}.
042   *   <li>{@code false} in all other situations.
043   * </ul>
044   *
045   * <p>This assumes that any non-null objects passed to this function conform to the {@code
046   * equals()} contract.
047   *
048   * <p><b>Note for Java 7 and later:</b> This method should be treated as deprecated; use {@link
049   * java.util.Objects#equals} instead.
050   */
051  public static boolean equal(@NullableDecl Object a, @NullableDecl Object b) {
052    return a == b || (a != null && a.equals(b));
053  }
054
055  /**
056   * Generates a hash code for multiple values. The hash code is generated by calling {@link
057   * Arrays#hashCode(Object[])}. Note that array arguments to this method, with the exception of a
058   * single Object array, do not get any special handling; their hash codes are based on identity
059   * and not contents.
060   *
061   * <p>This is useful for implementing {@link Object#hashCode()}. For example, in an object that
062   * has three properties, {@code x}, {@code y}, and {@code z}, one could write:
063   *
064   * <pre>{@code
065   * public int hashCode() {
066   *   return Objects.hashCode(getX(), getY(), getZ());
067   * }
068   * }</pre>
069   *
070   * <p><b>Warning:</b> When a single object is supplied, the returned hash code does not equal the
071   * hash code of that object.
072   *
073   * <p><b>Note for Java 7 and later:</b> This method should be treated as deprecated; use {@link
074   * java.util.Objects#hash} instead.
075   */
076  public static int hashCode(@NullableDecl Object... objects) {
077    return Arrays.hashCode(objects);
078  }
079}