public abstract class ToStringStyle
extends java.lang.Object
implements java.io.Serializable
String
formatting for ToStringBuilder
.
The main public interface is always via ToStringBuilder
.
These classes are intended to be used as singletons.
There is no need to instantiate a new style each time. A program
will generally use one of the predefined constants on this class.
Alternatively, the StandardToStringStyle
class can be used
to set the individual settings. Thus most styles can be achieved
without subclassing.
If required, a subclass can override as many or as few of the
methods as it requires. Each object type (from boolean
to long
to Object
to int[]
) has
its own methods to output it. Most have two versions, detail and summary.
For example, the detail version of the array based methods will output the whole array, whereas the summary method will just output the array length.
If you want to format the output of certain objects, such as dates, you must create a subclass and override a method.
public class MyStyle extends ToStringStyle { protected void appendDetail(StringBuffer buffer, String fieldName, Object value) { if (value instanceof Date) { value = new SimpleDateFormat("yyyy-MM-dd").format(value); } buffer.append(value); } }
Modifier and Type | Field and Description |
---|---|
static ToStringStyle |
DEFAULT_STYLE
The default toString style.
|
static ToStringStyle |
JSON_STYLE
The JSON toString style.
|
static ToStringStyle |
MULTI_LINE_STYLE
The multi line toString style.
|
static ToStringStyle |
NO_CLASS_NAME_STYLE
The no class name toString style.
|
static ToStringStyle |
NO_FIELD_NAMES_STYLE
The no field names toString style.
|
static ToStringStyle |
SHORT_PREFIX_STYLE
The short prefix toString style.
|
static ToStringStyle |
SIMPLE_STYLE
The simple toString style.
|
Modifier and Type | Method and Description |
---|---|
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
boolean value)
Appends to the
toString a boolean
value. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
boolean[] array,
java.lang.Boolean fullDetail)
Appends to the
toString a boolean
array. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
byte value)
Appends to the
toString a byte
value. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
byte[] array,
java.lang.Boolean fullDetail)
Appends to the
toString a byte
array. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
char value)
Appends to the
toString a char
value. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
char[] array,
java.lang.Boolean fullDetail)
Appends to the
toString a char
array. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
double value)
Appends to the
toString a double
value. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
double[] array,
java.lang.Boolean fullDetail)
Appends to the
toString a double
array. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
float value)
Appends to the
toString a float
value. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
float[] array,
java.lang.Boolean fullDetail)
Appends to the
toString a float
array. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
int value)
Appends to the
toString an int
value. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
int[] array,
java.lang.Boolean fullDetail)
Appends to the
toString an int
array. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
long value)
Appends to the
toString a long
value. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
long[] array,
java.lang.Boolean fullDetail)
Appends to the
toString a long
array. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
java.lang.Object[] array,
java.lang.Boolean fullDetail)
Appends to the
toString an Object
array. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
java.lang.Object value,
java.lang.Boolean fullDetail)
Appends to the
toString an Object
value, printing the full toString of the
Object passed in. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
short value)
Appends to the
toString a short
value. |
void |
append(java.lang.StringBuffer buffer,
java.lang.String fieldName,
short[] array,
java.lang.Boolean fullDetail)
Appends to the
toString a short
array. |
void |
appendEnd(java.lang.StringBuffer buffer,
java.lang.Object object)
Appends to the
toString the end of data indicator. |
void |
appendStart(java.lang.StringBuffer buffer,
java.lang.Object object)
Appends to the
toString the start of data indicator. |
void |
appendSuper(java.lang.StringBuffer buffer,
java.lang.String superToString)
Appends to the
toString the superclass toString. |
void |
appendToString(java.lang.StringBuffer buffer,
java.lang.String toString)
Appends to the
toString another toString. |
static java.util.Map<java.lang.Object,java.lang.Object> |
getRegistry()
Returns the registry of objects being traversed by the
reflectionToString
methods in the current thread. |
public static final ToStringStyle DEFAULT_STYLE
Person
example from ToStringBuilder
, the output would look like this:
Person@182f0db[name=John Doe,age=33,smoker=false]
public static final ToStringStyle MULTI_LINE_STYLE
Person
example from ToStringBuilder
, the output would look like this:
Person@182f0db[ name=John Doe age=33 smoker=false ]
public static final ToStringStyle NO_FIELD_NAMES_STYLE
Person
example from ToStringBuilder
, the output
would look like this:
Person@182f0db[John Doe,33,false]
public static final ToStringStyle SHORT_PREFIX_STYLE
Person
example
from ToStringBuilder
, the output would look like this:
Person[name=John Doe,age=33,smoker=false]
public static final ToStringStyle SIMPLE_STYLE
Person
example from ToStringBuilder
, the output would look like this:
John Doe,33,false
public static final ToStringStyle NO_CLASS_NAME_STYLE
Person
example from ToStringBuilder
, the output would look like this:
[name=John Doe,age=33,smoker=false]
public static final ToStringStyle JSON_STYLE
Person
example from
ToStringBuilder
, the output would look like this:
{"name": "John Doe", "age": 33, "smoker": true}Note: Since field names are mandatory in JSON, this ToStringStyle will throw an
UnsupportedOperationException
if no
field name is passed in while appending. Furthermore This ToStringStyle
will only generate valid JSON if referenced objects also produce JSON
when calling toString()
on them.public static java.util.Map<java.lang.Object,java.lang.Object> getRegistry()
reflectionToString
methods in the current thread.public void appendSuper(java.lang.StringBuffer buffer, java.lang.String superToString)
toString
the superclass toString.
NOTE: It assumes that the toString has been created from the same ToStringStyle.
A null
superToString
is ignored.
buffer
- the StringBuffer
to populatesuperToString
- the super.toString()
public void appendToString(java.lang.StringBuffer buffer, java.lang.String toString)
toString
another toString.
NOTE: It assumes that the toString has been created from the same ToStringStyle.
A null
toString
is ignored.
buffer
- the StringBuffer
to populatetoString
- the additional toString
public void appendStart(java.lang.StringBuffer buffer, java.lang.Object object)
toString
the start of data indicator.buffer
- the StringBuffer
to populateobject
- the Object
to build a toString
forpublic void appendEnd(java.lang.StringBuffer buffer, java.lang.Object object)
toString
the end of data indicator.buffer
- the StringBuffer
to populateobject
- the Object
to build a
toString
for.public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object value, java.lang.Boolean fullDetail)
toString
an Object
value, printing the full toString
of the
Object
passed in.buffer
- the StringBuffer
to populatefieldName
- the field namevalue
- the value to add to the toString
fullDetail
- true
for detail, false
for summary info, null
for style decidespublic void append(java.lang.StringBuffer buffer, java.lang.String fieldName, long value)
Appends to the toString
a long
value.
buffer
- the StringBuffer
to populatefieldName
- the field namevalue
- the value to add to the toString
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, int value)
toString
an int
value.buffer
- the StringBuffer
to populatefieldName
- the field namevalue
- the value to add to the toString
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, short value)
toString
a short
value.buffer
- the StringBuffer
to populatefieldName
- the field namevalue
- the value to add to the toString
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, byte value)
toString
a byte
value.buffer
- the StringBuffer
to populatefieldName
- the field namevalue
- the value to add to the toString
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, char value)
toString
a char
value.buffer
- the StringBuffer
to populatefieldName
- the field namevalue
- the value to add to the toString
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, double value)
toString
a double
value.buffer
- the StringBuffer
to populatefieldName
- the field namevalue
- the value to add to the toString
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, float value)
toString
a float
value.buffer
- the StringBuffer
to populatefieldName
- the field namevalue
- the value to add to the toString
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, boolean value)
toString
a boolean
value.buffer
- the StringBuffer
to populatefieldName
- the field namevalue
- the value to add to the toString
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object[] array, java.lang.Boolean fullDetail)
toString
an Object
array.buffer
- the StringBuffer
to populatefieldName
- the field namearray
- the array to add to the toStringfullDetail
- true
for detail, false
for summary info, null
for style decidespublic void append(java.lang.StringBuffer buffer, java.lang.String fieldName, long[] array, java.lang.Boolean fullDetail)
toString
a long
array.buffer
- the StringBuffer
to populatefieldName
- the field namearray
- the array to add to the toString
fullDetail
- true
for detail, false
for summary info, null
for style decidespublic void append(java.lang.StringBuffer buffer, java.lang.String fieldName, int[] array, java.lang.Boolean fullDetail)
toString
an int
array.buffer
- the StringBuffer
to populatefieldName
- the field namearray
- the array to add to the toString
fullDetail
- true
for detail, false
for summary info, null
for style decidespublic void append(java.lang.StringBuffer buffer, java.lang.String fieldName, short[] array, java.lang.Boolean fullDetail)
toString
a short
array.buffer
- the StringBuffer
to populatefieldName
- the field namearray
- the array to add to the toString
fullDetail
- true
for detail, false
for summary info, null
for style decidespublic void append(java.lang.StringBuffer buffer, java.lang.String fieldName, byte[] array, java.lang.Boolean fullDetail)
toString
a byte
array.buffer
- the StringBuffer
to populatefieldName
- the field namearray
- the array to add to the toString
fullDetail
- true
for detail, false
for summary info, null
for style decidespublic void append(java.lang.StringBuffer buffer, java.lang.String fieldName, char[] array, java.lang.Boolean fullDetail)
toString
a char
array.buffer
- the StringBuffer
to populatefieldName
- the field namearray
- the array to add to the toString
fullDetail
- true
for detail, false
for summary info, null
for style decidespublic void append(java.lang.StringBuffer buffer, java.lang.String fieldName, double[] array, java.lang.Boolean fullDetail)
toString
a double
array.buffer
- the StringBuffer
to populatefieldName
- the field namearray
- the array to add to the toStringfullDetail
- true
for detail, false
for summary info, null
for style decidespublic void append(java.lang.StringBuffer buffer, java.lang.String fieldName, float[] array, java.lang.Boolean fullDetail)
toString
a float
array.buffer
- the StringBuffer
to populatefieldName
- the field namearray
- the array to add to the toStringfullDetail
- true
for detail, false
for summary info, null
for style decidespublic void append(java.lang.StringBuffer buffer, java.lang.String fieldName, boolean[] array, java.lang.Boolean fullDetail)
toString
a boolean
array.buffer
- the StringBuffer
to populatefieldName
- the field namearray
- the array to add to the toStringfullDetail
- true
for detail, false
for summary info, null
for style decidesCopyright © 2010 - 2023 Adobe. All Rights Reserved