KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > builder > ToStringBuilder


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

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

92 public class ToStringBuilder {
93
94     /**
95      * The default style of output to use.
96      */

97     private static ToStringStyle defaultStyle = ToStringStyle.DEFAULT_STYLE;
98
99     //----------------------------------------------------------------------------
100

101     /**
102      * <p>Gets the default <code>ToStringStyle</code> to use.</p>
103      *
104      * <p>This could allow the <code>ToStringStyle</code> to be
105      * controlled for an entire application with one call.</p>
106      *
107      * <p>This might be used to have a verbose
108      * <code>ToStringStyle</code> during development and a compact
109      * <code>ToStringStyle</code> in production.</p>
110      *
111      * @return the default <code>ToStringStyle</code>
112      */

113     public static ToStringStyle getDefaultStyle() {
114         return defaultStyle;
115     }
116
117     /**
118      * <p>Forwards to <code>ReflectionToStringBuilder</code>.</p>
119      *
120      * @see ReflectionToStringBuilder#toString(Object)
121      */

122     public static String JavaDoc reflectionToString(Object JavaDoc object) {
123         return ReflectionToStringBuilder.toString(object);
124     }
125
126     /**
127      * <p>Forwards to <code>ReflectionToStringBuilder</code>.</p>
128      *
129      * @see ReflectionToStringBuilder#toString(Object,ToStringStyle)
130      */

131     public static String JavaDoc reflectionToString(Object JavaDoc object, ToStringStyle style) {
132         return ReflectionToStringBuilder.toString(object, style);
133     }
134
135     /**
136      * <p>Forwards to <code>ReflectionToStringBuilder</code>.</p>
137      *
138      * @see ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean)
139      */

140     public static String JavaDoc reflectionToString(Object JavaDoc object, ToStringStyle style, boolean outputTransients) {
141         return ReflectionToStringBuilder.toString(object, style, outputTransients, false, null);
142     }
143
144     /**
145      * <p>Forwards to <code>ReflectionToStringBuilder</code>.</p>
146      *
147      * @see ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean,boolean,Class)
148      * @since 2.0
149      */

150     public static String JavaDoc reflectionToString(
151         Object JavaDoc object,
152         ToStringStyle style,
153         boolean outputTransients,
154         Class JavaDoc reflectUpToClass) {
155         return ReflectionToStringBuilder.toString(object, style, outputTransients, false, reflectUpToClass);
156     }
157
158     /**
159      * <p>Sets the default <code>ToStringStyle</code> to use.</p>
160      *
161      * @param style the default <code>ToStringStyle</code>
162      * @throws IllegalArgumentException if the style is <code>null</code>
163      */

164     public static void setDefaultStyle(ToStringStyle style) {
165         if (style == null) {
166             throw new IllegalArgumentException JavaDoc("The style must not be null");
167         }
168         defaultStyle = style;
169     }
170
171     /**
172      * Current toString buffer.
173      */

174     private final StringBuffer JavaDoc buffer;
175
176     /**
177      * The object being output.
178      */

179     private final Object JavaDoc object;
180
181     /**
182      * The style of output to use.
183      */

184     private final ToStringStyle style;
185
186     /**
187      * <p>Constructor for <code>ToStringBuilder</code>.</p>
188      *
189      * <p>This constructor outputs using the default style set with
190      * <code>setDefaultStyle</code>.</p>
191      *
192      * @param object the Object to build a <code>toString</code> for
193      * @throws IllegalArgumentException if the Object passed in is
194      * <code>null</code>
195      */

196     public ToStringBuilder(Object JavaDoc object) {
197         this(object, getDefaultStyle(), null);
198     }
199
200     /**
201      * <p>Constructor for <code>ToStringBuilder</code> specifying the
202      * output style.</p>
203      *
204      * <p>If the style is <code>null</code>, the default style is used.</p>
205      *
206      * @param object the Object to build a <code>toString</code> for
207      * @param style the style of the <code>toString</code> to create,
208      * may be <code>null</code>
209      * @throws IllegalArgumentException if the Object passed in is
210      * <code>null</code>
211      */

212     public ToStringBuilder(Object JavaDoc object, ToStringStyle style) {
213         this(object, style, null);
214     }
215
216     /**
217      * <p>Constructor for <code>ToStringBuilder</code>.</p>
218      *
219      * <p>If the style is <code>null</code>, the default style is used.</p>
220      *
221      * <p>If the buffer is <code>null</code>, a new one is created.</p>
222      *
223      * @param object the Object to build a <code>toString</code> for
224      * @param style the style of the <code>toString</code> to create,
225      * may be <code>null</code>
226      * @param buffer the <code>StringBuffer</code> to populate, may be
227      * <code>null</code>
228      */

229     public ToStringBuilder(Object JavaDoc object, ToStringStyle style, StringBuffer JavaDoc buffer) {
230         if (style == null) {
231             style = getDefaultStyle();
232         }
233         if (buffer == null) {
234             buffer = new StringBuffer JavaDoc(512);
235         }
236         this.buffer = buffer;
237         this.style = style;
238         this.object = object;
239
240         style.appendStart(buffer, object);
241     }
242
243     //----------------------------------------------------------------------------
244

245     /**
246      * <p>Append to the <code>toString</code> a <code>boolean</code>
247      * value.</p>
248      *
249      * @param value the value to add to the <code>toString</code>
250      * @return this
251      */

252     public ToStringBuilder append(boolean value) {
253         style.append(buffer, null, value);
254         return this;
255     }
256
257     //----------------------------------------------------------------------------
258

259     /**
260      * <p>Append to the <code>toString</code> a <code>boolean</code>
261      * array.</p>
262      *
263      * @param array the array to add to the <code>toString</code>
264      * @return this
265      */

266     public ToStringBuilder append(boolean[] array) {
267         style.append(buffer, null, array, null);
268         return this;
269     }
270
271     //----------------------------------------------------------------------------
272

273     /**
274      * <p>Append to the <code>toString</code> a <code>byte</code>
275      * value.</p>
276      *
277      * @param value the value to add to the <code>toString</code>
278      * @return this
279      */

280     public ToStringBuilder append(byte value) {
281         style.append(buffer, null, value);
282         return this;
283     }
284
285     //----------------------------------------------------------------------------
286

287     /**
288      * <p>Append to the <code>toString</code> a <code>byte</code>
289      * array.</p>
290      *
291      * @param array the array to add to the <code>toString</code>
292      * @return this
293      */

294     public ToStringBuilder append(byte[] array) {
295         style.append(buffer, null, array, null);
296         return this;
297     }
298
299     //----------------------------------------------------------------------------
300

301     /**
302      * <p>Append to the <code>toString</code> a <code>char</code>
303      * value.</p>
304      *
305      * @param value the value to add to the <code>toString</code>
306      * @return this
307      */

308     public ToStringBuilder append(char value) {
309         style.append(buffer, null, value);
310         return this;
311     }
312
313     //----------------------------------------------------------------------------
314

315     /**
316      * <p>Append to the <code>toString</code> a <code>char</code>
317      * array.</p>
318      *
319      * @param array the array to add to the <code>toString</code>
320      * @return this
321      */

322     public ToStringBuilder append(char[] array) {
323         style.append(buffer, null, array, null);
324         return this;
325     }
326
327     //----------------------------------------------------------------------------
328

329     /**
330      * <p>Append to the <code>toString</code> a <code>double</code>
331      * value.</p>
332      *
333      * @param value the value to add to the <code>toString</code>
334      * @return this
335      */

336     public ToStringBuilder append(double value) {
337         style.append(buffer, null, value);
338         return this;
339     }
340
341     //----------------------------------------------------------------------------
342

343     /**
344      * <p>Append to the <code>toString</code> a <code>double</code>
345      * array.</p>
346      *
347      * @param array the array to add to the <code>toString</code>
348      * @return this
349      */

350     public ToStringBuilder append(double[] array) {
351         style.append(buffer, null, array, null);
352         return this;
353     }
354
355     //----------------------------------------------------------------------------
356

357     /**
358      * <p>Append to the <code>toString</code> a <code>float</code>
359      * value.</p>
360      *
361      * @param value the value to add to the <code>toString</code>
362      * @return this
363      */

364     public ToStringBuilder append(float value) {
365         style.append(buffer, null, value);
366         return this;
367     }
368
369     //----------------------------------------------------------------------------
370

371     /**
372      * <p>Append to the <code>toString</code> a <code>float</code>
373      * array.</p>
374      *
375      * @param array the array to add to the <code>toString</code>
376      * @return this
377      */

378     public ToStringBuilder append(float[] array) {
379         style.append(buffer, null, array, null);
380         return this;
381     }
382
383     //----------------------------------------------------------------------------
384

385     /**
386      * <p>Append to the <code>toString</code> an <code>int</code>
387      * value.</p>
388      *
389      * @param value the value to add to the <code>toString</code>
390      * @return this
391      */

392     public ToStringBuilder append(int value) {
393         style.append(buffer, null, value);
394         return this;
395     }
396
397     //----------------------------------------------------------------------------
398

399     /**
400      * <p>Append to the <code>toString</code> an <code>int</code>
401      * array.</p>
402      *
403      * @param array the array to add to the <code>toString</code>
404      * @return this
405      */

406     public ToStringBuilder append(int[] array) {
407         style.append(buffer, null, array, null);
408         return this;
409     }
410
411     //----------------------------------------------------------------------------
412

413     /**
414      * <p>Append to the <code>toString</code> a <code>long</code>
415      * value.</p>
416      *
417      * @param value the value to add to the <code>toString</code>
418      * @return this
419      */

420     public ToStringBuilder append(long value) {
421         style.append(buffer, null, value);
422         return this;
423     }
424
425     //----------------------------------------------------------------------------
426

427     /**
428      * <p>Append to the <code>toString</code> a <code>long</code>
429      * array.</p>
430      *
431      * @param array the array to add to the <code>toString</code>
432      * @return this
433      */

434     public ToStringBuilder append(long[] array) {
435         style.append(buffer, null, array, null);
436         return this;
437     }
438
439     //----------------------------------------------------------------------------
440

441     /**
442      * <p>Append to the <code>toString</code> an <code>Object</code>
443      * value.</p>
444      *
445      * @param object the value to add to the <code>toString</code>
446      * @return this
447      */

448     public ToStringBuilder append(Object JavaDoc object) {
449         style.append(buffer, null, object, null);
450         return this;
451     }
452
453     //----------------------------------------------------------------------------
454

455     /**
456      * <p>Append to the <code>toString</code> an <code>Object</code>
457      * array.</p>
458      *
459      * @param array the array to add to the <code>toString</code>
460      * @return this
461      */

462     public ToStringBuilder append(Object JavaDoc[] array) {
463         style.append(buffer, null, array, null);
464         return this;
465     }
466
467     //----------------------------------------------------------------------------
468

469     /**
470      * <p>Append to the <code>toString</code> a <code>short</code>
471      * value.</p>
472      *
473      * @param value the value to add to the <code>toString</code>
474      * @return this
475      */

476     public ToStringBuilder append(short value) {
477         style.append(buffer, null, value);
478         return this;
479     }
480
481     //----------------------------------------------------------------------------
482

483     /**
484      * <p>Append to the <code>toString</code> a <code>short</code>
485      * array.</p>
486      *
487      * @param array the array to add to the <code>toString</code>
488      * @return this
489      */

490     public ToStringBuilder append(short[] array) {
491         style.append(buffer, null, array, null);
492         return this;
493     }
494
495     /**
496      * <p>Append to the <code>toString</code> a <code>boolean</code>
497      * value.</p>
498      *
499      * @param fieldName the field name
500      * @param value the value to add to the <code>toString</code>
501      * @return this
502      */

503     public ToStringBuilder append(String JavaDoc fieldName, boolean value) {
504         style.append(buffer, fieldName, value);
505         return this;
506     }
507
508     /**
509      * <p>Append to the <code>toString</code> a <code>boolean</code>
510      * array.</p>
511      *
512      * @param fieldName the field name
513      * @param array the array to add to the <code>hashCode</code>
514      * @return this
515      */

516     public ToStringBuilder append(String JavaDoc fieldName, boolean[] array) {
517         style.append(buffer, fieldName, array, null);
518         return this;
519     }
520
521     /**
522      * <p>Append to the <code>toString</code> a <code>boolean</code>
523      * array.</p>
524      *
525      * <p>A boolean parameter controls the level of detail to show.
526      * Setting <code>true</code> will output the array in full. Setting
527      * <code>false</code> will output a summary, typically the size of
528      * the array.</p>
529      *
530      * @param fieldName the field name
531      * @param array the array to add to the <code>toString</code>
532      * @param fullDetail <code>true</code> for detail, <code>false</code>
533      * for summary info
534      * @return this
535      */

536     public ToStringBuilder append(String JavaDoc fieldName, boolean[] array, boolean fullDetail) {
537         style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
538         return this;
539     }
540
541     /**
542      * <p>Append to the <code>toString</code> an <code>byte</code>
543      * value.</p>
544      *
545      * @param fieldName the field name
546      * @param value the value to add to the <code>toString</code>
547      * @return this
548      */

549     public ToStringBuilder append(String JavaDoc fieldName, byte value) {
550         style.append(buffer, fieldName, value);
551         return this;
552     }
553
554     /**
555      * <p>Append to the <code>toString</code> a <code>byte</code> array.</p>
556      *
557      * @param fieldName the field name
558      * @param array the array to add to the <code>toString</code>
559      * @return this
560      */

561     public ToStringBuilder append(String JavaDoc fieldName, byte[] array) {
562         style.append(buffer, fieldName, array, null);
563         return this;
564     }
565
566     /**
567      * <p>Append to the <code>toString</code> a <code>byte</code>
568      * array.</p>
569      *
570      * <p>A boolean parameter controls the level of detail to show.
571      * Setting <code>true</code> will output the array in full. Setting
572      * <code>false</code> will output a summary, typically the size of
573      * the array.
574      *
575      * @param fieldName the field name
576      * @param array the array to add to the <code>toString</code>
577      * @param fullDetail <code>true</code> for detail, <code>false</code>
578      * for summary info
579      * @return this
580      */

581     public ToStringBuilder append(String JavaDoc fieldName, byte[] array, boolean fullDetail) {
582         style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
583         return this;
584     }
585
586     /**
587      * <p>Append to the <code>toString</code> a <code>char</code>
588      * value.</p>
589      *
590      * @param fieldName the field name
591      * @param value the value to add to the <code>toString</code>
592      * @return this
593      */

594     public ToStringBuilder append(String JavaDoc fieldName, char value) {
595         style.append(buffer, fieldName, value);
596         return this;
597     }
598
599     /**
600      * <p>Append to the <code>toString</code> a <code>char</code>
601      * array.</p>
602      *
603      * @param fieldName the field name
604      * @param array the array to add to the <code>toString</code>
605      * @return this
606      */

607     public ToStringBuilder append(String JavaDoc fieldName, char[] array) {
608         style.append(buffer, fieldName, array, null);
609         return this;
610     }
611
612     /**
613      * <p>Append to the <code>toString</code> a <code>char</code>
614      * array.</p>
615      *
616      * <p>A boolean parameter controls the level of detail to show.
617      * Setting <code>true</code> will output the array in full. Setting
618      * <code>false</code> will output a summary, typically the size of
619      * the array.</p>
620      *
621      * @param fieldName the field name
622      * @param array the array to add to the <code>toString</code>
623      * @param fullDetail <code>true</code> for detail, <code>false</code>
624      * for summary info
625      * @return this
626      */

627     public ToStringBuilder append(String JavaDoc fieldName, char[] array, boolean fullDetail) {
628         style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
629         return this;
630     }
631
632     /**
633      * <p>Append to the <code>toString</code> a <code>double</code>
634      * value.</p>
635      *
636      * @param fieldName the field name
637      * @param value the value to add to the <code>toString</code>
638      * @return this
639      */

640     public ToStringBuilder append(String JavaDoc fieldName, double value) {
641         style.append(buffer, fieldName, value);
642         return this;
643     }
644
645     /**
646      * <p>Append to the <code>toString</code> a <code>double</code>
647      * array.</p>
648      *
649      * @param fieldName the field name
650      * @param array the array to add to the <code>toString</code>
651      * @return this
652      */

653     public ToStringBuilder append(String JavaDoc fieldName, double[] array) {
654         style.append(buffer, fieldName, array, null);
655         return this;
656     }
657
658     /**
659      * <p>Append to the <code>toString</code> a <code>double</code>
660      * array.</p>
661      *
662      * <p>A boolean parameter controls the level of detail to show.
663      * Setting <code>true</code> will output the array in full. Setting
664      * <code>false</code> will output a summary, typically the size of
665      * the array.</p>
666      *
667      * @param fieldName the field name
668      * @param array the array to add to the <code>toString</code>
669      * @param fullDetail <code>true</code> for detail, <code>false</code>
670      * for summary info
671      * @return this
672      */

673     public ToStringBuilder append(String JavaDoc fieldName, double[] array, boolean fullDetail) {
674         style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
675         return this;
676     }
677
678     /**
679      * <p>Append to the <code>toString</code> an <code>float</code>
680      * value.</p>
681      *
682      * @param fieldName the field name
683      * @param value the value to add to the <code>toString</code>
684      * @return this
685      */

686     public ToStringBuilder append(String JavaDoc fieldName, float value) {
687         style.append(buffer, fieldName, value);
688         return this;
689     }
690
691     /**
692      * <p>Append to the <code>toString</code> a <code>float</code>
693      * array.</p>
694      *
695      * @param fieldName the field name
696      * @param array the array to add to the <code>toString</code>
697      * @return this
698      */

699     public ToStringBuilder append(String JavaDoc fieldName, float[] array) {
700         style.append(buffer, fieldName, array, null);
701         return this;
702     }
703
704     /**
705      * <p>Append to the <code>toString</code> a <code>float</code>
706      * array.</p>
707      *
708      * <p>A boolean parameter controls the level of detail to show.
709      * Setting <code>true</code> will output the array in full. Setting
710      * <code>false</code> will output a summary, typically the size of
711      * the array.</p>
712      *
713      * @param fieldName the field name
714      * @param array the array to add to the <code>toString</code>
715      * @param fullDetail <code>true</code> for detail, <code>false</code>
716      * for summary info
717      * @return this
718      */

719     public ToStringBuilder append(String JavaDoc fieldName, float[] array, boolean fullDetail) {
720         style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
721         return this;
722     }
723
724     /**
725      * <p>Append to the <code>toString</code> an <code>int</code>
726      * value.</p>
727      *
728      * @param fieldName the field name
729      * @param value the value to add to the <code>toString</code>
730      * @return this
731      */

732     public ToStringBuilder append(String JavaDoc fieldName, int value) {
733         style.append(buffer, fieldName, value);
734         return this;
735     }
736
737     /**
738      * <p>Append to the <code>toString</code> an <code>int</code>
739      * array.</p>
740      *
741      * @param fieldName the field name
742      * @param array the array to add to the <code>toString</code>
743      * @return this
744      */

745     public ToStringBuilder append(String JavaDoc fieldName, int[] array) {
746         style.append(buffer, fieldName, array, null);
747         return this;
748     }
749
750     /**
751      * <p>Append to the <code>toString</code> an <code>int</code>
752      * array.</p>
753      *
754      * <p>A boolean parameter controls the level of detail to show.
755      * Setting <code>true</code> will output the array in full. Setting
756      * <code>false</code> will output a summary, typically the size of
757      * the array.</p>
758      *
759      * @param fieldName the field name
760      * @param array the array to add to the <code>toString</code>
761      * @param fullDetail <code>true</code> for detail, <code>false</code>
762      * for summary info
763      * @return this
764      */

765     public ToStringBuilder append(String JavaDoc fieldName, int[] array, boolean fullDetail) {
766         style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
767         return this;
768     }
769
770     /**
771      * <p>Append to the <code>toString</code> a <code>long</code>
772      * value.</p>
773      *
774      * @param fieldName the field name
775      * @param value the value to add to the <code>toString</code>
776      * @return this
777      */

778     public ToStringBuilder append(String JavaDoc fieldName, long value) {
779         style.append(buffer, fieldName, value);
780         return this;
781     }
782
783     /**
784      * <p>Append to the <code>toString</code> a <code>long</code>
785      * array.</p>
786      *
787      * @param fieldName the field name
788      * @param array the array to add to the <code>toString</code>
789      * @return this
790      */

791     public ToStringBuilder append(String JavaDoc fieldName, long[] array) {
792         style.append(buffer, fieldName, array, null);
793         return this;
794     }
795
796     /**
797      * <p>Append to the <code>toString</code> a <code>long</code>
798      * array.</p>
799      *
800      * <p>A boolean parameter controls the level of detail to show.
801      * Setting <code>true</code> will output the array in full. Setting
802      * <code>false</code> will output a summary, typically the size of
803      * the array.</p>
804      *
805      * @param fieldName the field name
806      * @param array the array to add to the <code>toString</code>
807      * @param fullDetail <code>true</code> for detail, <code>false</code>
808      * for summary info
809      * @return this
810      */

811     public ToStringBuilder append(String JavaDoc fieldName, long[] array, boolean fullDetail) {
812         style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
813         return this;
814     }
815
816     /**
817      * <p>Append to the <code>toString</code> an <code>Object</code>
818      * value.</p>
819      *
820      * @param fieldName the field name
821      * @param object the value to add to the <code>toString</code>
822      * @return this
823      */

824     public ToStringBuilder append(String JavaDoc fieldName, Object JavaDoc object) {
825         style.append(buffer, fieldName, object, null);
826         return this;
827     }
828
829     /**
830      * <p>Append to the <code>toString</code> an <code>Object</code>
831      * value.</p>
832      *
833      * @param fieldName the field name
834      * @param object the value to add to the <code>toString</code>
835      * @param fullDetail <code>true</code> for detail,
836      * <code>false</code> for summary info
837      * @return this
838      */

839     public ToStringBuilder append(String JavaDoc fieldName, Object JavaDoc object, boolean fullDetail) {
840         style.append(buffer, fieldName, object, BooleanUtils.toBooleanObject(fullDetail));
841         return this;
842     }
843
844     /**
845      * <p>Append to the <code>toString</code> an <code>Object</code>
846      * array.</p>
847      *
848      * @param fieldName the field name
849      * @param array the array to add to the <code>toString</code>
850      * @return this
851      */

852     public ToStringBuilder append(String JavaDoc fieldName, Object JavaDoc[] array) {
853         style.append(buffer, fieldName, array, null);
854         return this;
855     }
856
857     /**
858      * <p>Append to the <code>toString</code> an <code>Object</code>
859      * array.</p>
860      *
861      * <p>A boolean parameter controls the level of detail to show.
862      * Setting <code>true</code> will output the array in full. Setting
863      * <code>false</code> will output a summary, typically the size of
864      * the array.</p>
865      *
866      * @param fieldName the field name
867      * @param array the array to add to the <code>toString</code>
868      * @param fullDetail <code>true</code> for detail, <code>false</code>
869      * for summary info
870      * @return this
871      */

872     public ToStringBuilder append(String JavaDoc fieldName, Object JavaDoc[] array, boolean fullDetail) {
873         style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
874         return this;
875     }
876
877     /**
878      * <p>Append to the <code>toString</code> an <code>short</code>
879      * value.</p>
880      *
881      * @param fieldName the field name
882      * @param value the value to add to the <code>toString</code>
883      * @return this
884      */

885     public ToStringBuilder append(String JavaDoc fieldName, short value) {
886         style.append(buffer, fieldName, value);
887         return this;
888     }
889
890     /**
891      * <p>Append to the <code>toString</code> a <code>short</code>
892      * array.</p>
893      *
894      * @param fieldName the field name
895      * @param array the array to add to the <code>toString</code>
896      * @return this
897      */

898     public ToStringBuilder append(String JavaDoc fieldName, short[] array) {
899         style.append(buffer, fieldName, array, null);
900         return this;
901     }
902
903     /**
904      * <p>Append to the <code>toString</code> a <code>short</code>
905      * array.</p>
906      *
907      * <p>A boolean parameter controls the level of detail to show.
908      * Setting <code>true</code> will output the array in full. Setting
909      * <code>false</code> will output a summary, typically the size of
910      * the array.
911      *
912      * @param fieldName the field name
913      * @param array the array to add to the <code>toString</code>
914      * @param fullDetail <code>true</code> for detail, <code>false</code>
915      * for summary info
916      * @return this
917      */

918     public ToStringBuilder append(String JavaDoc fieldName, short[] array, boolean fullDetail) {
919         style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
920         return this;
921     }
922
923     /**
924      * <p>Appends with the same format as the default <code>Object toString()
925      * </code> method. Appends the class name followed by
926      * {@link System#identityHashCode(java.lang.Object)}.</p>
927      *
928      * @param object the <code>Object</code> whose class name and id to output
929      * @return this
930      * @since 2.0
931      */

932     public ToStringBuilder appendAsObjectToString(Object JavaDoc object) {
933         ObjectUtils.appendIdentityToString(this.getStringBuffer(), object);
934         return this;
935     }
936
937     //----------------------------------------------------------------------------
938

939     /**
940      * <p>Append the <code>toString</code> from the superclass.</p>
941      *
942      * <p>This method assumes that the superclass uses the same <code>ToStringStyle</code>
943      * as this one.</p>
944      *
945      * <p>If <code>superToString</code> is <code>null</code>, no change is made.</p>
946      *
947      * @param superToString the result of <code>super.toString()</code>
948      * @return this
949      * @since 2.0
950      */

951     public ToStringBuilder appendSuper(String JavaDoc superToString) {
952         if (superToString != null) {
953             style.appendSuper(buffer, superToString);
954         }
955         return this;
956     }
957
958     /**
959      * <p>Append the <code>toString</code> from another object.</p>
960      *
961      * <p>This method is useful where a class delegates most of the implementation of
962      * its properties to another class. You can then call <code>toString()</code> on
963      * the other class and pass the result into this method.</p>
964      *
965      * <pre>
966      * private AnotherObject delegate;
967      * private String fieldInThisClass;
968      *
969      * public String toString() {
970      * return new ToStringBuilder(this).
971      * appendToString(delegate.toString()).
972      * append(fieldInThisClass).
973      * toString();
974      * }</pre>
975      *
976      * <p>This method assumes that the other object uses the same <code>ToStringStyle</code>
977      * as this one.</p>
978      *
979      * <p>If the <code>toString</code> is <code>null</code>, no change is made.</p>
980      *
981      * @param toString the result of <code>toString()</code> on another object
982      * @return this
983      * @since 2.0
984      */

985     public ToStringBuilder appendToString(String JavaDoc toString) {
986         if (toString != null) {
987             style.appendToString(buffer, toString);
988         }
989         return this;
990     }
991
992     /**
993      * <p>Returns the <code>Object</code> being output.</p>
994      *
995      * @return The object being output.
996      * @since 2.0
997      */

998     public Object JavaDoc getObject() {
999         return object;
1000    }
1001
1002    /**
1003     * <p>Gets the <code>StringBuffer</code> being populated.</p>
1004     *
1005     * @return the <code>StringBuffer</code> being populated
1006     */

1007    public StringBuffer JavaDoc getStringBuffer() {
1008        return buffer;
1009    }
1010
1011    //----------------------------------------------------------------------------
1012

1013    /**
1014     * <p>Gets the <code>ToStringStyle</code> being used.</p>
1015     *
1016     * @return the <code>ToStringStyle</code> being used
1017     * @since 2.0
1018     */

1019    public ToStringStyle getStyle() {
1020        return style;
1021    }
1022
1023    /**
1024     * <p>Returns the built <code>toString</code>.</p>
1025     *
1026     * <p>This method appends the end of data indicator, and can only be called once.
1027     * Use {@link #getStringBuffer} to get the current string state.</p>
1028     *
1029     * <p>If the object is <code>null</code>, return the style's <code>nullText</code></p>
1030     *
1031     * @return the String <code>toString</code>
1032     */

1033    public String JavaDoc toString() {
1034        if (this.getObject() == null) {
1035            this.getStringBuffer().append(this.getStyle().getNullText());
1036        } else {
1037            style.appendEnd(this.getStringBuffer(), this.getObject());
1038        }
1039        return this.getStringBuffer().toString();
1040    }
1041
1042}
1043
Popular Tags