Class ToStringStyle
- java.lang.Object
-
- org.apache.commons.lang3.builder.ToStringStyle
-
- All Implemented Interfaces:
java.io.Serializable
- Direct Known Subclasses:
RecursiveToStringStyle
,StandardToStringStyle
public abstract class ToStringStyle extends java.lang.Object implements java.io.Serializable
Controls
String
formatting forToStringBuilder
. The main public interface is always viaToStringBuilder
.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, theStandardToStringStyle
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
tolong
toObject
toint[]
) 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); } }
- Since:
- 1.0
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, boolean value)
Append to thetoString
aboolean
value.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, boolean[] array, java.lang.Boolean fullDetail)
Append to thetoString
aboolean
array.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, byte value)
Append to thetoString
abyte
value.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, byte[] array, java.lang.Boolean fullDetail)
Append to thetoString
abyte
array.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, char value)
Append to thetoString
achar
value.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, char[] array, java.lang.Boolean fullDetail)
Append to thetoString
achar
array.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, double value)
Append to thetoString
adouble
value.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, double[] array, java.lang.Boolean fullDetail)
Append to thetoString
adouble
array.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, float value)
Append to thetoString
afloat
value.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, float[] array, java.lang.Boolean fullDetail)
Append to thetoString
afloat
array.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, int value)
Append to thetoString
anint
value.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, int[] array, java.lang.Boolean fullDetail)
Append to thetoString
anint
array.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, long value)
Append to thetoString
along
value.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, long[] array, java.lang.Boolean fullDetail)
Append to thetoString
along
array.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, short value)
Append to thetoString
ashort
value.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, short[] array, java.lang.Boolean fullDetail)
Append to thetoString
ashort
array.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object[] array, java.lang.Boolean fullDetail)
Append to thetoString
anObject
array.void
append(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object value, java.lang.Boolean fullDetail)
Append to thetoString
anObject
value, printing the fulltoString
of theObject
passed in.void
appendEnd(java.lang.StringBuffer buffer, java.lang.Object object)
Append to thetoString
the end of data indicator.void
appendStart(java.lang.StringBuffer buffer, java.lang.Object object)
Append to thetoString
the start of data indicator.void
appendSuper(java.lang.StringBuffer buffer, java.lang.String superToString)
Append to thetoString
the superclass toString.void
appendToString(java.lang.StringBuffer buffer, java.lang.String toString)
Append to thetoString
another toString.
-
-
-
Field Detail
-
DEFAULT_STYLE
public static final ToStringStyle DEFAULT_STYLE
The default toString style. Using thePerson
example fromToStringBuilder
, the output would look like this:Person@182f0db[name=John Doe,age=33,smoker=false]
-
MULTI_LINE_STYLE
public static final ToStringStyle MULTI_LINE_STYLE
The multi line toString style. Using thePerson
example fromToStringBuilder
, the output would look like this:Person@182f0db[ name=John Doe age=33 smoker=false ]
-
NO_FIELD_NAMES_STYLE
public static final ToStringStyle NO_FIELD_NAMES_STYLE
The no field names toString style. Using thePerson
example fromToStringBuilder
, the output would look like this:Person@182f0db[John Doe,33,false]
-
SHORT_PREFIX_STYLE
public static final ToStringStyle SHORT_PREFIX_STYLE
The short prefix toString style. Using thePerson
example fromToStringBuilder
, the output would look like this:Person[name=John Doe,age=33,smoker=false]
- Since:
- 2.1
-
SIMPLE_STYLE
public static final ToStringStyle SIMPLE_STYLE
The simple toString style. Using thePerson
example fromToStringBuilder
, the output would look like this:John Doe,33,false
-
NO_CLASS_NAME_STYLE
public static final ToStringStyle NO_CLASS_NAME_STYLE
The no class name toString style. Using thePerson
example fromToStringBuilder
, the output would look like this:[name=John Doe,age=33,smoker=false]
- Since:
- 3.4
-
JSON_STYLE
public static final ToStringStyle JSON_STYLE
The JSON toString style. Using thePerson
example fromToStringBuilder
, 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 anUnsupportedOperationException
if no field name is passed in while appending. Furthermore This ToStringStyle will only generate valid JSON if referenced objects also produce JSON when callingtoString()
on them.- Since:
- 3.4
- See Also:
- json.org
-
-
Method Detail
-
appendSuper
public void appendSuper(java.lang.StringBuffer buffer, java.lang.String superToString)
Append to the
toString
the superclass toString.NOTE: It assumes that the toString has been created from the same ToStringStyle.
A
null
superToString
is ignored.- Parameters:
buffer
- theStringBuffer
to populatesuperToString
- thesuper.toString()
- Since:
- 2.0
-
appendToString
public void appendToString(java.lang.StringBuffer buffer, java.lang.String toString)
Append to the
toString
another toString.NOTE: It assumes that the toString has been created from the same ToStringStyle.
A
null
toString
is ignored.- Parameters:
buffer
- theStringBuffer
to populatetoString
- the additionaltoString
- Since:
- 2.0
-
appendStart
public void appendStart(java.lang.StringBuffer buffer, java.lang.Object object)
Append to the
toString
the start of data indicator.- Parameters:
buffer
- theStringBuffer
to populateobject
- theObject
to build atoString
for
-
appendEnd
public void appendEnd(java.lang.StringBuffer buffer, java.lang.Object object)
Append to the
toString
the end of data indicator.- Parameters:
buffer
- theStringBuffer
to populateobject
- theObject
to build atoString
for.
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object value, java.lang.Boolean fullDetail)
Append to the
toString
anObject
value, printing the fulltoString
of theObject
passed in.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namevalue
- the value to add to thetoString
fullDetail
-true
for detail,false
for summary info,null
for style decides
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, long value)
Append to the
toString
along
value.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namevalue
- the value to add to thetoString
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, int value)
Append to the
toString
anint
value.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namevalue
- the value to add to thetoString
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, short value)
Append to the
toString
ashort
value.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namevalue
- the value to add to thetoString
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, byte value)
Append to the
toString
abyte
value.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namevalue
- the value to add to thetoString
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, char value)
Append to the
toString
achar
value.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namevalue
- the value to add to thetoString
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, double value)
Append to the
toString
adouble
value.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namevalue
- the value to add to thetoString
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, float value)
Append to the
toString
afloat
value.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namevalue
- the value to add to thetoString
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, boolean value)
Append to the
toString
aboolean
value.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namevalue
- the value to add to thetoString
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object[] array, java.lang.Boolean fullDetail)
Append to the
toString
anObject
array.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namearray
- the array to add to the toStringfullDetail
-true
for detail,false
for summary info,null
for style decides
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, long[] array, java.lang.Boolean fullDetail)
Append to the
toString
along
array.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namearray
- the array to add to thetoString
fullDetail
-true
for detail,false
for summary info,null
for style decides
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, int[] array, java.lang.Boolean fullDetail)
Append to the
toString
anint
array.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namearray
- the array to add to thetoString
fullDetail
-true
for detail,false
for summary info,null
for style decides
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, short[] array, java.lang.Boolean fullDetail)
Append to the
toString
ashort
array.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namearray
- the array to add to thetoString
fullDetail
-true
for detail,false
for summary info,null
for style decides
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, byte[] array, java.lang.Boolean fullDetail)
Append to the
toString
abyte
array.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namearray
- the array to add to thetoString
fullDetail
-true
for detail,false
for summary info,null
for style decides
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, char[] array, java.lang.Boolean fullDetail)
Append to the
toString
achar
array.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namearray
- the array to add to thetoString
fullDetail
-true
for detail,false
for summary info,null
for style decides
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, double[] array, java.lang.Boolean fullDetail)
Append to the
toString
adouble
array.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namearray
- the array to add to the toStringfullDetail
-true
for detail,false
for summary info,null
for style decides
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, float[] array, java.lang.Boolean fullDetail)
Append to the
toString
afloat
array.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namearray
- the array to add to the toStringfullDetail
-true
for detail,false
for summary info,null
for style decides
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, boolean[] array, java.lang.Boolean fullDetail)
Append to the
toString
aboolean
array.- Parameters:
buffer
- theStringBuffer
to populatefieldName
- the field namearray
- the array to add to the toStringfullDetail
-true
for detail,false
for summary info,null
for style decides
-
-