Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.internal.util;
     18 
     19 import java.util.Arrays;
     20 
     21 /**
     22  * Object utility methods.
     23  */
     24 public class Objects {
     25 
     26     /**
     27      * Determines whether two possibly-null objects are equal. Returns:
     28      *
     29      * <ul>
     30      * <li>{@code true} if {@code a} and {@code b} are both null.
     31      * <li>{@code true} if {@code a} and {@code b} are both non-null and they are
     32      *     equal according to {@link Object#equals(Object)}.
     33      * <li>{@code false} in all other situations.
     34      * </ul>
     35      *
     36      * <p>This assumes that any non-null objects passed to this function conform
     37      * to the {@code equals()} contract.
     38      */
     39     public static boolean equal(Object a, Object b) {
     40         return a == b || (a != null && a.equals(b));
     41     }
     42 
     43     /**
     44      * Generates a hash code for multiple values. The hash code is generated by
     45      * calling {@link Arrays#hashCode(Object[])}.
     46      *
     47      * <p>This is useful for implementing {@link Object#hashCode()}. For example,
     48      * in an object that has three properties, {@code x}, {@code y}, and
     49      * {@code z}, one could write:
     50      * <pre>
     51      * public int hashCode() {
     52      *   return Objects.hashCode(getX(), getY(), getZ());
     53      * }</pre>
     54      *
     55      * <b>Warning</b>: When a single object is supplied, the returned hash code
     56      * does not equal the hash code of that object.
     57      */
     58     public static int hashCode(Object... objects) {
     59         return Arrays.hashCode(objects);
     60     }
     61 
     62 }
     63