Class 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 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:
    Serialized Form
    • 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 the toString a boolean value.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, boolean[] array, java.lang.Boolean fullDetail)
      Append to the toString a boolean array.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, byte value)
      Append to the toString a byte value.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, byte[] array, java.lang.Boolean fullDetail)
      Append to the toString a byte array.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, char value)
      Append to the toString a char value.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, char[] array, java.lang.Boolean fullDetail)
      Append to the toString a char array.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, double value)
      Append to the toString a double value.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, double[] array, java.lang.Boolean fullDetail)
      Append to the toString a double array.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, float value)
      Append to the toString a float value.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, float[] array, java.lang.Boolean fullDetail)
      Append to the toString a float array.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, int value)
      Append to the toString an int value.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, int[] array, java.lang.Boolean fullDetail)
      Append to the toString an int array.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, long value)
      Append to the toString a long value.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, long[] array, java.lang.Boolean fullDetail)
      Append to the toString a long array.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, short value)
      Append to the toString a short value.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, short[] array, java.lang.Boolean fullDetail)
      Append to the toString a short array.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object[] array, java.lang.Boolean fullDetail)
      Append to the toString an Object array.
      void append​(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object value, java.lang.Boolean fullDetail)
      Append to the toString an Object value, printing the full toString of the Object passed in.
      void appendEnd​(java.lang.StringBuffer buffer, java.lang.Object object)
      Append to the toString the end of data indicator.
      void appendStart​(java.lang.StringBuffer buffer, java.lang.Object object)
      Append to the toString the start of data indicator.
      void appendSuper​(java.lang.StringBuffer buffer, java.lang.String superToString)
      Append to the toString the superclass toString.
      void appendToString​(java.lang.StringBuffer buffer, java.lang.String toString)
      Append to the toString another toString.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • 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:
        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 - the StringBuffer to populate
        superToString - the super.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 - the StringBuffer to populate
        toString - the additional toString
        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 - the StringBuffer to populate
        object - the Object to build a toString for
      • appendEnd

        public void appendEnd​(java.lang.StringBuffer buffer,
                              java.lang.Object object)

        Append to the toString the end of data indicator.

        Parameters:
        buffer - the StringBuffer to populate
        object - the Object to build a toString 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 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​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           long value)

        Append 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​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           int value)

        Append 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​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           short value)

        Append 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​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           byte value)

        Append 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​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           char value)

        Append 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​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           double value)

        Append 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​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           float value)

        Append 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​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           boolean value)

        Append 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​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           java.lang.Object[] array,
                           java.lang.Boolean fullDetail)

        Append 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​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           long[] array,
                           java.lang.Boolean fullDetail)

        Append 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​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           int[] array,
                           java.lang.Boolean fullDetail)

        Append 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​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           short[] array,
                           java.lang.Boolean fullDetail)

        Append 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
      • append

        public void append​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           byte[] array,
                           java.lang.Boolean fullDetail)

        Append 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​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           char[] array,
                           java.lang.Boolean fullDetail)

        Append 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​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           double[] array,
                           java.lang.Boolean fullDetail)

        Append 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​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           float[] array,
                           java.lang.Boolean fullDetail)

        Append 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​(java.lang.StringBuffer buffer,
                           java.lang.String fieldName,
                           boolean[] array,
                           java.lang.Boolean fullDetail)

        Append 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