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.Nullable; 020 021/** 022 * Helper functions that can operate on any {@code Object}. 023 * 024 * <p>See the Guava User Guide on 025 * <a href="https://github.com/google/guava/wiki/CommonObjectUtilitiesExplained">writing 026 * {@code Object} 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 046 * {@code equals()} contract. 047 * 048 * <p><b>Note for Java 7 and later:</b> This method should be treated as deprecated; use 049 * {@link java.util.Objects#equals} instead. 050 */ 051 public static boolean equal(@Nullable Object a, @Nullable 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 057 * {@link Arrays#hashCode(Object[])}. Note that array arguments to this method, with the exception 058 * of a single Object array, do not get any special handling; their hash codes are based on 059 * identity 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 * }}</pre> 068 * 069 * <p><b>Warning:</b> When a single object is supplied, the returned hash code does not equal the 070 * hash code of that object. 071 * 072 * <p><b>Note for Java 7 and later:</b> This method should be treated as deprecated; use 073 * {@link java.util.Objects#hash} instead. 074 */ 075 public static int hashCode(@Nullable Object... objects) { 076 return Arrays.hashCode(objects); 077 } 078}