public class ToStringHelper extends Object
Objects#toStringHelper.| Modifier and Type | Method and Description |
|---|---|
ToStringHelper |
add(String name,
boolean value)
Adds a name/value pair to the formatted output in
name=value
format. |
ToStringHelper |
add(String name,
char value)
Adds a name/value pair to the formatted output in
name=value
format. |
ToStringHelper |
add(String name,
double value)
Adds a name/value pair to the formatted output in
name=value
format. |
ToStringHelper |
add(String name,
float value)
Adds a name/value pair to the formatted output in
name=value
format. |
ToStringHelper |
add(String name,
int value)
Adds a name/value pair to the formatted output in
name=value
format. |
ToStringHelper |
add(String name,
long value)
Adds a name/value pair to the formatted output in
name=value
format. |
ToStringHelper |
add(String name,
Object value)
Adds a name/value pair to the formatted output in
name=value
format. |
ToStringHelper |
addValue(boolean value)
Adds an unnamed value to the formatted output.
|
ToStringHelper |
addValue(char value)
Adds an unnamed value to the formatted output.
|
ToStringHelper |
addValue(double value)
Adds an unnamed value to the formatted output.
|
ToStringHelper |
addValue(float value)
Adds an unnamed value to the formatted output.
|
ToStringHelper |
addValue(int value)
Adds an unnamed value to the formatted output.
|
ToStringHelper |
addValue(long value)
Adds an unnamed value to the formatted output.
|
ToStringHelper |
addValue(Object value)
Adds an unnamed value to the formatted output.
|
static <T> T |
checkNotNull(T reference) |
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. |
String |
toString()
Returns a string in the format specified by
Objects#toStringHelper(Object). |
static ToStringHelper |
toStringHelper(Class<?> clazz)
Creates an instance of
ToStringHelper in the same manner as
Objects#toStringHelper(Object), but using the name of clazz
instead of using an instance's Object.getClass(). |
static ToStringHelper |
toStringHelper(Object self)
Creates an instance of
ToStringHelper. |
static ToStringHelper |
toStringHelper(String className)
Creates an instance of
ToStringHelper in the same manner as
Objects#toStringHelper(Object), but using className instead
of using an instance's Object.getClass(). |
public ToStringHelper add(String name, Object value)
name=value
format. If value is null, the string "null"
is used, unless #omitNullValues() is called, in which case this
name/value pair will not be added.public ToStringHelper add(String name, boolean value)
name=value
format.public ToStringHelper add(String name, char value)
name=value
format.public ToStringHelper add(String name, double value)
name=value
format.public ToStringHelper add(String name, float value)
name=value
format.public ToStringHelper add(String name, int value)
name=value
format.public ToStringHelper add(String name, long value)
name=value
format.public ToStringHelper addValue(Object value)
It is strongly encouraged to use add(String, Object) instead
and give value a readable name.
public ToStringHelper addValue(boolean value)
It is strongly encouraged to use add(String, boolean) instead
and give value a readable name.
public ToStringHelper addValue(char value)
It is strongly encouraged to use add(String, char) instead
and give value a readable name.
public ToStringHelper addValue(double value)
It is strongly encouraged to use add(String, double) instead
and give value a readable name.
public ToStringHelper addValue(float value)
It is strongly encouraged to use add(String, float) instead
and give value a readable name.
public ToStringHelper addValue(int value)
It is strongly encouraged to use add(String, int) instead
and give value a readable name.
public ToStringHelper addValue(long value)
It is strongly encouraged to use add(String, long) instead
and give value a readable name.
public String toString()
Objects#toStringHelper(Object).public static ToStringHelper toStringHelper(Object self)
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 namepublic static ToStringHelper toStringHelper(Class<?> clazz)
ToStringHelper in the same manner as
Objects#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 instancepublic static ToStringHelper toStringHelper(String className)
ToStringHelper in the same manner as
Objects#toStringHelper(Object), but using className instead
of using an instance's Object.getClass().className - the name of the instance typepublic 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).
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 <T> T checkNotNull(T reference)
Copyright © 2012–2017 CoGrOO. All rights reserved.