Package com.google.common.base
Class MoreObjects
-
- com.google.common.base.MoreObjects
-
@GwtCompatible public final class MoreObjects extends
Helper functions that operate on anyObject
, and are not already provided in .See the Guava User Guide on .
- Since:
- 18.0 (since 2.0 as
Objects
) - Author:
- Laurence Gonsalves
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MoreObjects.ToStringHelper
Support class fortoStringHelper(java.lang.Object)
.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
T first, T second)
Returns the first of two given parameters that is notnull
, if either is, or otherwise throws a .static MoreObjects.ToStringHelper
<?> clazz)
Creates an instance ofMoreObjects.ToStringHelper
in the same manner astoStringHelper(Object)
, but using the simple name ofclazz
instead of using an instance's .static MoreObjects.ToStringHelper
self)
Creates an instance ofMoreObjects.ToStringHelper
.static MoreObjects.ToStringHelper
className)
Creates an instance of .
-
-
-
Method Detail
-
firstNonNull
public static <T> T T first, T second)
Returns the first of two given parameters that is notnull
, if either is, or otherwise throws a .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
first
is 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:
first
if it is non-null; otherwisesecond
if it is non-null- Throws:
- if both
first
andsecond
are null- Since:
- 18.0 (since 3.0 as
Objects.firstNonNull()
).
-
toStringHelper
public static self)
Creates an instance ofMoreObjects.ToStringHelper
.This is helpful for implementing . 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 (typicallythis
), used only for its class name- Since:
- 18.0 (since 2.0 as
Objects.toStringHelper()
).
-
toStringHelper
public static <?> clazz)
Creates an instance ofMoreObjects.ToStringHelper
in the same manner astoStringHelper(Object)
, but using the simple name ofclazz
instead of using an instance's .Note that in GWT, class names are often obfuscated.
- Parameters:
clazz
- the of the instance- Since:
- 18.0 (since 7.0 as
Objects.toStringHelper()
).
-
toStringHelper
public static className)
Creates an instance of .- Parameters:
className
- the name of the instance type- Since:
- 18.0 (since 7.0 as
Objects.toStringHelper()
).
-
-