Package com.google.common.base
Class MoreObjects
- java.lang.Object
- 
- com.google.common.base.MoreObjects
 
- 
 @GwtCompatible public final class MoreObjects extends java.lang.Object Helper functions that operate on anyObject, and are not already provided inObjects.See the Guava User Guide on writing Objectmethods withMoreObjects.- Since:
- 18.0 (since 2.0 as Objects)
- Author:
- Laurence Gonsalves
 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classMoreObjects.ToStringHelperSupport class fortoStringHelper(java.lang.Object).
 - 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> TfirstNonNull(T first, T second)Returns the first of two given parameters that is notnull, if either is, or otherwise throws aNullPointerException.static MoreObjects.ToStringHelpertoStringHelper(java.lang.Class<?> clazz)Creates an instance ofMoreObjects.ToStringHelperin the same manner astoStringHelper(Object), but using the simple name ofclazzinstead of using an instance'sObject.getClass().static MoreObjects.ToStringHelpertoStringHelper(java.lang.Object self)Creates an instance ofMoreObjects.ToStringHelper.static MoreObjects.ToStringHelpertoStringHelper(java.lang.String className)Creates an instance ofMoreObjects.ToStringHelperin the same manner astoStringHelper(Object), but usingclassNameinstead of using an instance'sObject.getClass().
 
- 
- 
- 
Method Detail- 
firstNonNullpublic static <T> T firstNonNull(@CheckForNull T first, @CheckForNull T second) Returns the first of two given parameters that is notnull, if either is, or otherwise throws aNullPointerException.To find the first non-null element in an iterable, use Iterables.find(iterable, Predicates.notNull()). For varargs, useIterables.find(Arrays.asList(a, b, c, ...), Predicates.notNull()), static importing as necessary.Note: if firstis represented as anOptional, this can be accomplished withfirst.or(second). That approach also allows for lazy evaluation of the fallback instance, usingfirst.or(supplier).Java 9 users: use java.util.Objects.requireNonNullElse(first, second)instead.- Returns:
- firstif it is non-null; otherwise- secondif it is non-null
- Throws:
- java.lang.NullPointerException- if both- firstand- secondare null
- Since:
- 18.0 (since 3.0 as Objects.firstNonNull()).
 
 - 
toStringHelperpublic static MoreObjects.ToStringHelper toStringHelper(java.lang.Object self) Creates an instance ofMoreObjects.ToStringHelper.This is helpful for implementing Object.toString(). Specification by example:// Returns "ClassName{}" MoreObjects.toStringHelper(this) .toString(); // Returns "ClassName{x=1}" MoreObjects.toStringHelper(this) .add("x", 1) .toString(); // Returns "MyObject{x=1}" MoreObjects.toStringHelper("MyObject") .add("x", 1) .toString(); // Returns "ClassName{x=1, y=foo}" MoreObjects.toStringHelper(this) .add("x", 1) .add("y", "foo") .toString(); // Returns "ClassName{x=1}" MoreObjects.toStringHelper(this) .omitNullValues() .add("x", 1) .add("y", null) .toString();Note that in GWT, class names are often obfuscated. - Parameters:
- self- the object to generate the string for (typically- this), used only for its class name
- Since:
- 18.0 (since 2.0 as Objects.toStringHelper()).
 
 - 
toStringHelperpublic static MoreObjects.ToStringHelper toStringHelper(java.lang.Class<?> clazz) Creates an instance ofMoreObjects.ToStringHelperin the same manner astoStringHelper(Object), but using the simple name ofclazzinstead of using an instance'sObject.getClass().Note that in GWT, class names are often obfuscated. - Parameters:
- clazz- the- Classof the instance
- Since:
- 18.0 (since 7.0 as Objects.toStringHelper()).
 
 - 
toStringHelperpublic static MoreObjects.ToStringHelper toStringHelper(java.lang.String className) Creates an instance ofMoreObjects.ToStringHelperin the same manner astoStringHelper(Object), but usingclassNameinstead of using an instance'sObject.getClass().- Parameters:
- className- the name of the instance type
- Since:
- 18.0 (since 7.0 as Objects.toStringHelper()).
 
 
- 
 
-