com.google.common.base
Class Objects

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

@GwtCompatible
public final class Objects
extends Object

Helper functions that can operate on any Object.

Since:
2 (imported from Google Collections Library)
Author:
Laurence Gonsalves

Nested Class Summary
static class Objects.ToStringHelper
          Support class for toStringHelper(java.lang.Object).
 
Method Summary
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 Objects.ToStringHelper toStringHelper(Class<?> clazz)
          Creates an instance of Objects.ToStringHelper in the same manner as toStringHelper(Object), but using the name of clazz instead of using an instance's Object.getClass().
static Objects.ToStringHelper toStringHelper(Object self)
          Creates an instance of Objects.ToStringHelper.
static Objects.ToStringHelper toStringHelper(String className)
          Creates an instance of Objects.ToStringHelper in the same manner as toStringHelper(Object), but using className instead of using an instance's Object.getClass().
 
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(@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(@Nullable
                           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(Object self)
Creates an instance of Objects.ToStringHelper.

This is helpful for implementing Object.toString(). Specification by example:

   // Returns "ClassName{}"
   Objects.toStringHelper(this)
       .toString();

   // Returns "ClassName{x=1}"
   Objects.toStringHelper(this)
       .add("x", 1)
       .toString();

   // Returns "MyObject{x=1}"
   Objects.toStringHelper("MyObject")
       .add("x", 1)
       .toString();

   // Returns "ClassName{x=1, y=foo}"
   Objects.toStringHelper(this)
       .add("x", 1)
       .add("y", "foo")
       .toString();
   }

Parameters:
self - the object to generate the string for (typically this), used only for its class name
Since:
2

toStringHelper

public static Objects.ToStringHelper toStringHelper(Class<?> clazz)
Creates an instance of Objects.ToStringHelper in the same manner as toStringHelper(Object), but using the name of clazz instead of using an instance's Object.getClass().

Parameters:
clazz - the Class of the instance
Since:
7 (source-compatible since 2)

toStringHelper

public static Objects.ToStringHelper toStringHelper(String className)
Creates an instance of Objects.ToStringHelper in the same manner as toStringHelper(Object), but using className instead of using an instance's Object.getClass().

Parameters:
className - the name of the instance type
Since:
7 (source-compatible since 2)

firstNonNull

public static <T> T firstNonNull(@Nullable
                                 T first,
                                 @Nullable
                                 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:
NullPointerException - if both first and second were null
Since:
3