001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.ldap.util;
021    
022    
023    /**
024     * <p>
025     * Assists in implementing {@link Object#toString()} methods.
026     * </p>
027     * <p>
028     * This class enables a good and consistent <code>toString()</code> to be
029     * built for any class or object. This class aims to simplify the process by:
030     * </p>
031     * <ul>
032     * <li>allowing field names</li>
033     * <li>handling all types consistently</li>
034     * <li>handling nulls consistently</li>
035     * <li>outputting arrays and multi-dimensional arrays</li>
036     * <li>enabling the detail level to be controlled for Objects and Collections</li>
037     * <li>handling class hierarchies</li>
038     * </ul>
039     * <p>
040     * To use this class write code as follows:
041     * </p>
042     * 
043     * <pre>
044     *  public class Person {
045     *    String name;
046     *    int age;
047     *    boolean isSmoker;
048     *  
049     *    ...
050     *  
051     *    public String toString() {
052     *      return new ToStringBuilder(this).
053     *        append(&quot;name&quot;, name).
054     *        append(&quot;age&quot;, age).
055     *        append(&quot;smoker&quot;, smoker).
056     *        toString();
057     *    }
058     *  }
059     * </pre>
060     * 
061     * <p>
062     * This will produce a toString of the format:
063     * <code>Person@7f54[name=Stephen,age=29,smoker=false]</code>
064     * </p>
065     * <p>
066     * To add the superclass <code>toString</code>, use {@link #appendSuper}. To
067     * append the <code>toString</code> from an object that is delegated to (or
068     * any other object), use {@link #appendToString}.
069     * </p>
070     * <p>
071     * Alternatively, there is a method that uses reflection to determine the fields
072     * to test. Because these fields are usually private, the method,
073     * <code>reflectionToString</code>, uses
074     * <code>AccessibleObject.setAccessible</code> to change the visibility of the
075     * fields. This will fail under a security manager, unless the appropriate
076     * permissions are set up correctly. It is also slower than testing explicitly.
077     * </p>
078     * <p>
079     * A typical invocation for this method would look like:
080     * </p>
081     * 
082     * <pre>
083     * public String toString()
084     * {
085     *     return ToStringBuilder.reflectionToString( this );
086     * }
087     * </pre>
088     * 
089     * <p>
090     * You can also use the builder to debug 3rd party objects:
091     * </p>
092     * 
093     * <pre>
094     * System.out.println( &quot;An object: &quot; + ToStringBuilder.reflectionToString( anObject ) );
095     * </pre>
096     * 
097     * <p>
098     * The exact format of the <code>toString</code> is determined by the
099     * {@link ToStringStyle} passed into the constructor.
100     * </p>
101     * 
102     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
103     */
104    public class ToStringBuilder
105    {
106    
107        /**
108         * The default style of output to use.
109         */
110        private static ToStringStyle defaultStyle = ToStringStyle.DEFAULT_STYLE;
111    
112    
113        // ----------------------------------------------------------------------------
114    
115        /**
116         * <p>
117         * Gets the default <code>ToStringStyle</code> to use.
118         * </p>
119         * <p>
120         * This could allow the <code>ToStringStyle</code> to be controlled for an
121         * entire application with one call.
122         * </p>
123         * <p>
124         * This might be used to have a verbose <code>ToStringStyle</code> during
125         * development and a compact <code>ToStringStyle</code> in production.
126         * </p>
127         * 
128         * @return the default <code>ToStringStyle</code>
129         */
130        public static ToStringStyle getDefaultStyle()
131        {
132            return defaultStyle;
133        }
134    
135    
136        /**
137         * <p>
138         * Forwards to <code>ReflectionToStringBuilder</code>.
139         * </p>
140         * 
141         * @see ReflectionToStringBuilder#toString(Object)
142         */
143        public static String reflectionToString( Object object )
144        {
145            return ReflectionToStringBuilder.toString( object );
146        }
147    
148    
149        /**
150         * <p>
151         * Forwards to <code>ReflectionToStringBuilder</code>.
152         * </p>
153         * 
154         * @see ReflectionToStringBuilder#toString(Object,ToStringStyle)
155         */
156        public static String reflectionToString( Object object, ToStringStyle style )
157        {
158            return ReflectionToStringBuilder.toString( object, style );
159        }
160    
161    
162        /**
163         * <p>
164         * Forwards to <code>ReflectionToStringBuilder</code>.
165         * </p>
166         * 
167         * @see ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean)
168         */
169        public static String reflectionToString( Object object, ToStringStyle style, boolean outputTransients )
170        {
171            return ReflectionToStringBuilder.toString( object, style, outputTransients, false, null );
172        }
173    
174    
175        /**
176         * <p>
177         * Forwards to <code>ReflectionToStringBuilder</code>.
178         * </p>
179         * 
180         * @see ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean,boolean,Class)
181         * @since 2.0
182         */
183        public static String reflectionToString( Object object, ToStringStyle style, boolean outputTransients,
184            Class reflectUpToClass )
185        {
186            return ReflectionToStringBuilder.toString( object, style, outputTransients, false, reflectUpToClass );
187        }
188    
189    
190        /**
191         * <p>
192         * Sets the default <code>ToStringStyle</code> to use.
193         * </p>
194         * 
195         * @param style
196         *            the default <code>ToStringStyle</code>
197         * @throws IllegalArgumentException
198         *             if the style is <code>null</code>
199         */
200        public static void setDefaultStyle( ToStringStyle style )
201        {
202            if ( style == null )
203            {
204                throw new IllegalArgumentException( "The style must not be null" );
205            }
206            defaultStyle = style;
207        }
208    
209        /**
210         * Current toString buffer.
211         */
212        private final StringBuffer buffer;
213    
214        /**
215         * The object being output.
216         */
217        private final Object object;
218    
219        /**
220         * The style of output to use.
221         */
222        private final ToStringStyle style;
223    
224    
225        /**
226         * <p>
227         * Constructor for <code>ToStringBuilder</code>.
228         * </p>
229         * <p>
230         * This constructor outputs using the default style set with
231         * <code>setDefaultStyle</code>.
232         * </p>
233         * 
234         * @param object
235         *            the Object to build a <code>toString</code> for
236         * @throws IllegalArgumentException
237         *             if the Object passed in is <code>null</code>
238         */
239        public ToStringBuilder(Object object)
240        {
241            this( object, getDefaultStyle(), null );
242        }
243    
244    
245        /**
246         * <p>
247         * Constructor for <code>ToStringBuilder</code> specifying the output
248         * style.
249         * </p>
250         * <p>
251         * If the style is <code>null</code>, the default style is used.
252         * </p>
253         * 
254         * @param object
255         *            the Object to build a <code>toString</code> for
256         * @param style
257         *            the style of the <code>toString</code> to create, may be
258         *            <code>null</code>
259         * @throws IllegalArgumentException
260         *             if the Object passed in is <code>null</code>
261         */
262        public ToStringBuilder(Object object, ToStringStyle style)
263        {
264            this( object, style, null );
265        }
266    
267    
268        /**
269         * <p>
270         * Constructor for <code>ToStringBuilder</code>.
271         * </p>
272         * <p>
273         * If the style is <code>null</code>, the default style is used.
274         * </p>
275         * <p>
276         * If the buffer is <code>null</code>, a new one is created.
277         * </p>
278         * 
279         * @param object
280         *            the Object to build a <code>toString</code> for
281         * @param style
282         *            the style of the <code>toString</code> to create, may be
283         *            <code>null</code>
284         * @param buffer
285         *            the <code>StringBuffer</code> to populate, may be
286         *            <code>null</code>
287         */
288        public ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer)
289        {
290            if ( style == null )
291            {
292                style = getDefaultStyle();
293            }
294            if ( buffer == null )
295            {
296                buffer = new StringBuffer( 512 );
297            }
298            this.buffer = buffer;
299            this.style = style;
300            this.object = object;
301    
302            style.appendStart( buffer, object );
303        }
304    
305    
306        // ----------------------------------------------------------------------------
307    
308        /**
309         * <p>
310         * Append to the <code>toString</code> a <code>boolean</code> value.
311         * </p>
312         * 
313         * @param value
314         *            the value to add to the <code>toString</code>
315         * @return this
316         */
317        public ToStringBuilder append( boolean value )
318        {
319            style.append( buffer, null, value );
320            return this;
321        }
322    
323    
324        // ----------------------------------------------------------------------------
325    
326        /**
327         * <p>
328         * Append to the <code>toString</code> a <code>boolean</code> array.
329         * </p>
330         * 
331         * @param array
332         *            the array to add to the <code>toString</code>
333         * @return this
334         */
335        public ToStringBuilder append( boolean[] array )
336        {
337            style.append( buffer, null, array, null );
338            return this;
339        }
340    
341    
342        // ----------------------------------------------------------------------------
343    
344        /**
345         * <p>
346         * Append to the <code>toString</code> a <code>byte</code> value.
347         * </p>
348         * 
349         * @param value
350         *            the value to add to the <code>toString</code>
351         * @return this
352         */
353        public ToStringBuilder append( byte value )
354        {
355            style.append( buffer, null, value );
356            return this;
357        }
358    
359    
360        // ----------------------------------------------------------------------------
361    
362        /**
363         * <p>
364         * Append to the <code>toString</code> a <code>byte</code> array.
365         * </p>
366         * 
367         * @param array
368         *            the array to add to the <code>toString</code>
369         * @return this
370         */
371        public ToStringBuilder append( byte[] array )
372        {
373            style.append( buffer, null, array, null );
374            return this;
375        }
376    
377    
378        // ----------------------------------------------------------------------------
379    
380        /**
381         * <p>
382         * Append to the <code>toString</code> a <code>char</code> value.
383         * </p>
384         * 
385         * @param value
386         *            the value to add to the <code>toString</code>
387         * @return this
388         */
389        public ToStringBuilder append( char value )
390        {
391            style.append( buffer, null, value );
392            return this;
393        }
394    
395    
396        // ----------------------------------------------------------------------------
397    
398        /**
399         * <p>
400         * Append to the <code>toString</code> a <code>char</code> array.
401         * </p>
402         * 
403         * @param array
404         *            the array to add to the <code>toString</code>
405         * @return this
406         */
407        public ToStringBuilder append( char[] array )
408        {
409            style.append( buffer, null, array, null );
410            return this;
411        }
412    
413    
414        // ----------------------------------------------------------------------------
415    
416        /**
417         * <p>
418         * Append to the <code>toString</code> a <code>double</code> value.
419         * </p>
420         * 
421         * @param value
422         *            the value to add to the <code>toString</code>
423         * @return this
424         */
425        public ToStringBuilder append( double value )
426        {
427            style.append( buffer, null, value );
428            return this;
429        }
430    
431    
432        // ----------------------------------------------------------------------------
433    
434        /**
435         * <p>
436         * Append to the <code>toString</code> a <code>double</code> array.
437         * </p>
438         * 
439         * @param array
440         *            the array to add to the <code>toString</code>
441         * @return this
442         */
443        public ToStringBuilder append( double[] array )
444        {
445            style.append( buffer, null, array, null );
446            return this;
447        }
448    
449    
450        // ----------------------------------------------------------------------------
451    
452        /**
453         * <p>
454         * Append to the <code>toString</code> a <code>float</code> value.
455         * </p>
456         * 
457         * @param value
458         *            the value to add to the <code>toString</code>
459         * @return this
460         */
461        public ToStringBuilder append( float value )
462        {
463            style.append( buffer, null, value );
464            return this;
465        }
466    
467    
468        // ----------------------------------------------------------------------------
469    
470        /**
471         * <p>
472         * Append to the <code>toString</code> a <code>float</code> array.
473         * </p>
474         * 
475         * @param array
476         *            the array to add to the <code>toString</code>
477         * @return this
478         */
479        public ToStringBuilder append( float[] array )
480        {
481            style.append( buffer, null, array, null );
482            return this;
483        }
484    
485    
486        // ----------------------------------------------------------------------------
487    
488        /**
489         * <p>
490         * Append to the <code>toString</code> an <code>int</code> value.
491         * </p>
492         * 
493         * @param value
494         *            the value to add to the <code>toString</code>
495         * @return this
496         */
497        public ToStringBuilder append( int value )
498        {
499            style.append( buffer, null, value );
500            return this;
501        }
502    
503    
504        // ----------------------------------------------------------------------------
505    
506        /**
507         * <p>
508         * Append to the <code>toString</code> an <code>int</code> array.
509         * </p>
510         * 
511         * @param array
512         *            the array to add to the <code>toString</code>
513         * @return this
514         */
515        public ToStringBuilder append( int[] array )
516        {
517            style.append( buffer, null, array, null );
518            return this;
519        }
520    
521    
522        // ----------------------------------------------------------------------------
523    
524        /**
525         * <p>
526         * Append to the <code>toString</code> a <code>long</code> value.
527         * </p>
528         * 
529         * @param value
530         *            the value to add to the <code>toString</code>
531         * @return this
532         */
533        public ToStringBuilder append( long value )
534        {
535            style.append( buffer, null, value );
536            return this;
537        }
538    
539    
540        // ----------------------------------------------------------------------------
541    
542        /**
543         * <p>
544         * Append to the <code>toString</code> a <code>long</code> array.
545         * </p>
546         * 
547         * @param array
548         *            the array to add to the <code>toString</code>
549         * @return this
550         */
551        public ToStringBuilder append( long[] array )
552        {
553            style.append( buffer, null, array, null );
554            return this;
555        }
556    
557    
558        // ----------------------------------------------------------------------------
559    
560        /**
561         * <p>
562         * Append to the <code>toString</code> an <code>Object</code> value.
563         * </p>
564         * 
565         * @param object
566         *            the value to add to the <code>toString</code>
567         * @return this
568         */
569        public ToStringBuilder append( Object object )
570        {
571            style.append( buffer, null, object, null );
572            return this;
573        }
574    
575    
576        // ----------------------------------------------------------------------------
577    
578        /**
579         * <p>
580         * Append to the <code>toString</code> an <code>Object</code> array.
581         * </p>
582         * 
583         * @param array
584         *            the array to add to the <code>toString</code>
585         * @return this
586         */
587        public ToStringBuilder append( Object[] array )
588        {
589            style.append( buffer, null, array, null );
590            return this;
591        }
592    
593    
594        // ----------------------------------------------------------------------------
595    
596        /**
597         * <p>
598         * Append to the <code>toString</code> a <code>short</code> value.
599         * </p>
600         * 
601         * @param value
602         *            the value to add to the <code>toString</code>
603         * @return this
604         */
605        public ToStringBuilder append( short value )
606        {
607            style.append( buffer, null, value );
608            return this;
609        }
610    
611    
612        // ----------------------------------------------------------------------------
613    
614        /**
615         * <p>
616         * Append to the <code>toString</code> a <code>short</code> array.
617         * </p>
618         * 
619         * @param array
620         *            the array to add to the <code>toString</code>
621         * @return this
622         */
623        public ToStringBuilder append( short[] array )
624        {
625            style.append( buffer, null, array, null );
626            return this;
627        }
628    
629    
630        /**
631         * <p>
632         * Append to the <code>toString</code> a <code>boolean</code> value.
633         * </p>
634         * 
635         * @param fieldName
636         *            the field name
637         * @param value
638         *            the value to add to the <code>toString</code>
639         * @return this
640         */
641        public ToStringBuilder append( String fieldName, boolean value )
642        {
643            style.append( buffer, fieldName, value );
644            return this;
645        }
646    
647    
648        /**
649         * <p>
650         * Append to the <code>toString</code> a <code>boolean</code> array.
651         * </p>
652         * 
653         * @param fieldName
654         *            the field name
655         * @param array
656         *            the array to add to the <code>hashCode</code>
657         * @return this
658         */
659        public ToStringBuilder append( String fieldName, boolean[] array )
660        {
661            style.append( buffer, fieldName, array, null );
662            return this;
663        }
664    
665    
666        /**
667         * <p>
668         * Append to the <code>toString</code> a <code>boolean</code> array.
669         * </p>
670         * <p>
671         * A boolean parameter controls the level of detail to show. Setting
672         * <code>true</code> will output the array in full. Setting
673         * <code>false</code> will output a summary, typically the size of the
674         * array.
675         * </p>
676         * 
677         * @param fieldName
678         *            the field name
679         * @param array
680         *            the array to add to the <code>toString</code>
681         * @param fullDetail
682         *            <code>true</code> for detail, <code>false</code> for
683         *            summary info
684         * @return this
685         */
686        public ToStringBuilder append( String fieldName, boolean[] array, boolean fullDetail )
687        {
688            style.append( buffer, fieldName, array, BooleanUtils.toBooleanObject( fullDetail ) );
689            return this;
690        }
691    
692    
693        /**
694         * <p>
695         * Append to the <code>toString</code> an <code>byte</code> value.
696         * </p>
697         * 
698         * @param fieldName
699         *            the field name
700         * @param value
701         *            the value to add to the <code>toString</code>
702         * @return this
703         */
704        public ToStringBuilder append( String fieldName, byte value )
705        {
706            style.append( buffer, fieldName, value );
707            return this;
708        }
709    
710    
711        /**
712         * <p>
713         * Append to the <code>toString</code> a <code>byte</code> array.
714         * </p>
715         * 
716         * @param fieldName
717         *            the field name
718         * @param array
719         *            the array to add to the <code>toString</code>
720         * @return this
721         */
722        public ToStringBuilder append( String fieldName, byte[] array )
723        {
724            style.append( buffer, fieldName, array, null );
725            return this;
726        }
727    
728    
729        /**
730         * <p>
731         * Append to the <code>toString</code> a <code>byte</code> array.
732         * </p>
733         * <p>
734         * A boolean parameter controls the level of detail to show. Setting
735         * <code>true</code> will output the array in full. Setting
736         * <code>false</code> will output a summary, typically the size of the
737         * array.
738         * 
739         * @param fieldName
740         *            the field name
741         * @param array
742         *            the array to add to the <code>toString</code>
743         * @param fullDetail
744         *            <code>true</code> for detail, <code>false</code> for
745         *            summary info
746         * @return this
747         */
748        public ToStringBuilder append( String fieldName, byte[] array, boolean fullDetail )
749        {
750            style.append( buffer, fieldName, array, BooleanUtils.toBooleanObject( fullDetail ) );
751            return this;
752        }
753    
754    
755        /**
756         * <p>
757         * Append to the <code>toString</code> a <code>char</code> value.
758         * </p>
759         * 
760         * @param fieldName
761         *            the field name
762         * @param value
763         *            the value to add to the <code>toString</code>
764         * @return this
765         */
766        public ToStringBuilder append( String fieldName, char value )
767        {
768            style.append( buffer, fieldName, value );
769            return this;
770        }
771    
772    
773        /**
774         * <p>
775         * Append to the <code>toString</code> a <code>char</code> array.
776         * </p>
777         * 
778         * @param fieldName
779         *            the field name
780         * @param array
781         *            the array to add to the <code>toString</code>
782         * @return this
783         */
784        public ToStringBuilder append( String fieldName, char[] array )
785        {
786            style.append( buffer, fieldName, array, null );
787            return this;
788        }
789    
790    
791        /**
792         * <p>
793         * Append to the <code>toString</code> a <code>char</code> array.
794         * </p>
795         * <p>
796         * A boolean parameter controls the level of detail to show. Setting
797         * <code>true</code> will output the array in full. Setting
798         * <code>false</code> will output a summary, typically the size of the
799         * array.
800         * </p>
801         * 
802         * @param fieldName
803         *            the field name
804         * @param array
805         *            the array to add to the <code>toString</code>
806         * @param fullDetail
807         *            <code>true</code> for detail, <code>false</code> for
808         *            summary info
809         * @return this
810         */
811        public ToStringBuilder append( String fieldName, char[] array, boolean fullDetail )
812        {
813            style.append( buffer, fieldName, array, BooleanUtils.toBooleanObject( fullDetail ) );
814            return this;
815        }
816    
817    
818        /**
819         * <p>
820         * Append to the <code>toString</code> a <code>double</code> value.
821         * </p>
822         * 
823         * @param fieldName
824         *            the field name
825         * @param value
826         *            the value to add to the <code>toString</code>
827         * @return this
828         */
829        public ToStringBuilder append( String fieldName, double value )
830        {
831            style.append( buffer, fieldName, value );
832            return this;
833        }
834    
835    
836        /**
837         * <p>
838         * Append to the <code>toString</code> a <code>double</code> array.
839         * </p>
840         * 
841         * @param fieldName
842         *            the field name
843         * @param array
844         *            the array to add to the <code>toString</code>
845         * @return this
846         */
847        public ToStringBuilder append( String fieldName, double[] array )
848        {
849            style.append( buffer, fieldName, array, null );
850            return this;
851        }
852    
853    
854        /**
855         * <p>
856         * Append to the <code>toString</code> a <code>double</code> array.
857         * </p>
858         * <p>
859         * A boolean parameter controls the level of detail to show. Setting
860         * <code>true</code> will output the array in full. Setting
861         * <code>false</code> will output a summary, typically the size of the
862         * array.
863         * </p>
864         * 
865         * @param fieldName
866         *            the field name
867         * @param array
868         *            the array to add to the <code>toString</code>
869         * @param fullDetail
870         *            <code>true</code> for detail, <code>false</code> for
871         *            summary info
872         * @return this
873         */
874        public ToStringBuilder append( String fieldName, double[] array, boolean fullDetail )
875        {
876            style.append( buffer, fieldName, array, BooleanUtils.toBooleanObject( fullDetail ) );
877            return this;
878        }
879    
880    
881        /**
882         * <p>
883         * Append to the <code>toString</code> an <code>float</code> value.
884         * </p>
885         * 
886         * @param fieldName
887         *            the field name
888         * @param value
889         *            the value to add to the <code>toString</code>
890         * @return this
891         */
892        public ToStringBuilder append( String fieldName, float value )
893        {
894            style.append( buffer, fieldName, value );
895            return this;
896        }
897    
898    
899        /**
900         * <p>
901         * Append to the <code>toString</code> a <code>float</code> array.
902         * </p>
903         * 
904         * @param fieldName
905         *            the field name
906         * @param array
907         *            the array to add to the <code>toString</code>
908         * @return this
909         */
910        public ToStringBuilder append( String fieldName, float[] array )
911        {
912            style.append( buffer, fieldName, array, null );
913            return this;
914        }
915    
916    
917        /**
918         * <p>
919         * Append to the <code>toString</code> a <code>float</code> array.
920         * </p>
921         * <p>
922         * A boolean parameter controls the level of detail to show. Setting
923         * <code>true</code> will output the array in full. Setting
924         * <code>false</code> will output a summary, typically the size of the
925         * array.
926         * </p>
927         * 
928         * @param fieldName
929         *            the field name
930         * @param array
931         *            the array to add to the <code>toString</code>
932         * @param fullDetail
933         *            <code>true</code> for detail, <code>false</code> for
934         *            summary info
935         * @return this
936         */
937        public ToStringBuilder append( String fieldName, float[] array, boolean fullDetail )
938        {
939            style.append( buffer, fieldName, array, BooleanUtils.toBooleanObject( fullDetail ) );
940            return this;
941        }
942    
943    
944        /**
945         * <p>
946         * Append to the <code>toString</code> an <code>int</code> value.
947         * </p>
948         * 
949         * @param fieldName
950         *            the field name
951         * @param value
952         *            the value to add to the <code>toString</code>
953         * @return this
954         */
955        public ToStringBuilder append( String fieldName, int value )
956        {
957            style.append( buffer, fieldName, value );
958            return this;
959        }
960    
961    
962        /**
963         * <p>
964         * Append to the <code>toString</code> an <code>int</code> array.
965         * </p>
966         * 
967         * @param fieldName
968         *            the field name
969         * @param array
970         *            the array to add to the <code>toString</code>
971         * @return this
972         */
973        public ToStringBuilder append( String fieldName, int[] array )
974        {
975            style.append( buffer, fieldName, array, null );
976            return this;
977        }
978    
979    
980        /**
981         * <p>
982         * Append to the <code>toString</code> an <code>int</code> array.
983         * </p>
984         * <p>
985         * A boolean parameter controls the level of detail to show. Setting
986         * <code>true</code> will output the array in full. Setting
987         * <code>false</code> will output a summary, typically the size of the
988         * array.
989         * </p>
990         * 
991         * @param fieldName
992         *            the field name
993         * @param array
994         *            the array to add to the <code>toString</code>
995         * @param fullDetail
996         *            <code>true</code> for detail, <code>false</code> for
997         *            summary info
998         * @return this
999         */
1000        public ToStringBuilder append( String fieldName, int[] array, boolean fullDetail )
1001        {
1002            style.append( buffer, fieldName, array, BooleanUtils.toBooleanObject( fullDetail ) );
1003            return this;
1004        }
1005    
1006    
1007        /**
1008         * <p>
1009         * Append to the <code>toString</code> a <code>long</code> value.
1010         * </p>
1011         * 
1012         * @param fieldName
1013         *            the field name
1014         * @param value
1015         *            the value to add to the <code>toString</code>
1016         * @return this
1017         */
1018        public ToStringBuilder append( String fieldName, long value )
1019        {
1020            style.append( buffer, fieldName, value );
1021            return this;
1022        }
1023    
1024    
1025        /**
1026         * <p>
1027         * Append to the <code>toString</code> a <code>long</code> array.
1028         * </p>
1029         * 
1030         * @param fieldName
1031         *            the field name
1032         * @param array
1033         *            the array to add to the <code>toString</code>
1034         * @return this
1035         */
1036        public ToStringBuilder append( String fieldName, long[] array )
1037        {
1038            style.append( buffer, fieldName, array, null );
1039            return this;
1040        }
1041    
1042    
1043        /**
1044         * <p>
1045         * Append to the <code>toString</code> a <code>long</code> array.
1046         * </p>
1047         * <p>
1048         * A boolean parameter controls the level of detail to show. Setting
1049         * <code>true</code> will output the array in full. Setting
1050         * <code>false</code> will output a summary, typically the size of the
1051         * array.
1052         * </p>
1053         * 
1054         * @param fieldName
1055         *            the field name
1056         * @param array
1057         *            the array to add to the <code>toString</code>
1058         * @param fullDetail
1059         *            <code>true</code> for detail, <code>false</code> for
1060         *            summary info
1061         * @return this
1062         */
1063        public ToStringBuilder append( String fieldName, long[] array, boolean fullDetail )
1064        {
1065            style.append( buffer, fieldName, array, BooleanUtils.toBooleanObject( fullDetail ) );
1066            return this;
1067        }
1068    
1069    
1070        /**
1071         * <p>
1072         * Append to the <code>toString</code> an <code>Object</code> value.
1073         * </p>
1074         * 
1075         * @param fieldName
1076         *            the field name
1077         * @param object
1078         *            the value to add to the <code>toString</code>
1079         * @return this
1080         */
1081        public ToStringBuilder append( String fieldName, Object object )
1082        {
1083            style.append( buffer, fieldName, object, null );
1084            return this;
1085        }
1086    
1087    
1088        /**
1089         * <p>
1090         * Append to the <code>toString</code> an <code>Object</code> value.
1091         * </p>
1092         * 
1093         * @param fieldName
1094         *            the field name
1095         * @param object
1096         *            the value to add to the <code>toString</code>
1097         * @param fullDetail
1098         *            <code>true</code> for detail, <code>false</code> for
1099         *            summary info
1100         * @return this
1101         */
1102        public ToStringBuilder append( String fieldName, Object object, boolean fullDetail )
1103        {
1104            style.append( buffer, fieldName, object, BooleanUtils.toBooleanObject( fullDetail ) );
1105            return this;
1106        }
1107    
1108    
1109        /**
1110         * <p>
1111         * Append to the <code>toString</code> an <code>Object</code> array.
1112         * </p>
1113         * 
1114         * @param fieldName
1115         *            the field name
1116         * @param array
1117         *            the array to add to the <code>toString</code>
1118         * @return this
1119         */
1120        public ToStringBuilder append( String fieldName, Object[] array )
1121        {
1122            style.append( buffer, fieldName, array, null );
1123            return this;
1124        }
1125    
1126    
1127        /**
1128         * <p>
1129         * Append to the <code>toString</code> an <code>Object</code> array.
1130         * </p>
1131         * <p>
1132         * A boolean parameter controls the level of detail to show. Setting
1133         * <code>true</code> will output the array in full. Setting
1134         * <code>false</code> will output a summary, typically the size of the
1135         * array.
1136         * </p>
1137         * 
1138         * @param fieldName
1139         *            the field name
1140         * @param array
1141         *            the array to add to the <code>toString</code>
1142         * @param fullDetail
1143         *            <code>true</code> for detail, <code>false</code> for
1144         *            summary info
1145         * @return this
1146         */
1147        public ToStringBuilder append( String fieldName, Object[] array, boolean fullDetail )
1148        {
1149            style.append( buffer, fieldName, array, BooleanUtils.toBooleanObject( fullDetail ) );
1150            return this;
1151        }
1152    
1153    
1154        /**
1155         * <p>
1156         * Append to the <code>toString</code> an <code>short</code> value.
1157         * </p>
1158         * 
1159         * @param fieldName
1160         *            the field name
1161         * @param value
1162         *            the value to add to the <code>toString</code>
1163         * @return this
1164         */
1165        public ToStringBuilder append( String fieldName, short value )
1166        {
1167            style.append( buffer, fieldName, value );
1168            return this;
1169        }
1170    
1171    
1172        /**
1173         * <p>
1174         * Append to the <code>toString</code> a <code>short</code> array.
1175         * </p>
1176         * 
1177         * @param fieldName
1178         *            the field name
1179         * @param array
1180         *            the array to add to the <code>toString</code>
1181         * @return this
1182         */
1183        public ToStringBuilder append( String fieldName, short[] array )
1184        {
1185            style.append( buffer, fieldName, array, null );
1186            return this;
1187        }
1188    
1189    
1190        /**
1191         * <p>
1192         * Append to the <code>toString</code> a <code>short</code> array.
1193         * </p>
1194         * <p>
1195         * A boolean parameter controls the level of detail to show. Setting
1196         * <code>true</code> will output the array in full. Setting
1197         * <code>false</code> will output a summary, typically the size of the
1198         * array.
1199         * 
1200         * @param fieldName
1201         *            the field name
1202         * @param array
1203         *            the array to add to the <code>toString</code>
1204         * @param fullDetail
1205         *            <code>true</code> for detail, <code>false</code> for
1206         *            summary info
1207         * @return this
1208         */
1209        public ToStringBuilder append( String fieldName, short[] array, boolean fullDetail )
1210        {
1211            style.append( buffer, fieldName, array, BooleanUtils.toBooleanObject( fullDetail ) );
1212            return this;
1213        }
1214    
1215    
1216        /**
1217         * <p>
1218         * Appends with the same format as the default <code>Object toString()
1219         * </code>
1220         * method. Appends the class name followed by
1221         * {@link System#identityHashCode(java.lang.Object)}.
1222         * </p>
1223         * 
1224         * @param object
1225         *            the <code>Object</code> whose class name and id to output
1226         * @return this
1227         * @since 2.0
1228         */
1229        public ToStringBuilder appendAsObjectToString( Object object )
1230        {
1231            ObjectUtils.appendIdentityToString( this.getStringBuffer(), object );
1232            return this;
1233        }
1234    
1235    
1236        // ----------------------------------------------------------------------------
1237    
1238        /**
1239         * <p>
1240         * Append the <code>toString</code> from the superclass.
1241         * </p>
1242         * <p>
1243         * This method assumes that the superclass uses the same
1244         * <code>ToStringStyle</code> as this one.
1245         * </p>
1246         * <p>
1247         * If <code>superToString</code> is <code>null</code>, no change is
1248         * made.
1249         * </p>
1250         * 
1251         * @param superToString
1252         *            the result of <code>super.toString()</code>
1253         * @return this
1254         * @since 2.0
1255         */
1256        public ToStringBuilder appendSuper( String superToString )
1257        {
1258            if ( superToString != null )
1259            {
1260                style.appendSuper( buffer, superToString );
1261            }
1262            return this;
1263        }
1264    
1265    
1266        /**
1267         * <p>
1268         * Append the <code>toString</code> from another object.
1269         * </p>
1270         * <p>
1271         * This method is useful where a class delegates most of the implementation
1272         * of its properties to another class. You can then call
1273         * <code>toString()</code> on the other class and pass the result into
1274         * this method.
1275         * </p>
1276         * 
1277         * <pre>
1278         * private AnotherObject delegate;
1279         * 
1280         * private String fieldInThisClass;
1281         * 
1282         * 
1283         * public String toString()
1284         * {
1285         *     return new ToStringBuilder( this ).appendToString( delegate.toString() ).append( fieldInThisClass ).toString();
1286         * }
1287         * </pre>
1288         * 
1289         * <p>
1290         * This method assumes that the other object uses the same
1291         * <code>ToStringStyle</code> as this one.
1292         * </p>
1293         * <p>
1294         * If the <code>toString</code> is <code>null</code>, no change is
1295         * made.
1296         * </p>
1297         * 
1298         * @param toString
1299         *            the result of <code>toString()</code> on another object
1300         * @return this
1301         * @since 2.0
1302         */
1303        public ToStringBuilder appendToString( String toString )
1304        {
1305            if ( toString != null )
1306            {
1307                style.appendToString( buffer, toString );
1308            }
1309            return this;
1310        }
1311    
1312    
1313        /**
1314         * <p>
1315         * Returns the <code>Object</code> being output.
1316         * </p>
1317         * 
1318         * @return The object being output.
1319         * @since 2.0
1320         */
1321        public Object getObject()
1322        {
1323            return object;
1324        }
1325    
1326    
1327        /**
1328         * <p>
1329         * Gets the <code>StringBuffer</code> being populated.
1330         * </p>
1331         * 
1332         * @return the <code>StringBuffer</code> being populated
1333         */
1334        public StringBuffer getStringBuffer()
1335        {
1336            return buffer;
1337        }
1338    
1339    
1340        // ----------------------------------------------------------------------------
1341    
1342        /**
1343         * <p>
1344         * Gets the <code>ToStringStyle</code> being used.
1345         * </p>
1346         * 
1347         * @return the <code>ToStringStyle</code> being used
1348         * @since 2.0
1349         */
1350        public ToStringStyle getStyle()
1351        {
1352            return style;
1353        }
1354    
1355    
1356        /**
1357         * <p>
1358         * Returns the built <code>toString</code>.
1359         * </p>
1360         * <p>
1361         * This method appends the end of data indicator, and can only be called
1362         * once. Use {@link #getStringBuffer} to get the current string state.
1363         * </p>
1364         * <p>
1365         * If the object is <code>null</code>, return the style's
1366         * <code>nullText</code>
1367         * </p>
1368         * 
1369         * @return the String <code>toString</code>
1370         */
1371        public String toString()
1372        {
1373            if ( this.getObject() == null )
1374            {
1375                this.getStringBuffer().append( this.getStyle().getNullText() );
1376            }
1377            else
1378            {
1379                style.appendEnd( this.getStringBuffer(), this.getObject() );
1380            }
1381            return this.getStringBuffer().toString();
1382        }
1383    
1384    }