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.SerializableControlsStringformatting 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, the
StandardToStringStyleclass 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
booleantolongtoObjecttoint[]) 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 ToStringStyleDEFAULT_STYLEThe default toString style.static ToStringStyleJSON_STYLEThe JSON toString style.static ToStringStyleMULTI_LINE_STYLEThe multi line toString style.static ToStringStyleNO_CLASS_NAME_STYLEThe no class name toString style.static ToStringStyleNO_FIELD_NAMES_STYLEThe no field names toString style.static ToStringStyleSHORT_PREFIX_STYLEThe short prefix toString style.static ToStringStyleSIMPLE_STYLEThe simple toString style. 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, boolean value)Appends to thetoStringabooleanvalue.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, boolean[] array, java.lang.Boolean fullDetail)Appends to thetoStringabooleanarray.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, byte value)Appends to thetoStringabytevalue.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, byte[] array, java.lang.Boolean fullDetail)Appends to thetoStringabytearray.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, char value)Appends to thetoStringacharvalue.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, char[] array, java.lang.Boolean fullDetail)Appends to thetoStringachararray.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, double value)Appends to thetoStringadoublevalue.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, double[] array, java.lang.Boolean fullDetail)Appends to thetoStringadoublearray.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, float value)Appends to thetoStringafloatvalue.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, float[] array, java.lang.Boolean fullDetail)Appends to thetoStringafloatarray.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, int value)Appends to thetoStringanintvalue.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, int[] array, java.lang.Boolean fullDetail)Appends to thetoStringanintarray.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, long value)Appends to thetoStringalongvalue.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, long[] array, java.lang.Boolean fullDetail)Appends to thetoStringalongarray.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, short value)Appends to thetoStringashortvalue.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, short[] array, java.lang.Boolean fullDetail)Appends to thetoStringashortarray.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object[] array, java.lang.Boolean fullDetail)Appends to thetoStringanObjectarray.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object value, java.lang.Boolean fullDetail)Appends to thetoStringanObjectvalue, printing the fulltoStringof theObjectpassed in.voidappendEnd(java.lang.StringBuffer buffer, java.lang.Object object)Appends to thetoStringthe end of data indicator.voidappendStart(java.lang.StringBuffer buffer, java.lang.Object object)Appends to thetoStringthe start of data indicator.voidappendSuper(java.lang.StringBuffer buffer, java.lang.String superToString)Appends to thetoStringthe superclass toString.voidappendToString(java.lang.StringBuffer buffer, java.lang.String toString)Appends to thetoStringanother toString.static java.util.Map<java.lang.Object,java.lang.Object>getRegistry()Returns the registry of objects being traversed by thereflectionToStringmethods in the current thread. 
 - 
 
- 
- 
Field Detail
- 
DEFAULT_STYLE
public static final ToStringStyle DEFAULT_STYLE
The default toString style. Using thePersonexample 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 thePersonexample 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 thePersonexample 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 thePersonexample 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 thePersonexample 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 thePersonexample 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 thePersonexample 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 anUnsupportedOperationExceptionif 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
- 
getRegistry
public static java.util.Map<java.lang.Object,java.lang.Object> getRegistry()
Returns the registry of objects being traversed by thereflectionToStringmethods in the current thread.- Returns:
 - Set the registry of objects being traversed
 
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, boolean value)Appends to thetoStringabooleanvalue.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namevalue- the value to add to thetoString
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, boolean[] array, java.lang.Boolean fullDetail)Appends to thetoStringabooleanarray.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namearray- the array to add to the toStringfullDetail-truefor detail,falsefor summary info,nullfor style decides
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, byte value)Appends to thetoStringabytevalue.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namevalue- the value to add to thetoString
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, byte[] array, java.lang.Boolean fullDetail)Appends to thetoStringabytearray.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info,nullfor style decides
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, char value)Appends to thetoStringacharvalue.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namevalue- the value to add to thetoString
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, char[] array, java.lang.Boolean fullDetail)Appends to thetoStringachararray.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info,nullfor style decides
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, double value)Appends to thetoStringadoublevalue.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namevalue- the value to add to thetoString
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, double[] array, java.lang.Boolean fullDetail)Appends to thetoStringadoublearray.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namearray- the array to add to the toStringfullDetail-truefor detail,falsefor summary info,nullfor style decides
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, float value)Appends to thetoStringafloatvalue.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namevalue- the value to add to thetoString
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, float[] array, java.lang.Boolean fullDetail)Appends to thetoStringafloatarray.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namearray- the array to add to the toStringfullDetail-truefor detail,falsefor summary info,nullfor style decides
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, int value)Appends to thetoStringanintvalue.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namevalue- the value to add to thetoString
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, int[] array, java.lang.Boolean fullDetail)Appends to thetoStringanintarray.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info,nullfor style decides
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, long value)Appends to the
toStringalongvalue.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namevalue- the value to add to thetoString
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, long[] array, java.lang.Boolean fullDetail)Appends to thetoStringalongarray.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info,nullfor style decides
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object value, java.lang.Boolean fullDetail)Appends to thetoStringanObjectvalue, printing the fulltoStringof theObjectpassed in.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namevalue- the value to add to thetoStringfullDetail-truefor detail,falsefor summary info,nullfor style decides
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object[] array, java.lang.Boolean fullDetail)Appends to thetoStringanObjectarray.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namearray- the array to add to the toStringfullDetail-truefor detail,falsefor summary info,nullfor style decides
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, short value)Appends to thetoStringashortvalue.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namevalue- the value to add to thetoString
 
- 
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, short[] array, java.lang.Boolean fullDetail)Appends to thetoStringashortarray.- Parameters:
 buffer- theStringBufferto populatefieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info,nullfor style decides
 
- 
appendEnd
public void appendEnd(java.lang.StringBuffer buffer, java.lang.Object object)Appends to thetoStringthe end of data indicator.- Parameters:
 buffer- theStringBufferto populateobject- theObjectto build atoStringfor.
 
- 
appendStart
public void appendStart(java.lang.StringBuffer buffer, java.lang.Object object)Appends to thetoStringthe start of data indicator.- Parameters:
 buffer- theStringBufferto populateobject- theObjectto build atoStringfor
 
- 
appendSuper
public void appendSuper(java.lang.StringBuffer buffer, java.lang.String superToString)Appends to thetoStringthe superclass toString.NOTE: It assumes that the toString has been created from the same ToStringStyle.
A
nullsuperToStringis ignored.- Parameters:
 buffer- theStringBufferto populatesuperToString- thesuper.toString()- Since:
 - 2.0
 
 
- 
appendToString
public void appendToString(java.lang.StringBuffer buffer, java.lang.String toString)Appends to thetoStringanother toString.NOTE: It assumes that the toString has been created from the same ToStringStyle.
A
nulltoStringis ignored.- Parameters:
 buffer- theStringBufferto populatetoString- the additionaltoString- Since:
 - 2.0
 
 
 - 
 
 -