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 033public final class Objects extends ExtraObjectsMethodsForWeb { 034 private Objects() {} 035 036 /** 037 * Determines whether two possibly-null objects are equal. Returns: 038 * 039 * <ul> 040 * <li>{@code true} if {@code a} and {@code b} are both null. 041 * <li>{@code true} if {@code a} and {@code b} are both non-null and they are equal according to 042 * {@link Object#equals(Object)}. 043 * <li>{@code false} in all other situations. 044 * </ul> 045 * 046 * <p>This assumes that any non-null objects passed to this function conform to the {@code 047 * equals()} contract. 048 * 049 * <p><b>Note:</b> this method is now unnecessary and should be treated as deprecated; use {@link 050 * java.util.Objects#equals} instead. 051 */ 052 public static boolean equal(@CheckForNull Object a, @CheckForNull Object b) { 053 return a == b || (a != null && a.equals(b)); 054 } 055 056 /** 057 * Generates a hash code for multiple values. The hash code is generated by calling {@link 058 * Arrays#hashCode(Object[])}. Note that array arguments to this method, with the exception of a 059 * single Object array, do not get any special handling; their hash codes are based on identity 060 * and not contents. 061 * 062 * <p>This is useful for implementing {@link Object#hashCode()}. For example, in an object that 063 * has three properties, {@code x}, {@code y}, and {@code z}, one could write: 064 * 065 * <pre>{@code 066 * public int hashCode() { 067 * return Objects.hashCode(getX(), getY(), getZ()); 068 * } 069 * }</pre> 070 * 071 * <p><b>Warning:</b> When a single object is supplied, the returned hash code does not equal the 072 * hash code of that object. 073 * 074 * <p><b>Note:</b> this method is now unnecessary and should be treated as deprecated; use {@link 075 * java.util.Objects#hash} instead. 076 */ 077 public static int hashCode(@CheckForNull @Nullable Object... objects) { 078 return Arrays.hashCode(objects); 079 } 080}