public final class Objects extends Object
Object.
See the Guava User Guide on writing Object methods
with Objects.
| Modifier and Type | Class and Description |
|---|---|
static class |
Objects.ToStringHelper
Support class for
toStringHelper(java.lang.Object). |
| Modifier | Constructor and Description |
|---|---|
private |
Objects()
Hidden for pure static factory.
|
| Modifier and Type | Method and Description |
|---|---|
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.
|
private static String |
simpleName(Class<?> clazz)
Class.getSimpleName() is not GWT compatible yet, so we provide our own implementation. |
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(). |
public static boolean equal(Object a, Object b)
Returns:
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.
a - first object to compareb - second object to comparetrue if a and be are equal, else falsepublic static int hashCode(Object... objects)
The hash code is generated by calling Arrays.hashCode(Object[]). Note that array arguments to
this method, with the exception of a single Object array, do not get any special handling; their hash codes are
based on identity and not contents.
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.
objects - objects to hashpublic static <T> T firstNonNull(T first,
T second)
null, if either is, or otherwise throws a
NullPointerException.
Note: if first is represented as an Optional<T>, this can be accomplished with
first.or(second). That approach also allows for lazy evaluation of the fallback instance, using
first.or(Supplier).
T - a T object.first - a T object.second - a T object.first if first is not null, or second if first is null
and second is not nullNullPointerException - if both first and second were nullpublic static Objects.ToStringHelper toStringHelper(Object self)
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();
// Returns "ClassName{x=1}"
Objects.toStringHelper(this)
.omitNullValues()
.add("x", 1)
.add("y", null)
.toString();
}
Note that in GWT, class names are often obfuscated.
self - the object to generate the string for (typically this), used only for its class namenull, always new instancepublic static Objects.ToStringHelper toStringHelper(Class<?> clazz)
Objects.ToStringHelper in the same manner as
toStringHelper(Object), but using the name of clazz
instead of using an instance's Object.getClass().
Note that in GWT, class names are often obfuscated.
clazz - the Class of the instancenull, always new instancepublic static Objects.ToStringHelper toStringHelper(String className)
Objects.ToStringHelper in the same manner as
toStringHelper(Object), but using className instead of
using an instance's Object.getClass().className - the name of the instance typenull, always new instanceprivate static String simpleName(Class<?> clazz)
Class.getSimpleName() is not GWT compatible yet, so we provide our own implementation.clazz - the Class of the instancenullCopyright © 2014 Sven Strittmatter. All Rights Reserved.