Serializable
, Appendable
, CharSequence
public class ToStringBuilder extends Object implements Appendable, CharSequence, Serializable
The builder is very customizable, but has some sensible defaults:
'{'
and '}'
.", "
.null
values will by default not be included.brackets
will by default only be added to the prefix if there is content within them.
An example of a default result could be:
"TypeName{field=\"String value\", otherField=0, booleanField=false}"
.
Class diagram:
Modifier and Type | Field | Description |
---|---|---|
protected StringBuilder |
fields |
|
protected String |
prefix |
Modifier | Constructor | Description |
---|---|---|
|
ToStringBuilder(Object source) |
Constructor to create a new ToStringBuilder for a particular object or type.
|
protected |
ToStringBuilder(ToStringBuilder parent,
Object source) |
Constructor to create a new ToStringBuilder for a
source object, while copying all customizable
behaviour from another parent builder such as: brackets, separator, includeNulls and forceBrackets. |
Modifier and Type | Method | Description |
---|---|---|
ToStringBuilder |
append(char c) |
Implementation of
Appendable.append(char) to append a single character as a field value to this builder. |
ToStringBuilder |
append(CharSequence value) |
Implementation of
Appendable.append(CharSequence) , delegated to append(Object)
to add a field value without prefixing it with a field name. |
ToStringBuilder |
append(CharSequence csq,
int start,
int end) |
Implementation of
Appendable.append(CharSequence, int, int) to append a partial value to this builder
without prefixing it with a field name. |
ToStringBuilder |
append(CharSequence name,
Object value) |
This method is the only main 'append' method for this builder.
|
ToStringBuilder |
append(Object value) |
This method adds the value of a field to this builder, without prefixing it with a field name.
|
ToStringBuilder |
appendSuper(CharSequence superToStringResult) |
This method attempts to add the appended properties from a finished
toString() result to this
builder. |
protected void |
appendValue(Object value) |
Delegate method that appends a property 'value' to this builder.
|
ToStringBuilder |
brackets(String openingBracket,
String closingBracket) |
Configuration method that defines a custom set of brackets around the appended properties.
|
char |
charAt(int index) |
Implementation of
CharSequence.charAt(int) that returns a single character from the current state of this
builder. |
ToStringBuilder |
forceBrackets(String openingBracket,
String closingBracket) |
Forces brackets being added to this builder, even if there are no appended properties.
|
protected boolean |
hasContent() |
This method returns whether 'content' must be rendered (between the brackets).
|
ToStringBuilder |
includeNulls() |
Tells the builder that
null properties must be added (which is false by default). |
ToStringBuilder |
includeNulls(boolean includeNulls) |
Tells the builder whether
null property values should be appended. |
int |
length() |
|
static ToStringBuilder |
reflect(Object source) |
This method creates a
ToStringBuilder where all public fields and property accessor methods from the
source object have been reflected. |
ToStringBuilder |
separator(CharSequence separator) |
Configuration method that allows the builder to use another separator between the appended properties
(
", " by default). |
CharSequence |
subSequence(int start,
int end) |
Implementation of
CharSequence.subSequence(int, int) that returns a subsequence of the current state of
this builder. |
String |
toString() |
chars, codePoints
protected final String prefix
protected final StringBuilder fields
public ToStringBuilder(Object source)
source
- The object or type to create the ToStringBuilder for (Object, Class or a name).protected ToStringBuilder(ToStringBuilder parent, Object source)
source
object, while copying all customizable
behaviour from another parent
builder such as: brackets, separator, includeNulls and forceBrackets.parent
- The parent builder to copy settings from (optional, don't copy any settings when null
).source
- The object to create the string for (or a custom prefix string).public static ToStringBuilder reflect(Object source)
ToStringBuilder
where all public fields and property accessor methods from the
source object have been reflected.source
- The source object for which a toString() builder will be created based on its public fields and
property accessor methods.null
.public ToStringBuilder includeNulls()
null
properties must be added (which is false
by default).public ToStringBuilder includeNulls(boolean includeNulls)
null
property values should be appended. (false
by default).includeNulls
- Whether null
properties should be appended.public ToStringBuilder separator(CharSequence separator)
", "
by default).
Please note that this separator has no influence on previously appended properties.
separator
- The new separator that should be used when appending additional properties to this builder,
or null
to omit the separator altogether.public ToStringBuilder forceBrackets(String openingBracket, String closingBracket)
It is also possible to merely modify the brackets being used without forcing them into the end-result using the
brackets(String, String)
configuration method.
openingBracket
- The opening bracket to use for the resulting string.closingBracket
- The closing bracket to use for the resulting string.brackets(String, String)
public ToStringBuilder brackets(String openingBracket, String closingBracket)
It is also possible to force brackets being added even if no properties were added by using the
forceBrackets(String, String)
method.
openingBracket
- The opening bracket to use for the resulting string
("{"
is default, specify null
to omit this bracket).closingBracket
- The closing bracket to use for the resulting string
("}"
is default, specify null
to omit this bracket).forceBrackets(String, String)
public ToStringBuilder appendSuper(CharSequence superToStringResult)
toString()
result to this
builder. This could be from a superclass toString result for example.
For this parsing the builder will use the configured brackets settings to attempt to find the appended properties.
When in control of the superclass, it may be better to 'design it for extension' by letting it return the
non-finalized builder in a protected method so it can be appended to by a subclass without having to parse the
end-result.
protected ToStringBuilder toStringBuilder() {
return super.toStringBuilder().append("fieldname", "Field value");
}
superToStringResult
- The toString result from the superclass from which the appended properties will
be extracted if possible.public ToStringBuilder append(CharSequence name, Object value)
name
- The name of the property being added (optional).value
- The value of the property being added.includeNulls()
protected void appendValue(Object value)
This method gets called after a decision was made to append the value, so it will not be called in vain.
The method does not need to take separators or field names into account. That has all been taken care of
before this method call by append(CharSequence, Object)
. Therefore it is not normally necessary to call
this method by yourself.
Some specifics on how objects are rendered by default:
null
: <null>
.CharSequence
: "value"
.Object
: value.toString()
, except:reflected
String representation is used.value
- The property value to be added to this builder.public ToStringBuilder append(Object value)
value
- The field value to be added to this builder.append(CharSequence, Object)
public ToStringBuilder append(CharSequence value)
Appendable.append(CharSequence)
, delegated to append(Object)
to add a field value without prefixing it with a field name.append
in interface Appendable
value
- The field value to be added to this builder.append(CharSequence, Object)
public ToStringBuilder append(CharSequence csq, int start, int end)
Appendable.append(CharSequence, int, int)
to append a partial value to this builder
without prefixing it with a field name.append
in interface Appendable
csq
- The sequence to be partially appended to this builder.start
- The start index, inclusive.end
- The end index, exclusive.append(CharSequence, Object)
public ToStringBuilder append(char c)
Appendable.append(char)
to append a single character as a field value to this builder.append
in interface Appendable
c
- The character to be appended as a field value in this builder.append(CharSequence, Object)
public CharSequence subSequence(int start, int end)
CharSequence.subSequence(int, int)
that returns a subsequence of the current state of
this builder.subSequence
in interface CharSequence
start
- The start index, inclusive.end
- The end index, exclusive.protected boolean hasContent()
public String toString()
toString
in interface CharSequence
toString
in class Object
public int length()
length
in interface CharSequence
public char charAt(int index)
CharSequence.charAt(int)
that returns a single character from the current state of this
builder.charAt
in interface CharSequence
index
- The index of the requested character.index
in the current state of this builder.Copyright © 2016–2020 Talsma ICT. All rights reserved.