@GwtCompatible
public final class Objects
extends java.lang.Object
Object.| Modifier and Type | Class and Description |
|---|---|
static class |
Objects.ToStringHelper
Helper class for generating consistent toString across ads/api pojos.
This class is made to support
toStringHelper(Object). |
| Modifier and Type | Method and Description |
|---|---|
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. |
public static boolean equal(java.lang.Object a,
java.lang.Object b)
true if a and b are both null.
true if a and b are both non-null and they are
equal according to Object.equals(Object).
false in all other situations.
This assumes that any non-null objects passed to this function conform
to the equals() contract.
public static int hashCode(java.lang.Object... objects)
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.public static Objects.ToStringHelper toStringHelper(java.lang.Object object)
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}".public static <T> T nonNull(T obj)
null.obj - the object to check for nullnessobj if not null.java.lang.NullPointerException - if obj is null.public static <T> T nonNull(T obj,
java.lang.String message)
null.obj - the object to check for nullnessmessage - exception message used in the event that a NullPointerException is thrownobj if not nulljava.lang.NullPointerException - if obj is nullpublic static <T> T firstNonNull(T first,
T second)
null, if
either is, or otherwise throws a NullPointerException.first if first is not null, or
second if first is null and second is
not nulljava.lang.NullPointerException - if both first and second were
nullpublic static boolean deepEquals(java.lang.Object a,
java.lang.Object b)
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.
public static int deepHashCode(java.lang.Object obj)
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.
public static java.lang.String deepToString(java.lang.Object obj)
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.