Coverage Summary for Class: Objects (com.google.common.base)

Class Class, % Method, % Line, %
Objects 100% (1/1) 66.7% (2/3) 66.7% (2/3)


1 /* 2  * Copyright (C) 2007 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5  * in compliance with the License. You may obtain a copy of the License at 6  * 7  * http://www.apache.org/licenses/LICENSE-2.0 8  * 9  * Unless required by applicable law or agreed to in writing, software distributed under the License 10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11  * or implied. See the License for the specific language governing permissions and limitations under 12  * the License. 13  */ 14  15 package com.google.common.base; 16  17 import com.google.common.annotations.GwtCompatible; 18 import java.util.Arrays; 19 import javax.annotation.CheckForNull; 20 import org.checkerframework.checker.nullness.qual.Nullable; 21  22 /** 23  * Helper functions that can operate on any {@code Object}. 24  * 25  * <p>See the Guava User Guide on <a 26  * href="https://github.com/google/guava/wiki/CommonObjectUtilitiesExplained">writing {@code Object} 27  * methods with {@code Objects}</a>. 28  * 29  * @author Laurence Gonsalves 30  * @since 2.0 31  */ 32 @GwtCompatible 33 @ElementTypesAreNonnullByDefault 34 public final class Objects extends ExtraObjectsMethodsForWeb { 35  private Objects() {} 36  37  /** 38  * Determines whether two possibly-null objects are equal. Returns: 39  * 40  * <ul> 41  * <li>{@code true} if {@code a} and {@code b} are both null. 42  * <li>{@code true} if {@code a} and {@code b} are both non-null and they are equal according to 43  * {@link Object#equals(Object)}. 44  * <li>{@code false} in all other situations. 45  * </ul> 46  * 47  * <p>This assumes that any non-null objects passed to this function conform to the {@code 48  * equals()} contract. 49  * 50  * <p><b>Note for Java 7 and later:</b> This method should be treated as deprecated; use {@link 51  * java.util.Objects#equals} instead. 52  */ 53  public static boolean equal(@CheckForNull Object a, @CheckForNull Object b) { 54  return a == b || (a != null && a.equals(b)); 55  } 56  57  /** 58  * Generates a hash code for multiple values. The hash code is generated by calling {@link 59  * Arrays#hashCode(Object[])}. Note that array arguments to this method, with the exception of a 60  * single Object array, do not get any special handling; their hash codes are based on identity 61  * and not contents. 62  * 63  * <p>This is useful for implementing {@link Object#hashCode()}. For example, in an object that 64  * has three properties, {@code x}, {@code y}, and {@code z}, one could write: 65  * 66  * <pre>{@code 67  * public int hashCode() { 68  * return Objects.hashCode(getX(), getY(), getZ()); 69  * } 70  * }</pre> 71  * 72  * <p><b>Warning:</b> When a single object is supplied, the returned hash code does not equal the 73  * hash code of that object. 74  * 75  * <p><b>Note for Java 7 and later:</b> This method should be treated as deprecated; use {@link 76  * java.util.Objects#hash} instead. 77  */ 78  public static int hashCode(@CheckForNull @Nullable Object... objects) { 79  return Arrays.hashCode(objects); 80  } 81 }