com.google.common.base
Class Objects

java.lang.Object
  extended by com.google.common.base.Objects

public final class Objects
extends Object

Helper functions for operating on Objects.

Author:
Laurence Gonsalves

Method Summary
static boolean deepEquals(Object o1, Object o2)
          Determines if two objects are equal as determined by Object.equals(Object), or "deeply equal" if both are arrays.
static int deepHashCode(Object o)
          Gets hash code of an object, optionally returns hash code based on the "deep contents" of array if the object is an array.
static String deepToString(Object o)
          Gets string representation of an object, or the "deep content" of the array if the o is an array.
static boolean equal(Object a, Object b)
          Determines whether two possibly-null objects are equal.
static
<T> T
firstNonNull(T first, T second)
          Returns the first of the given parameters that is not null if any, or otherwise throws NullPointerException.
static int hashCode(Object... objects)
          Generates a hashcode for multiple values.
static
<T> T
nonNull(T o)
          Checks that the specified object is not null.
static
<T> T
nonNull(T o, String message)
          Checks that the specified object is not null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

deepHashCode

public static int deepHashCode(@Nullable
                               Object o)
Gets hash code of an object, optionally returns hash code based on the "deep contents" of array if the object is an array.

If o is null, 0 is returned; if o is an array, the corresponding Arrays.deepHashCode(Object[]), or Arrays.hashCode(int[]) or the like is used to calculate the hash code.


deepToString

public static String deepToString(@Nullable
                                  Object o)
Gets string representation of an object, or the "deep content" of the array if the o is an array.

If o is null, "null" is returned; if o is an array, the corresponding Arrays.deepToString(Object[]), Arrays.toString(int[]) or the like is used to get the string.


deepEquals

public static boolean deepEquals(@Nullable
                                 Object o1,
                                 @Nullable
                                 Object o2)
Determines if two objects are equal as determined by Object.equals(Object), or "deeply equal" if both are arrays.

If both objects are null, true is returned; if both objects are array, the corresponding Arrays.deepEquals(Object[], Object[]), or Arrays.equals(int[], int[]) or the like are called to determine equality.

Note that this method does not "deeply" compare the fields of the objects.


equal

public static boolean equal(@Nullable
                            Object a,
                            @Nullable
                            Object b)
Determines whether two possibly-null objects are equal. Returns:

This assumes that any non-null objects passed to this function conform to the equals() contract.


hashCode

public static int hashCode(Object... objects)
Generates a hashcode for multiple values. The hash code is generated by calling Arrays.hashCode(Object[]).

This is useful for implementing Object.hashCode(). For example, in an object that has three properties, x, y, and z, one could write:

 public int hashCode() {
   return Objects.hashCode(getX(), getY(), getZ());
 }
Warning: When a single object is supplied, the returned hash code does not equal the hash code of that object.


nonNull

public static <T> T nonNull(T o)
Checks that the specified object is not null.

Parameters:
o - the object to check for nullness.
Returns:
o if not null.
Throws:
NullPointerException - if o is null.

nonNull

public static <T> T nonNull(T o,
                            String message)
Checks that the specified object is not null.

Parameters:
o - the object to check for nullness.
message - exception message used in the event that a NullPointerException is thrown.
Returns:
o if not null.
Throws:
NullPointerException - if o is null.

firstNonNull

public static <T> T firstNonNull(@Nullable
                                 T first,
                                 @Nullable
                                 T second)
Returns the first of the given parameters that is not null if any, or otherwise throws NullPointerException.

Returns:
first if first is not null, or second if first is null and second is not null.
Throws:
NullPointerException - if both first and second were null.