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 javax.annotation.CheckForNull; 020import org.checkerframework.checker.nullness.qual.Nullable; 021 022/** 023 * Helper functions that can operate on any {@code Object}. 024 * 025 * <p>See the Guava User Guide on <a 026 * href="https://github.com/google/guava/wiki/CommonObjectUtilitiesExplained">writing {@code Object} 027 * methods with {@code Objects}</a>. 028 * 029 * @author Laurence Gonsalves 030 * @since 2.0 031 */ 032@GwtCompatible 033@ElementTypesAreNonnullByDefault 034public final class Objects extends ExtraObjectsMethodsForWeb { 035 private Objects() {} 036 037 /** 038 * Determines whether two possibly-null objects are equal. Returns: 039 * 040 * <ul> 041 * <li>{@code true} if {@code a} and {@code b} are both null. 042 * <li>{@code true} if {@code a} and {@code b} are both non-null and they are equal according to 043 * {@link Object#equals(Object)}. 044 * <li>{@code false} in all other situations. 045 * </ul> 046 * 047 * <p>This assumes that any non-null objects passed to this function conform to the {@code 048 * equals()} contract. 049 * 050 * <p><b>Java 7+ users:</b> This method should be treated as deprecated; use {@link 051 * java.util.Objects#equals} instead. 052 */ 053 public static boolean equal(@CheckForNull Object a, @CheckForNull Object b) { 054 return a == b || (a != null && a.equals(b)); 055 } 056 057 /** 058 * Generates a hash code for multiple values. The hash code is generated by calling {@link 059 * Arrays#hashCode(Object[])}. Note that array arguments to this method, with the exception of a 060 * single Object array, do not get any special handling; their hash codes are based on identity 061 * and not contents. 062 * 063 * <p>This is useful for implementing {@link Object#hashCode()}. For example, in an object that 064 * has three properties, {@code x}, {@code y}, and {@code z}, one could write: 065 * 066 * <pre>{@code 067 * public int hashCode() { 068 * return Objects.hashCode(getX(), getY(), getZ()); 069 * } 070 * }</pre> 071 * 072 * <p><b>Warning:</b> When a single object is supplied, the returned hash code does not equal the 073 * hash code of that object. 074 * 075 * <p><b>Java 7+ users:</b> This method should be treated as deprecated; use {@link 076 * java.util.Objects#hash} instead. 077 */ 078 public static int hashCode(@CheckForNull @Nullable Object... objects) { 079 return Arrays.hashCode(objects); 080 } 081}