ToStringBuilder.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.lang3.builder;

  18. import java.util.Objects;

  19. import org.apache.commons.lang3.ObjectUtils;

  20. /**
  21.  * Assists in implementing {@link Object#toString()} methods.
  22.  *
  23.  * <p>This class enables a good and consistent {@code toString()} to be built for any
  24.  * class or object. This class aims to simplify the process by:</p>
  25.  * <ul>
  26.  *  <li>allowing field names</li>
  27.  *  <li>handling all types consistently</li>
  28.  *  <li>handling nulls consistently</li>
  29.  *  <li>outputting arrays and multi-dimensional arrays</li>
  30.  *  <li>enabling the detail level to be controlled for Objects and Collections</li>
  31.  *  <li>handling class hierarchies</li>
  32.  * </ul>
  33.  *
  34.  * <p>To use this class write code as follows:</p>
  35.  *
  36.  * <pre>
  37.  * public class Person {
  38.  *   String name;
  39.  *   int age;
  40.  *   boolean smoker;
  41.  *
  42.  *   ...
  43.  *
  44.  *   public String toString() {
  45.  *     return new ToStringBuilder(this).
  46.  *       append("name", name).
  47.  *       append("age", age).
  48.  *       append("smoker", smoker).
  49.  *       toString();
  50.  *   }
  51.  * }
  52.  * </pre>
  53.  *
  54.  * <p>This will produce a toString of the format:
  55.  * {@code Person@7f54[name=Stephen,age=29,smoker=false]}</p>
  56.  *
  57.  * <p>To add the superclass {@code toString}, use {@link #appendSuper}.
  58.  * To append the {@code toString} from an object that is delegated
  59.  * to (or any other object), use {@link #appendToString}.</p>
  60.  *
  61.  * <p>Alternatively, there is a method that uses reflection to determine
  62.  * the fields to test. Because these fields are usually private, the method,
  63.  * {@code reflectionToString}, uses {@code AccessibleObject.setAccessible} to
  64.  * change the visibility of the fields. This will fail under a security manager,
  65.  * unless the appropriate permissions are set up correctly. It is also
  66.  * slower than testing explicitly.</p>
  67.  *
  68.  * <p>A typical invocation for this method would look like:</p>
  69.  *
  70.  * <pre>
  71.  * public String toString() {
  72.  *   return ToStringBuilder.reflectionToString(this);
  73.  * }
  74.  * </pre>
  75.  *
  76.  * <p>You can also use the builder to debug 3rd party objects:</p>
  77.  *
  78.  * <pre>
  79.  * System.out.println("An object: " + ToStringBuilder.reflectionToString(anObject));
  80.  * </pre>
  81.  *
  82.  * <p>The exact format of the {@code toString} is determined by
  83.  * the {@link ToStringStyle} passed into the constructor.</p>
  84.  *
  85.  * @since 1.0
  86.  */
  87. public class ToStringBuilder implements Builder<String> {

  88.     /**
  89.      * The default style of output to use, not null.
  90.      */
  91.     private static volatile ToStringStyle defaultStyle = ToStringStyle.DEFAULT_STYLE;

  92.     /**
  93.      * Gets the default {@link ToStringStyle} to use.
  94.      *
  95.      * <p>This method gets a singleton default value, typically for the whole JVM.
  96.      * Changing this default should generally only be done during application startup.
  97.      * It is recommended to pass a {@link ToStringStyle} to the constructor instead
  98.      * of using this global default.</p>
  99.      *
  100.      * <p>This method can be used from multiple threads.
  101.      * Internally, a {@code volatile} variable is used to provide the guarantee
  102.      * that the latest value set using {@link #setDefaultStyle} is the value returned.
  103.      * It is strongly recommended that the default style is only changed during application startup.</p>
  104.      *
  105.      * <p>One reason for changing the default could be to have a verbose style during
  106.      * development and a compact style in production.</p>
  107.      *
  108.      * @return the default {@link ToStringStyle}, never null
  109.      */
  110.     public static ToStringStyle getDefaultStyle() {
  111.         return defaultStyle;
  112.     }

  113.     /**
  114.      * Uses {@link ReflectionToStringBuilder} to generate a
  115.      * {@code toString} for the specified object.
  116.      *
  117.      * @param object  the Object to be output
  118.      * @return the String result
  119.      * @see ReflectionToStringBuilder#toString(Object)
  120.      */
  121.     public static String reflectionToString(final Object object) {
  122.         return ReflectionToStringBuilder.toString(object);
  123.     }

  124.     /**
  125.      * Uses {@link ReflectionToStringBuilder} to generate a
  126.      * {@code toString} for the specified object.
  127.      *
  128.      * @param object  the Object to be output
  129.      * @param style  the style of the {@code toString} to create, may be {@code null}
  130.      * @return the String result
  131.      * @see ReflectionToStringBuilder#toString(Object,ToStringStyle)
  132.      */
  133.     public static String reflectionToString(final Object object, final ToStringStyle style) {
  134.         return ReflectionToStringBuilder.toString(object, style);
  135.     }

  136.     /**
  137.      * Uses {@link ReflectionToStringBuilder} to generate a
  138.      * {@code toString} for the specified object.
  139.      *
  140.      * @param object  the Object to be output
  141.      * @param style  the style of the {@code toString} to create, may be {@code null}
  142.      * @param outputTransients  whether to include transient fields
  143.      * @return the String result
  144.      * @see ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean)
  145.      */
  146.     public static String reflectionToString(final Object object, final ToStringStyle style, final boolean outputTransients) {
  147.         return ReflectionToStringBuilder.toString(object, style, outputTransients, false, null);
  148.     }

  149.     /**
  150.      * Uses {@link ReflectionToStringBuilder} to generate a
  151.      * {@code toString} for the specified object.
  152.      *
  153.      * @param <T> the type of the object
  154.      * @param object  the Object to be output
  155.      * @param style  the style of the {@code toString} to create, may be {@code null}
  156.      * @param outputTransients  whether to include transient fields
  157.      * @param reflectUpToClass  the superclass to reflect up to (inclusive), may be {@code null}
  158.      * @return the String result
  159.      * @see ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean,boolean,Class)
  160.      * @since 2.0
  161.      */
  162.     public static <T> String reflectionToString(
  163.         final T object,
  164.         final ToStringStyle style,
  165.         final boolean outputTransients,
  166.         final Class<? super T> reflectUpToClass) {
  167.         return ReflectionToStringBuilder.toString(object, style, outputTransients, false, reflectUpToClass);
  168.     }

  169.     /**
  170.      * Sets the default {@link ToStringStyle} to use.
  171.      *
  172.      * <p>This method sets a singleton default value, typically for the whole JVM.
  173.      * Changing this default should generally only be done during application startup.
  174.      * It is recommended to pass a {@link ToStringStyle} to the constructor instead
  175.      * of changing this global default.</p>
  176.      *
  177.      * <p>This method is not intended for use from multiple threads.
  178.      * Internally, a {@code volatile} variable is used to provide the guarantee
  179.      * that the latest value set is the value returned from {@link #getDefaultStyle}.</p>
  180.      *
  181.      * @param style  the default {@link ToStringStyle}
  182.      * @throws NullPointerException if the style is {@code null}
  183.      */
  184.     public static void setDefaultStyle(final ToStringStyle style) {
  185.         defaultStyle = Objects.requireNonNull(style, "style");
  186.     }

  187.     /**
  188.      * Current toString buffer, not null.
  189.      */
  190.     private final StringBuffer buffer;
  191.     /**
  192.      * The object being output, may be null.
  193.      */
  194.     private final Object object;
  195.     /**
  196.      * The style of output to use, not null.
  197.      */
  198.     private final ToStringStyle style;

  199.     /**
  200.      * Constructs a builder for the specified object using the default output style.
  201.      *
  202.      * <p>This default style is obtained from {@link #getDefaultStyle()}.</p>
  203.      *
  204.      * @param object  the Object to build a {@code toString} for, not recommended to be null
  205.      */
  206.     public ToStringBuilder(final Object object) {
  207.         this(object, null, null);
  208.     }

  209.     /**
  210.      * Constructs a builder for the specified object using the defined output style.
  211.      *
  212.      * <p>If the style is {@code null}, the default style is used.</p>
  213.      *
  214.      * @param object  the Object to build a {@code toString} for, not recommended to be null
  215.      * @param style  the style of the {@code toString} to create, null uses the default style
  216.      */
  217.     public ToStringBuilder(final Object object, final ToStringStyle style) {
  218.         this(object, style, null);
  219.     }

  220.     /**
  221.      * Constructs a builder for the specified object.
  222.      *
  223.      * <p>If the style is {@code null}, the default style is used.</p>
  224.      *
  225.      * <p>If the buffer is {@code null}, a new one is created.</p>
  226.      *
  227.      * @param object  the Object to build a {@code toString} for, not recommended to be null
  228.      * @param style  the style of the {@code toString} to create, null uses the default style
  229.      * @param buffer  the {@link StringBuffer} to populate, may be null
  230.      */
  231.     public ToStringBuilder(final Object object, ToStringStyle style, StringBuffer buffer) {
  232.         if (style == null) {
  233.             style = getDefaultStyle();
  234.         }
  235.         if (buffer == null) {
  236.             buffer = new StringBuffer(512);
  237.         }
  238.         this.buffer = buffer;
  239.         this.style = style;
  240.         this.object = object;

  241.         style.appendStart(buffer, object);
  242.     }

  243.     /**
  244.      * Append to the {@code toString} a {@code boolean}
  245.      * value.
  246.      *
  247.      * @param value  the value to add to the {@code toString}
  248.      * @return {@code this} instance.
  249.      */
  250.     public ToStringBuilder append(final boolean value) {
  251.         style.append(buffer, null, value);
  252.         return this;
  253.     }

  254.     /**
  255.      * Append to the {@code toString} a {@code boolean}
  256.      * array.
  257.      *
  258.      * @param array  the array to add to the {@code toString}
  259.      * @return {@code this} instance.
  260.      */
  261.     public ToStringBuilder append(final boolean[] array) {
  262.         style.append(buffer, null, array, null);
  263.         return this;
  264.     }

  265.     /**
  266.      * Append to the {@code toString} a {@code byte}
  267.      * value.
  268.      *
  269.      * @param value  the value to add to the {@code toString}
  270.      * @return {@code this} instance.
  271.      */
  272.     public ToStringBuilder append(final byte value) {
  273.         style.append(buffer, null, value);
  274.         return this;
  275.     }

  276.     /**
  277.      * Append to the {@code toString} a {@code byte}
  278.      * array.
  279.      *
  280.      * @param array  the array to add to the {@code toString}
  281.      * @return {@code this} instance.
  282.      */
  283.     public ToStringBuilder append(final byte[] array) {
  284.         style.append(buffer, null, array, null);
  285.         return this;
  286.     }

  287.     /**
  288.      * Append to the {@code toString} a {@code char}
  289.      * value.
  290.      *
  291.      * @param value  the value to add to the {@code toString}
  292.      * @return {@code this} instance.
  293.      */
  294.     public ToStringBuilder append(final char value) {
  295.         style.append(buffer, null, value);
  296.         return this;
  297.     }

  298.     /**
  299.      * Append to the {@code toString} a {@code char}
  300.      * array.
  301.      *
  302.      * @param array  the array to add to the {@code toString}
  303.      * @return {@code this} instance.
  304.      */
  305.     public ToStringBuilder append(final char[] array) {
  306.         style.append(buffer, null, array, null);
  307.         return this;
  308.     }

  309.     /**
  310.      * Append to the {@code toString} a {@code double}
  311.      * value.
  312.      *
  313.      * @param value  the value to add to the {@code toString}
  314.      * @return {@code this} instance.
  315.      */
  316.     public ToStringBuilder append(final double value) {
  317.         style.append(buffer, null, value);
  318.         return this;
  319.     }

  320.     /**
  321.      * Append to the {@code toString} a {@code double}
  322.      * array.
  323.      *
  324.      * @param array  the array to add to the {@code toString}
  325.      * @return {@code this} instance.
  326.      */
  327.     public ToStringBuilder append(final double[] array) {
  328.         style.append(buffer, null, array, null);
  329.         return this;
  330.     }

  331.     /**
  332.      * Append to the {@code toString} a {@code float}
  333.      * value.
  334.      *
  335.      * @param value  the value to add to the {@code toString}
  336.      * @return {@code this} instance.
  337.      */
  338.     public ToStringBuilder append(final float value) {
  339.         style.append(buffer, null, value);
  340.         return this;
  341.     }

  342.     /**
  343.      * Append to the {@code toString} a {@code float}
  344.      * array.
  345.      *
  346.      * @param array  the array to add to the {@code toString}
  347.      * @return {@code this} instance.
  348.      */
  349.     public ToStringBuilder append(final float[] array) {
  350.         style.append(buffer, null, array, null);
  351.         return this;
  352.     }

  353.     /**
  354.      * Append to the {@code toString} an {@code int}
  355.      * value.
  356.      *
  357.      * @param value  the value to add to the {@code toString}
  358.      * @return {@code this} instance.
  359.      */
  360.     public ToStringBuilder append(final int value) {
  361.         style.append(buffer, null, value);
  362.         return this;
  363.     }

  364.     /**
  365.      * Append to the {@code toString} an {@code int}
  366.      * array.
  367.      *
  368.      * @param array  the array to add to the {@code toString}
  369.      * @return {@code this} instance.
  370.      */
  371.     public ToStringBuilder append(final int[] array) {
  372.         style.append(buffer, null, array, null);
  373.         return this;
  374.     }

  375.     /**
  376.      * Append to the {@code toString} a {@code long}
  377.      * value.
  378.      *
  379.      * @param value  the value to add to the {@code toString}
  380.      * @return {@code this} instance.
  381.      */
  382.     public ToStringBuilder append(final long value) {
  383.         style.append(buffer, null, value);
  384.         return this;
  385.     }

  386.     /**
  387.      * Append to the {@code toString} a {@code long}
  388.      * array.
  389.      *
  390.      * @param array  the array to add to the {@code toString}
  391.      * @return {@code this} instance.
  392.      */
  393.     public ToStringBuilder append(final long[] array) {
  394.         style.append(buffer, null, array, null);
  395.         return this;
  396.     }

  397.     /**
  398.      * Append to the {@code toString} an {@link Object}
  399.      * value.
  400.      *
  401.      * @param obj  the value to add to the {@code toString}
  402.      * @return {@code this} instance.
  403.      */
  404.     public ToStringBuilder append(final Object obj) {
  405.         style.append(buffer, null, obj, null);
  406.         return this;
  407.     }

  408.     /**
  409.      * Append to the {@code toString} an {@link Object}
  410.      * array.
  411.      *
  412.      * @param array  the array to add to the {@code toString}
  413.      * @return {@code this} instance.
  414.      */
  415.     public ToStringBuilder append(final Object[] array) {
  416.         style.append(buffer, null, array, null);
  417.         return this;
  418.     }

  419.     /**
  420.      * Append to the {@code toString} a {@code short}
  421.      * value.
  422.      *
  423.      * @param value  the value to add to the {@code toString}
  424.      * @return {@code this} instance.
  425.      */
  426.     public ToStringBuilder append(final short value) {
  427.         style.append(buffer, null, value);
  428.         return this;
  429.     }

  430.     /**
  431.      * Append to the {@code toString} a {@code short}
  432.      * array.
  433.      *
  434.      * @param array  the array to add to the {@code toString}
  435.      * @return {@code this} instance.
  436.      */
  437.     public ToStringBuilder append(final short[] array) {
  438.         style.append(buffer, null, array, null);
  439.         return this;
  440.     }

  441.     /**
  442.      * Append to the {@code toString} a {@code boolean}
  443.      * value.
  444.      *
  445.      * @param fieldName  the field name
  446.      * @param value  the value to add to the {@code toString}
  447.      * @return {@code this} instance.
  448.      */
  449.     public ToStringBuilder append(final String fieldName, final boolean value) {
  450.         style.append(buffer, fieldName, value);
  451.         return this;
  452.     }

  453.     /**
  454.      * Append to the {@code toString} a {@code boolean}
  455.      * array.
  456.      *
  457.      * @param fieldName  the field name
  458.      * @param array  the array to add to the {@code hashCode}
  459.      * @return {@code this} instance.
  460.      */
  461.     public ToStringBuilder append(final String fieldName, final boolean[] array) {
  462.         style.append(buffer, fieldName, array, null);
  463.         return this;
  464.     }

  465.     /**
  466.      * Append to the {@code toString} a {@code boolean}
  467.      * array.
  468.      *
  469.      * <p>A boolean parameter controls the level of detail to show.
  470.      * Setting {@code true} will output the array in full. Setting
  471.      * {@code false} will output a summary, typically the size of
  472.      * the array.</p>
  473.      *
  474.      * @param fieldName  the field name
  475.      * @param array  the array to add to the {@code toString}
  476.      * @param fullDetail  {@code true} for detail, {@code false}
  477.      *  for summary info
  478.      * @return {@code this} instance.
  479.      */
  480.     public ToStringBuilder append(final String fieldName, final boolean[] array, final boolean fullDetail) {
  481.         style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail));
  482.         return this;
  483.     }

  484.     /**
  485.      * Append to the {@code toString} an {@code byte}
  486.      * value.
  487.      *
  488.      * @param fieldName  the field name
  489.      * @param value  the value to add to the {@code toString}
  490.      * @return {@code this} instance.
  491.      */
  492.     public ToStringBuilder append(final String fieldName, final byte value) {
  493.         style.append(buffer, fieldName, value);
  494.         return this;
  495.     }

  496.     /**
  497.      * Append to the {@code toString} a {@code byte} array.
  498.      *
  499.      * @param fieldName  the field name
  500.      * @param array  the array to add to the {@code toString}
  501.      * @return {@code this} instance.
  502.      */
  503.     public ToStringBuilder append(final String fieldName, final byte[] array) {
  504.         style.append(buffer, fieldName, array, null);
  505.         return this;
  506.     }

  507.     /**
  508.      * Append to the {@code toString} a {@code byte}
  509.      * array.
  510.      *
  511.      * <p>A boolean parameter controls the level of detail to show.
  512.      * Setting {@code true} will output the array in full. Setting
  513.      * {@code false} will output a summary, typically the size of
  514.      * the array.
  515.      *
  516.      * @param fieldName  the field name
  517.      * @param array  the array to add to the {@code toString}
  518.      * @param fullDetail  {@code true} for detail, {@code false}
  519.      *  for summary info
  520.      * @return {@code this} instance.
  521.      */
  522.     public ToStringBuilder append(final String fieldName, final byte[] array, final boolean fullDetail) {
  523.         style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail));
  524.         return this;
  525.     }

  526.     /**
  527.      * Append to the {@code toString} a {@code char}
  528.      * value.
  529.      *
  530.      * @param fieldName  the field name
  531.      * @param value  the value to add to the {@code toString}
  532.      * @return {@code this} instance.
  533.      */
  534.     public ToStringBuilder append(final String fieldName, final char value) {
  535.         style.append(buffer, fieldName, value);
  536.         return this;
  537.     }

  538.     /**
  539.      * Append to the {@code toString} a {@code char}
  540.      * array.
  541.      *
  542.      * @param fieldName  the field name
  543.      * @param array  the array to add to the {@code toString}
  544.      * @return {@code this} instance.
  545.      */
  546.     public ToStringBuilder append(final String fieldName, final char[] array) {
  547.         style.append(buffer, fieldName, array, null);
  548.         return this;
  549.     }

  550.     /**
  551.      * Append to the {@code toString} a {@code char}
  552.      * array.
  553.      *
  554.      * <p>A boolean parameter controls the level of detail to show.
  555.      * Setting {@code true} will output the array in full. Setting
  556.      * {@code false} will output a summary, typically the size of
  557.      * the array.</p>
  558.      *
  559.      * @param fieldName  the field name
  560.      * @param array  the array to add to the {@code toString}
  561.      * @param fullDetail  {@code true} for detail, {@code false}
  562.      *  for summary info
  563.      * @return {@code this} instance.
  564.      */
  565.     public ToStringBuilder append(final String fieldName, final char[] array, final boolean fullDetail) {
  566.         style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail));
  567.         return this;
  568.     }

  569.     /**
  570.      * Append to the {@code toString} a {@code double}
  571.      * value.
  572.      *
  573.      * @param fieldName  the field name
  574.      * @param value  the value to add to the {@code toString}
  575.      * @return {@code this} instance.
  576.      */
  577.     public ToStringBuilder append(final String fieldName, final double value) {
  578.         style.append(buffer, fieldName, value);
  579.         return this;
  580.     }

  581.     /**
  582.      * Append to the {@code toString} a {@code double}
  583.      * array.
  584.      *
  585.      * @param fieldName  the field name
  586.      * @param array  the array to add to the {@code toString}
  587.      * @return {@code this} instance.
  588.      */
  589.     public ToStringBuilder append(final String fieldName, final double[] array) {
  590.         style.append(buffer, fieldName, array, null);
  591.         return this;
  592.     }

  593.     /**
  594.      * Append to the {@code toString} a {@code double}
  595.      * array.
  596.      *
  597.      * <p>A boolean parameter controls the level of detail to show.
  598.      * Setting {@code true} will output the array in full. Setting
  599.      * {@code false} will output a summary, typically the size of
  600.      * the array.</p>
  601.      *
  602.      * @param fieldName  the field name
  603.      * @param array  the array to add to the {@code toString}
  604.      * @param fullDetail  {@code true} for detail, {@code false}
  605.      *  for summary info
  606.      * @return {@code this} instance.
  607.      */
  608.     public ToStringBuilder append(final String fieldName, final double[] array, final boolean fullDetail) {
  609.         style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail));
  610.         return this;
  611.     }

  612.     /**
  613.      * Append to the {@code toString} an {@code float}
  614.      * value.
  615.      *
  616.      * @param fieldName  the field name
  617.      * @param value  the value to add to the {@code toString}
  618.      * @return {@code this} instance.
  619.      */
  620.     public ToStringBuilder append(final String fieldName, final float value) {
  621.         style.append(buffer, fieldName, value);
  622.         return this;
  623.     }

  624.     /**
  625.      * Append to the {@code toString} a {@code float}
  626.      * array.
  627.      *
  628.      * @param fieldName  the field name
  629.      * @param array  the array to add to the {@code toString}
  630.      * @return {@code this} instance.
  631.      */
  632.     public ToStringBuilder append(final String fieldName, final float[] array) {
  633.         style.append(buffer, fieldName, array, null);
  634.         return this;
  635.     }

  636.     /**
  637.      * Append to the {@code toString} a {@code float}
  638.      * array.
  639.      *
  640.      * <p>A boolean parameter controls the level of detail to show.
  641.      * Setting {@code true} will output the array in full. Setting
  642.      * {@code false} will output a summary, typically the size of
  643.      * the array.</p>
  644.      *
  645.      * @param fieldName  the field name
  646.      * @param array  the array to add to the {@code toString}
  647.      * @param fullDetail  {@code true} for detail, {@code false}
  648.      *  for summary info
  649.      * @return {@code this} instance.
  650.      */
  651.     public ToStringBuilder append(final String fieldName, final float[] array, final boolean fullDetail) {
  652.         style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail));
  653.         return this;
  654.     }

  655.     /**
  656.      * Append to the {@code toString} an {@code int}
  657.      * value.
  658.      *
  659.      * @param fieldName  the field name
  660.      * @param value  the value to add to the {@code toString}
  661.      * @return {@code this} instance.
  662.      */
  663.     public ToStringBuilder append(final String fieldName, final int value) {
  664.         style.append(buffer, fieldName, value);
  665.         return this;
  666.     }

  667.     /**
  668.      * Append to the {@code toString} an {@code int}
  669.      * array.
  670.      *
  671.      * @param fieldName  the field name
  672.      * @param array  the array to add to the {@code toString}
  673.      * @return {@code this} instance.
  674.      */
  675.     public ToStringBuilder append(final String fieldName, final int[] array) {
  676.         style.append(buffer, fieldName, array, null);
  677.         return this;
  678.     }

  679.     /**
  680.      * Append to the {@code toString} an {@code int}
  681.      * array.
  682.      *
  683.      * <p>A boolean parameter controls the level of detail to show.
  684.      * Setting {@code true} will output the array in full. Setting
  685.      * {@code false} will output a summary, typically the size of
  686.      * the array.</p>
  687.      *
  688.      * @param fieldName  the field name
  689.      * @param array  the array to add to the {@code toString}
  690.      * @param fullDetail  {@code true} for detail, {@code false}
  691.      *  for summary info
  692.      * @return {@code this} instance.
  693.      */
  694.     public ToStringBuilder append(final String fieldName, final int[] array, final boolean fullDetail) {
  695.         style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail));
  696.         return this;
  697.     }

  698.     /**
  699.      * Append to the {@code toString} a {@code long}
  700.      * value.
  701.      *
  702.      * @param fieldName  the field name
  703.      * @param value  the value to add to the {@code toString}
  704.      * @return {@code this} instance.
  705.      */
  706.     public ToStringBuilder append(final String fieldName, final long value) {
  707.         style.append(buffer, fieldName, value);
  708.         return this;
  709.     }

  710.     /**
  711.      * Append to the {@code toString} a {@code long}
  712.      * array.
  713.      *
  714.      * @param fieldName  the field name
  715.      * @param array  the array to add to the {@code toString}
  716.      * @return {@code this} instance.
  717.      */
  718.     public ToStringBuilder append(final String fieldName, final long[] array) {
  719.         style.append(buffer, fieldName, array, null);
  720.         return this;
  721.     }

  722.     /**
  723.      * Append to the {@code toString} a {@code long}
  724.      * array.
  725.      *
  726.      * <p>A boolean parameter controls the level of detail to show.
  727.      * Setting {@code true} will output the array in full. Setting
  728.      * {@code false} will output a summary, typically the size of
  729.      * the array.</p>
  730.      *
  731.      * @param fieldName  the field name
  732.      * @param array  the array to add to the {@code toString}
  733.      * @param fullDetail  {@code true} for detail, {@code false}
  734.      *  for summary info
  735.      * @return {@code this} instance.
  736.      */
  737.     public ToStringBuilder append(final String fieldName, final long[] array, final boolean fullDetail) {
  738.         style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail));
  739.         return this;
  740.     }

  741.     /**
  742.      * Append to the {@code toString} an {@link Object}
  743.      * value.
  744.      *
  745.      * @param fieldName  the field name
  746.      * @param obj  the value to add to the {@code toString}
  747.      * @return {@code this} instance.
  748.      */
  749.     public ToStringBuilder append(final String fieldName, final Object obj) {
  750.         style.append(buffer, fieldName, obj, null);
  751.         return this;
  752.     }

  753.     /**
  754.      * Append to the {@code toString} an {@link Object}
  755.      * value.
  756.      *
  757.      * @param fieldName  the field name
  758.      * @param obj  the value to add to the {@code toString}
  759.      * @param fullDetail  {@code true} for detail,
  760.      *  {@code false} for summary info
  761.      * @return {@code this} instance.
  762.      */
  763.     public ToStringBuilder append(final String fieldName, final Object obj, final boolean fullDetail) {
  764.         style.append(buffer, fieldName, obj, Boolean.valueOf(fullDetail));
  765.         return this;
  766.     }

  767.     /**
  768.      * Append to the {@code toString} an {@link Object}
  769.      * array.
  770.      *
  771.      * @param fieldName  the field name
  772.      * @param array  the array to add to the {@code toString}
  773.      * @return {@code this} instance.
  774.      */
  775.     public ToStringBuilder append(final String fieldName, final Object[] array) {
  776.         style.append(buffer, fieldName, array, null);
  777.         return this;
  778.     }

  779.     /**
  780.      * Append to the {@code toString} an {@link Object}
  781.      * array.
  782.      *
  783.      * <p>A boolean parameter controls the level of detail to show.
  784.      * Setting {@code true} will output the array in full. Setting
  785.      * {@code false} will output a summary, typically the size of
  786.      * the array.</p>
  787.      *
  788.      * @param fieldName  the field name
  789.      * @param array  the array to add to the {@code toString}
  790.      * @param fullDetail  {@code true} for detail, {@code false}
  791.      *  for summary info
  792.      * @return {@code this} instance.
  793.      */
  794.     public ToStringBuilder append(final String fieldName, final Object[] array, final boolean fullDetail) {
  795.         style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail));
  796.         return this;
  797.     }

  798.     /**
  799.      * Append to the {@code toString} an {@code short}
  800.      * value.
  801.      *
  802.      * @param fieldName  the field name
  803.      * @param value  the value to add to the {@code toString}
  804.      * @return {@code this} instance.
  805.      */
  806.     public ToStringBuilder append(final String fieldName, final short value) {
  807.         style.append(buffer, fieldName, value);
  808.         return this;
  809.     }

  810.     /**
  811.      * Append to the {@code toString} a {@code short}
  812.      * array.
  813.      *
  814.      * @param fieldName  the field name
  815.      * @param array  the array to add to the {@code toString}
  816.      * @return {@code this} instance.
  817.      */
  818.     public ToStringBuilder append(final String fieldName, final short[] array) {
  819.         style.append(buffer, fieldName, array, null);
  820.         return this;
  821.     }

  822.     /**
  823.      * Append to the {@code toString} a {@code short}
  824.      * array.
  825.      *
  826.      * <p>A boolean parameter controls the level of detail to show.
  827.      * Setting {@code true} will output the array in full. Setting
  828.      * {@code false} will output a summary, typically the size of
  829.      * the array.
  830.      *
  831.      * @param fieldName  the field name
  832.      * @param array  the array to add to the {@code toString}
  833.      * @param fullDetail  {@code true} for detail, {@code false}
  834.      *  for summary info
  835.      * @return {@code this} instance.
  836.      */
  837.     public ToStringBuilder append(final String fieldName, final short[] array, final boolean fullDetail) {
  838.         style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail));
  839.         return this;
  840.     }

  841.     /**
  842.      * Appends with the same format as the default {@code Object toString()
  843.      * } method. Appends the class name followed by
  844.      * {@link System#identityHashCode(Object)}.
  845.      *
  846.      * @param srcObject  the {@link Object} whose class name and id to output
  847.      * @return {@code this} instance.
  848.      * @throws NullPointerException if {@code srcObject} is {@code null}
  849.      * @since 2.0
  850.      */
  851.     public ToStringBuilder appendAsObjectToString(final Object srcObject) {
  852.         ObjectUtils.identityToString(this.getStringBuffer(), srcObject);
  853.         return this;
  854.     }

  855.     /**
  856.      * Append the {@code toString} from the superclass.
  857.      *
  858.      * <p>This method assumes that the superclass uses the same {@link ToStringStyle}
  859.      * as this one.</p>
  860.      *
  861.      * <p>If {@code superToString} is {@code null}, no change is made.</p>
  862.      *
  863.      * @param superToString  the result of {@code super.toString()}
  864.      * @return {@code this} instance.
  865.      * @since 2.0
  866.      */
  867.     public ToStringBuilder appendSuper(final String superToString) {
  868.         if (superToString != null) {
  869.             style.appendSuper(buffer, superToString);
  870.         }
  871.         return this;
  872.     }

  873.     /**
  874.      * Append the {@code toString} from another object.
  875.      *
  876.      * <p>This method is useful where a class delegates most of the implementation of
  877.      * its properties to another class. You can then call {@code toString()} on
  878.      * the other class and pass the result into this method.</p>
  879.      *
  880.      * <pre>
  881.      *   private AnotherObject delegate;
  882.      *   private String fieldInThisClass;
  883.      *
  884.      *   public String toString() {
  885.      *     return new ToStringBuilder(this).
  886.      *       appendToString(delegate.toString()).
  887.      *       append(fieldInThisClass).
  888.      *       toString();
  889.      *   }</pre>
  890.      *
  891.      * <p>This method assumes that the other object uses the same {@link ToStringStyle}
  892.      * as this one.</p>
  893.      *
  894.      * <p>If the {@code toString} is {@code null}, no change is made.</p>
  895.      *
  896.      * @param toString  the result of {@code toString()} on another object
  897.      * @return {@code this} instance.
  898.      * @since 2.0
  899.      */
  900.     public ToStringBuilder appendToString(final String toString) {
  901.         if (toString != null) {
  902.             style.appendToString(buffer, toString);
  903.         }
  904.         return this;
  905.     }

  906.     /**
  907.      * Returns the String that was build as an object representation. The
  908.      * default implementation utilizes the {@link #toString()} implementation.
  909.      *
  910.      * @return the String {@code toString}
  911.      *
  912.      * @see #toString()
  913.      *
  914.      * @since 3.0
  915.      */
  916.     @Override
  917.     public String build() {
  918.         return toString();
  919.     }

  920.     /**
  921.      * Returns the {@link Object} being output.
  922.      *
  923.      * @return The object being output.
  924.      * @since 2.0
  925.      */
  926.     public Object getObject() {
  927.         return object;
  928.     }

  929.     /**
  930.      * Gets the {@link StringBuffer} being populated.
  931.      *
  932.      * @return the {@link StringBuffer} being populated
  933.      */
  934.     public StringBuffer getStringBuffer() {
  935.         return buffer;
  936.     }

  937.     /**
  938.      * Gets the {@link ToStringStyle} being used.
  939.      *
  940.      * @return the {@link ToStringStyle} being used
  941.      * @since 2.0
  942.      */
  943.     public ToStringStyle getStyle() {
  944.         return style;
  945.     }

  946.     /**
  947.      * Returns the built {@code toString}.
  948.      *
  949.      * <p>This method appends the end of data indicator, and can only be called once.
  950.      * Use {@link #getStringBuffer} to get the current string state.</p>
  951.      *
  952.      * <p>If the object is {@code null}, return the style's {@code nullText}</p>
  953.      *
  954.      * @return the String {@code toString}
  955.      */
  956.     @Override
  957.     public String toString() {
  958.         if (this.getObject() == null) {
  959.             this.getStringBuffer().append(this.getStyle().getNullText());
  960.         } else {
  961.             style.appendEnd(this.getStringBuffer(), this.getObject());
  962.         }
  963.         return this.getStringBuffer().toString();
  964.     }
  965. }