com.google.gdata.util.common.base
Class Objects

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

public final class Objects
extends java.lang.Object

Helper functions that can operate on any Object.


Nested Class Summary
static class Objects.ToStringHelper
          Helper class for generating consistent toString across ads/api pojos.

This class is made to support toStringHelper(Object).

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

Method Detail

equal

public static boolean equal(java.lang.Object a,
                            java.lang.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(java.lang.Object... objects)
Generates a hash code 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.


toStringHelper

public static Objects.ToStringHelper toStringHelper(java.lang.Object object)
Creates an instance of Objects.ToStringHelper.

This is helpful for implementing Object.toString(). For example, in an object that contains two member variables, x, and y, one could write:

    
   public class ClassName {
     public String toString() {
       return Objects.toStringHelper(this)
           .add("x", x)
           .add("y", y)
           .toString();
     }
   }
 
Assuming the values of x and y are 1 and 2, this code snippet returns the string "ClassName{x=1, y=2}".


nonNull

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

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

nonNull

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

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

firstNonNull

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

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

deepEquals

public static boolean deepEquals(java.lang.Object a,
                                 java.lang.Object b)
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 only one is null, false is returned. If both objects are arrays, the corresponding Arrays.deepEquals(Object[], Object[]), or Arrays.equals(int[], int[]) or the like are called to determine equality. Otherwise, a.equals(b) is returned.

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


deepHashCode

public static int deepHashCode(java.lang.Object obj)
Determines the hash code of an object, returning a hash code based on the array's "deep contents" if the object is an array.

If obj is null, 0 is returned. If obj is an array, the corresponding Arrays.deepHashCode(Object[]), or Arrays.hashCode(int[]) or the like is used to calculate the hash code. If obj isn't null or an array, obj.hashCode() is returned.


deepToString

public static java.lang.String deepToString(java.lang.Object obj)
Determines the string representation of an object, or the "deep contents of the array if the obj is an array.

If obj is null, "null" is returned; if obj is an array, the corresponding Arrays.deepToString(Object[]), Arrays.toString(int[]) or the like is used to generate the string. If obj isn't null or an array, obj.toString() is returned.