public final class

Objects

extends Object
java.lang.Object
   ↳ com.google.gdata.util.common.base.Objects

Class Overview

Helper functions that can operate on any Object.

Summary

Nested Classes
class Objects.ToStringHelper Helper class for generating consistent toString across ads/api pojos. 
Public Methods
static boolean deepEquals(Object a, Object b)
Determines if two objects are equal as determined by equals(Object), or "deeply equal" if both are arrays.
static int deepHashCode(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 String deepToString(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(Object a, 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(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, String message)
Checks that the specified object is not null.
static Objects.ToStringHelper toStringHelper(Object object)
Creates an instance of Objects.ToStringHelper.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public static boolean deepEquals (Object a, Object b)

Determines if two objects are equal as determined by 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 deepEquals(Object[], Object[]), or 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.

Parameters
a
b

public static int deepHashCode (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 deepHashCode(Object[]), or hashCode(int[]) or the like is used to calculate the hash code. If obj isn't null or an array, obj.hashCode() is returned.

Parameters
obj

public static String deepToString (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 deepToString(Object[]), toString(int[]) or the like is used to generate the string. If obj isn't null or an array, obj.toString() is returned.

Parameters
obj

public static boolean equal (Object a, Object b)

Determines whether two possibly-null objects are equal. Returns:

  • true if a and b are both null.
  • true if a and b are both non-null and they are equal according to equals(Object).
  • false in all other situations.

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

Parameters
a
b

public static 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.

Parameters
first
second
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

public static int hashCode (Object... objects)

Generates a hash code for multiple values. The hash code is generated by calling hashCode(Object[]).

This is useful for implementing 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.

Parameters
objects

public static 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
NullPointerException if obj is null.

public static T nonNull (T obj, 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
NullPointerException if obj is null

public static Objects.ToStringHelper toStringHelper (Object object)

Creates an instance of Objects.ToStringHelper.

This is helpful for implementing 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}".

Parameters
object