Class ToStringStyle

java.lang.Object
org.apache.commons.lang3.builder.ToStringStyle
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
RecursiveToStringStyle, StandardToStringStyle

public abstract class ToStringStyle extends Object implements Serializable
Controls 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);
   }
 }
 
Since:
1.0
See Also:
  • Field Details

    • DEFAULT_STYLE

      public static final ToStringStyle DEFAULT_STYLE
      The default toString style. Using the Person example from ToStringBuilder, 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 the Person example from ToStringBuilder, 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 the Person example from ToStringBuilder, 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 the Person example from ToStringBuilder, 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 the Person example from ToStringBuilder, 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 the Person example from ToStringBuilder, 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 the 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.
      Since:
      3.4
      See Also:
  • Method Details

    • getRegistry

      public static Map<Object,Object> getRegistry()
      Returns the registry of objects being traversed by the reflectionToString methods in the current thread.
      Returns:
      Set the registry of objects being traversed
    • append

      public void append(StringBuffer buffer, String fieldName, boolean value)
      Appends to the toString a boolean value.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      value - the value to add to the toString
    • append

      public void append(StringBuffer buffer, String fieldName, boolean[] array, Boolean fullDetail)
      Appends to the toString a boolean array.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      array - the array to add to the toString
      fullDetail - true for detail, false for summary info, null for style decides
    • append

      public void append(StringBuffer buffer, String fieldName, byte value)
      Appends to the toString a byte value.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      value - the value to add to the toString
    • append

      public void append(StringBuffer buffer, String fieldName, byte[] array, Boolean fullDetail)
      Appends to the toString a byte array.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      array - the array to add to the toString
      fullDetail - true for detail, false for summary info, null for style decides
    • append

      public void append(StringBuffer buffer, String fieldName, char value)
      Appends to the toString a char value.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      value - the value to add to the toString
    • append

      public void append(StringBuffer buffer, String fieldName, char[] array, Boolean fullDetail)
      Appends to the toString a char array.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      array - the array to add to the toString
      fullDetail - true for detail, false for summary info, null for style decides
    • append

      public void append(StringBuffer buffer, String fieldName, double value)
      Appends to the toString a double value.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      value - the value to add to the toString
    • append

      public void append(StringBuffer buffer, String fieldName, double[] array, Boolean fullDetail)
      Appends to the toString a double array.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      array - the array to add to the toString
      fullDetail - true for detail, false for summary info, null for style decides
    • append

      public void append(StringBuffer buffer, String fieldName, float value)
      Appends to the toString a float value.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      value - the value to add to the toString
    • append

      public void append(StringBuffer buffer, String fieldName, float[] array, Boolean fullDetail)
      Appends to the toString a float array.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      array - the array to add to the toString
      fullDetail - true for detail, false for summary info, null for style decides
    • append

      public void append(StringBuffer buffer, String fieldName, int value)
      Appends to the toString an int value.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      value - the value to add to the toString
    • append

      public void append(StringBuffer buffer, String fieldName, int[] array, Boolean fullDetail)
      Appends to the toString an int array.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      array - the array to add to the toString
      fullDetail - true for detail, false for summary info, null for style decides
    • append

      public void append(StringBuffer buffer, String fieldName, long value)

      Appends to the toString a long value.

      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      value - the value to add to the toString
    • append

      public void append(StringBuffer buffer, String fieldName, long[] array, Boolean fullDetail)
      Appends to the toString a long array.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      array - the array to add to the toString
      fullDetail - true for detail, false for summary info, null for style decides
    • append

      public void append(StringBuffer buffer, String fieldName, Object value, Boolean fullDetail)
      Appends to the toString an Object value, printing the full toString of the Object passed in.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      value - the value to add to the toString
      fullDetail - true for detail, false for summary info, null for style decides
    • append

      public void append(StringBuffer buffer, String fieldName, Object[] array, Boolean fullDetail)
      Appends to the toString an Object array.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      array - the array to add to the toString
      fullDetail - true for detail, false for summary info, null for style decides
    • append

      public void append(StringBuffer buffer, String fieldName, short value)
      Appends to the toString a short value.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      value - the value to add to the toString
    • append

      public void append(StringBuffer buffer, String fieldName, short[] array, Boolean fullDetail)
      Appends to the toString a short array.
      Parameters:
      buffer - the StringBuffer to populate
      fieldName - the field name
      array - the array to add to the toString
      fullDetail - true for detail, false for summary info, null for style decides
    • appendEnd

      public void appendEnd(StringBuffer buffer, Object object)
      Appends to the toString the end of data indicator.
      Parameters:
      buffer - the StringBuffer to populate
      object - the Object to build a toString for.
    • appendStart

      public void appendStart(StringBuffer buffer, Object object)
      Appends to the toString the start of data indicator.
      Parameters:
      buffer - the StringBuffer to populate
      object - the Object to build a toString for
    • appendSuper

      public void appendSuper(StringBuffer buffer, String superToString)
      Appends 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 - the StringBuffer to populate
      superToString - the super.toString()
      Since:
      2.0
    • appendToString

      public void appendToString(StringBuffer buffer, String toString)
      Appends 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 - the StringBuffer to populate
      toString - the additional toString
      Since:
      2.0