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.jspecify.annotations.Nullable;
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
034  /**
035   * Determines whether two possibly-null objects are equal. Returns:
036   *
037   * <ul>
038   *   <li>{@code true} if {@code a} and {@code b} are both null.
039   *   <li>{@code true} if {@code a} and {@code b} are both non-null and they are equal according to
040   *       {@link Object#equals(Object)}.
041   *   <li>{@code false} in all other situations.
042   * </ul>
043   *
044   * <p>This assumes that any non-null objects passed to this function conform to the {@code
045   * equals()} contract.
046   *
047   * <p><b>Note:</b> this method is now unnecessary and should be treated as deprecated; use {@link
048   * java.util.Objects#equals} instead.
049   */
050  @SuppressWarnings("InlineMeSuggester") // would introduce fully qualified references to Objects
051  public static boolean equal(@Nullable Object a, @Nullable Object b) {
052    return java.util.Objects.equals(a, 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   * {@snippet :
065   * public int hashCode() {
066   *   return Objects.hashCode(getX(), getY(), getZ());
067   * }
068   * }
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:</b> this method is now unnecessary and should be treated as deprecated; use {@link
074   * java.util.Objects#hash} instead.
075   */
076  @SuppressWarnings("InlineMeSuggester") // would introduce fully qualified references to Objects
077  public static int hashCode(@Nullable Object @Nullable ... objects) {
078    return java.util.Objects.hash(objects);
079  }
080
081  private Objects() {}
082}