Source Code Cross Referenced for ToStringBuilder.java in  » Library » Apache-common-lang » org » apache » commons » lang » builder » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache common lang » org.apache.commons.lang.builder 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * Licensed to the Apache Software Foundation (ASF) under one or more
0003:         * contributor license agreements.  See the NOTICE file distributed with
0004:         * this work for additional information regarding copyright ownership.
0005:         * The ASF licenses this file to You under the Apache License, Version 2.0
0006:         * (the "License"); you may not use this file except in compliance with
0007:         * the License.  You may obtain a copy of the License at
0008:         * 
0009:         *      http://www.apache.org/licenses/LICENSE-2.0
0010:         * 
0011:         * Unless required by applicable law or agreed to in writing, software
0012:         * distributed under the License is distributed on an "AS IS" BASIS,
0013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         * See the License for the specific language governing permissions and
0015:         * limitations under the License.
0016:         */
0017:        package org.apache.commons.lang.builder;
0018:
0019:        import org.apache.commons.lang.BooleanUtils;
0020:        import org.apache.commons.lang.ObjectUtils;
0021:
0022:        /**
0023:         * <p>Assists in implementing {@link Object#toString()} methods.</p>
0024:         *
0025:         * <p>This class enables a good and consistent <code>toString()</code> to be built for any
0026:         * class or object. This class aims to simplify the process by:</p>
0027:         * <ul>
0028:         *  <li>allowing field names</li>
0029:         *  <li>handling all types consistently</li>
0030:         *  <li>handling nulls consistently</li>
0031:         *  <li>outputting arrays and multi-dimensional arrays</li>
0032:         *  <li>enabling the detail level to be controlled for Objects and Collections</li>
0033:         *  <li>handling class hierarchies</li>
0034:         * </ul>
0035:         *
0036:         * <p>To use this class write code as follows:</p>
0037:         *
0038:         * <pre>
0039:         * public class Person {
0040:         *   String name;
0041:         *   int age;
0042:         *   boolean smoker;
0043:         * 
0044:         *   ...
0045:         * 
0046:         *   public String toString() {
0047:         *     return new ToStringBuilder(this).
0048:         *       append("name", name).
0049:         *       append("age", age).
0050:         *       append("smoker", smoker).
0051:         *       toString();
0052:         *   }
0053:         * }
0054:         * </pre>
0055:         *
0056:         * <p>This will produce a toString of the format:
0057:         * <code>Person@7f54[name=Stephen,age=29,smoker=false]</code></p>
0058:         * 
0059:         * <p>To add the superclass <code>toString</code>, use {@link #appendSuper}.
0060:         * To append the <code>toString</code> from an object that is delegated
0061:         * to (or any other object), use {@link #appendToString}.</p>
0062:         *
0063:         * <p>Alternatively, there is a method that uses reflection to determine
0064:         * the fields to test. Because these fields are usually private, the method, 
0065:         * <code>reflectionToString</code>, uses <code>AccessibleObject.setAccessible</code> to
0066:         * change the visibility of the fields. This will fail under a security manager,
0067:         * unless the appropriate permissions are set up correctly. It is also
0068:         * slower than testing explicitly.</p>
0069:         *
0070:         * <p>A typical invocation for this method would look like:</p>
0071:         *
0072:         * <pre>
0073:         * public String toString() {
0074:         *   return ToStringBuilder.reflectionToString(this);
0075:         * }
0076:         * </pre>
0077:         *
0078:         * <p>You can also use the builder to debug 3rd party objects:</p>
0079:         *
0080:         * <pre>
0081:         * System.out.println("An object: " + ToStringBuilder.reflectionToString(anObject));
0082:         * </pre>
0083:         * 
0084:         * <p>The exact format of the <code>toString</code> is determined by
0085:         * the {@link ToStringStyle} passed into the constructor.</p>
0086:         *
0087:         * @author Stephen Colebourne
0088:         * @author Gary Gregory
0089:         * @author Pete Gieser
0090:         * @since 1.0
0091:         * @version $Id: ToStringBuilder.java 492354 2007-01-03 23:48:10Z scolebourne $
0092:         */
0093:        public class ToStringBuilder {
0094:
0095:            /**
0096:             * The default style of output to use.
0097:             */
0098:            private static ToStringStyle defaultStyle = ToStringStyle.DEFAULT_STYLE;
0099:
0100:            //----------------------------------------------------------------------------
0101:
0102:            /**
0103:             * <p>Gets the default <code>ToStringStyle</code> to use.</p>
0104:             *
0105:             * <p>This could allow the <code>ToStringStyle</code> to be
0106:             * controlled for an entire application with one call.</p>
0107:             *
0108:             * <p>This might be used to have a verbose
0109:             * <code>ToStringStyle</code> during development and a compact
0110:             * <code>ToStringStyle</code> in production.</p>
0111:             * 
0112:             * @return the default <code>ToStringStyle</code>
0113:             */
0114:            public static ToStringStyle getDefaultStyle() {
0115:                return defaultStyle;
0116:            }
0117:
0118:            /**
0119:             * <p>Forwards to <code>ReflectionToStringBuilder</code>.</p>
0120:             * 
0121:             * @param object  the Object to be output
0122:             * @return the String result
0123:             * @see ReflectionToStringBuilder#toString(Object)
0124:             */
0125:            public static String reflectionToString(Object object) {
0126:                return ReflectionToStringBuilder.toString(object);
0127:            }
0128:
0129:            /**
0130:             * <p>Forwards to <code>ReflectionToStringBuilder</code>.</p>
0131:             * 
0132:             * @param object  the Object to be output
0133:             * @param style  the style of the <code>toString</code> to create, may be <code>null</code>
0134:             * @return the String result
0135:             * @see ReflectionToStringBuilder#toString(Object,ToStringStyle)
0136:             */
0137:            public static String reflectionToString(Object object,
0138:                    ToStringStyle style) {
0139:                return ReflectionToStringBuilder.toString(object, style);
0140:            }
0141:
0142:            /**
0143:             * <p>Forwards to <code>ReflectionToStringBuilder</code>.</p>
0144:             * 
0145:             * @param object  the Object to be output
0146:             * @param style  the style of the <code>toString</code> to create, may be <code>null</code>
0147:             * @param outputTransients  whether to include transient fields
0148:             * @return the String result
0149:             * @see ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean)
0150:             */
0151:            public static String reflectionToString(Object object,
0152:                    ToStringStyle style, boolean outputTransients) {
0153:                return ReflectionToStringBuilder.toString(object, style,
0154:                        outputTransients, false, null);
0155:            }
0156:
0157:            /**
0158:             * <p>Forwards to <code>ReflectionToStringBuilder</code>.</p>
0159:             * 
0160:             * @param object  the Object to be output
0161:             * @param style  the style of the <code>toString</code> to create, may be <code>null</code>
0162:             * @param outputTransients  whether to include transient fields
0163:             * @param reflectUpToClass  the superclass to reflect up to (inclusive), may be <code>null</code>
0164:             * @return the String result
0165:             * @see ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean,boolean,Class)
0166:             * @since 2.0
0167:             */
0168:            public static String reflectionToString(Object object,
0169:                    ToStringStyle style, boolean outputTransients,
0170:                    Class reflectUpToClass) {
0171:                return ReflectionToStringBuilder.toString(object, style,
0172:                        outputTransients, false, reflectUpToClass);
0173:            }
0174:
0175:            /**
0176:             * <p>Sets the default <code>ToStringStyle</code> to use.</p>
0177:             * 
0178:             * @param style  the default <code>ToStringStyle</code>
0179:             * @throws IllegalArgumentException if the style is <code>null</code>
0180:             */
0181:            public static void setDefaultStyle(ToStringStyle style) {
0182:                if (style == null) {
0183:                    throw new IllegalArgumentException(
0184:                            "The style must not be null");
0185:                }
0186:                defaultStyle = style;
0187:            }
0188:
0189:            /**
0190:             * Current toString buffer.
0191:             */
0192:            private final StringBuffer buffer;
0193:
0194:            /**
0195:             * The object being output.
0196:             */
0197:            private final Object object;
0198:
0199:            /**
0200:             * The style of output to use.
0201:             */
0202:            private final ToStringStyle style;
0203:
0204:            /**
0205:             * <p>Constructor for <code>ToStringBuilder</code>.</p>
0206:             *
0207:             * <p>This constructor outputs using the default style set with
0208:             * <code>setDefaultStyle</code>.</p>
0209:             * 
0210:             * @param object  the Object to build a <code>toString</code> for
0211:             * @throws IllegalArgumentException  if the Object passed in is
0212:             *  <code>null</code>
0213:             */
0214:            public ToStringBuilder(Object object) {
0215:                this (object, getDefaultStyle(), null);
0216:            }
0217:
0218:            /**
0219:             * <p>Constructor for <code>ToStringBuilder</code> specifying the
0220:             * output style.</p>
0221:             *
0222:             * <p>If the style is <code>null</code>, the default style is used.</p>
0223:             * 
0224:             * @param object  the Object to build a <code>toString</code> for
0225:             * @param style  the style of the <code>toString</code> to create,
0226:             *  may be <code>null</code>
0227:             * @throws IllegalArgumentException  if the Object passed in is
0228:             *  <code>null</code>
0229:             */
0230:            public ToStringBuilder(Object object, ToStringStyle style) {
0231:                this (object, style, null);
0232:            }
0233:
0234:            /**
0235:             * <p>Constructor for <code>ToStringBuilder</code>.</p>
0236:             *
0237:             * <p>If the style is <code>null</code>, the default style is used.</p>
0238:             *
0239:             * <p>If the buffer is <code>null</code>, a new one is created.</p>
0240:             * 
0241:             * @param object  the Object to build a <code>toString</code> for
0242:             * @param style  the style of the <code>toString</code> to create,
0243:             *  may be <code>null</code>
0244:             * @param buffer  the <code>StringBuffer</code> to populate, may be
0245:             *  <code>null</code>
0246:             */
0247:            public ToStringBuilder(Object object, ToStringStyle style,
0248:                    StringBuffer buffer) {
0249:                if (style == null) {
0250:                    style = getDefaultStyle();
0251:                }
0252:                if (buffer == null) {
0253:                    buffer = new StringBuffer(512);
0254:                }
0255:                this .buffer = buffer;
0256:                this .style = style;
0257:                this .object = object;
0258:
0259:                style.appendStart(buffer, object);
0260:            }
0261:
0262:            //----------------------------------------------------------------------------
0263:
0264:            /**
0265:             * <p>Append to the <code>toString</code> a <code>boolean</code>
0266:             * value.</p>
0267:             *
0268:             * @param value  the value to add to the <code>toString</code>
0269:             * @return this
0270:             */
0271:            public ToStringBuilder append(boolean value) {
0272:                style.append(buffer, null, value);
0273:                return this ;
0274:            }
0275:
0276:            //----------------------------------------------------------------------------
0277:
0278:            /**
0279:             * <p>Append to the <code>toString</code> a <code>boolean</code>
0280:             * array.</p>
0281:             *
0282:             * @param array  the array to add to the <code>toString</code>
0283:             * @return this
0284:             */
0285:            public ToStringBuilder append(boolean[] array) {
0286:                style.append(buffer, null, array, null);
0287:                return this ;
0288:            }
0289:
0290:            //----------------------------------------------------------------------------
0291:
0292:            /**
0293:             * <p>Append to the <code>toString</code> a <code>byte</code>
0294:             * value.</p>
0295:             *
0296:             * @param value  the value to add to the <code>toString</code>
0297:             * @return this
0298:             */
0299:            public ToStringBuilder append(byte value) {
0300:                style.append(buffer, null, value);
0301:                return this ;
0302:            }
0303:
0304:            //----------------------------------------------------------------------------
0305:
0306:            /**
0307:             * <p>Append to the <code>toString</code> a <code>byte</code>
0308:             * array.</p>
0309:             *
0310:             * @param array  the array to add to the <code>toString</code>
0311:             * @return this
0312:             */
0313:            public ToStringBuilder append(byte[] array) {
0314:                style.append(buffer, null, array, null);
0315:                return this ;
0316:            }
0317:
0318:            //----------------------------------------------------------------------------
0319:
0320:            /**
0321:             * <p>Append to the <code>toString</code> a <code>char</code>
0322:             * value.</p>
0323:             *
0324:             * @param value  the value to add to the <code>toString</code>
0325:             * @return this
0326:             */
0327:            public ToStringBuilder append(char value) {
0328:                style.append(buffer, null, value);
0329:                return this ;
0330:            }
0331:
0332:            //----------------------------------------------------------------------------
0333:
0334:            /**
0335:             * <p>Append to the <code>toString</code> a <code>char</code>
0336:             * array.</p>
0337:             *
0338:             * @param array  the array to add to the <code>toString</code>
0339:             * @return this
0340:             */
0341:            public ToStringBuilder append(char[] array) {
0342:                style.append(buffer, null, array, null);
0343:                return this ;
0344:            }
0345:
0346:            //----------------------------------------------------------------------------
0347:
0348:            /**
0349:             * <p>Append to the <code>toString</code> a <code>double</code>
0350:             * value.</p>
0351:             *
0352:             * @param value  the value to add to the <code>toString</code>
0353:             * @return this
0354:             */
0355:            public ToStringBuilder append(double value) {
0356:                style.append(buffer, null, value);
0357:                return this ;
0358:            }
0359:
0360:            //----------------------------------------------------------------------------
0361:
0362:            /**
0363:             * <p>Append to the <code>toString</code> a <code>double</code>
0364:             * array.</p>
0365:             *
0366:             * @param array  the array to add to the <code>toString</code>
0367:             * @return this
0368:             */
0369:            public ToStringBuilder append(double[] array) {
0370:                style.append(buffer, null, array, null);
0371:                return this ;
0372:            }
0373:
0374:            //----------------------------------------------------------------------------
0375:
0376:            /**
0377:             * <p>Append to the <code>toString</code> a <code>float</code>
0378:             * value.</p>
0379:             *
0380:             * @param value  the value to add to the <code>toString</code>
0381:             * @return this
0382:             */
0383:            public ToStringBuilder append(float value) {
0384:                style.append(buffer, null, value);
0385:                return this ;
0386:            }
0387:
0388:            //----------------------------------------------------------------------------
0389:
0390:            /**
0391:             * <p>Append to the <code>toString</code> a <code>float</code>
0392:             * array.</p>
0393:             *
0394:             * @param array  the array to add to the <code>toString</code>
0395:             * @return this
0396:             */
0397:            public ToStringBuilder append(float[] array) {
0398:                style.append(buffer, null, array, null);
0399:                return this ;
0400:            }
0401:
0402:            //----------------------------------------------------------------------------
0403:
0404:            /**
0405:             * <p>Append to the <code>toString</code> an <code>int</code>
0406:             * value.</p>
0407:             *
0408:             * @param value  the value to add to the <code>toString</code>
0409:             * @return this
0410:             */
0411:            public ToStringBuilder append(int value) {
0412:                style.append(buffer, null, value);
0413:                return this ;
0414:            }
0415:
0416:            //----------------------------------------------------------------------------
0417:
0418:            /**
0419:             * <p>Append to the <code>toString</code> an <code>int</code>
0420:             * array.</p>
0421:             *
0422:             * @param array  the array to add to the <code>toString</code>
0423:             * @return this
0424:             */
0425:            public ToStringBuilder append(int[] array) {
0426:                style.append(buffer, null, array, null);
0427:                return this ;
0428:            }
0429:
0430:            //----------------------------------------------------------------------------
0431:
0432:            /**
0433:             * <p>Append to the <code>toString</code> a <code>long</code>
0434:             * value.</p>
0435:             *
0436:             * @param value  the value to add to the <code>toString</code>
0437:             * @return this
0438:             */
0439:            public ToStringBuilder append(long value) {
0440:                style.append(buffer, null, value);
0441:                return this ;
0442:            }
0443:
0444:            //----------------------------------------------------------------------------
0445:
0446:            /**
0447:             * <p>Append to the <code>toString</code> a <code>long</code>
0448:             * array.</p>
0449:             *
0450:             * @param array  the array to add to the <code>toString</code>
0451:             * @return this
0452:             */
0453:            public ToStringBuilder append(long[] array) {
0454:                style.append(buffer, null, array, null);
0455:                return this ;
0456:            }
0457:
0458:            //----------------------------------------------------------------------------
0459:
0460:            /**
0461:             * <p>Append to the <code>toString</code> an <code>Object</code>
0462:             * value.</p>
0463:             *
0464:             * @param obj  the value to add to the <code>toString</code>
0465:             * @return this
0466:             */
0467:            public ToStringBuilder append(Object obj) {
0468:                style.append(buffer, null, obj, null);
0469:                return this ;
0470:            }
0471:
0472:            //----------------------------------------------------------------------------
0473:
0474:            /**
0475:             * <p>Append to the <code>toString</code> an <code>Object</code>
0476:             * array.</p>
0477:             *
0478:             * @param array  the array to add to the <code>toString</code>
0479:             * @return this
0480:             */
0481:            public ToStringBuilder append(Object[] array) {
0482:                style.append(buffer, null, array, null);
0483:                return this ;
0484:            }
0485:
0486:            //----------------------------------------------------------------------------
0487:
0488:            /**
0489:             * <p>Append to the <code>toString</code> a <code>short</code>
0490:             * value.</p>
0491:             *
0492:             * @param value  the value to add to the <code>toString</code>
0493:             * @return this
0494:             */
0495:            public ToStringBuilder append(short value) {
0496:                style.append(buffer, null, value);
0497:                return this ;
0498:            }
0499:
0500:            //----------------------------------------------------------------------------
0501:
0502:            /**
0503:             * <p>Append to the <code>toString</code> a <code>short</code>
0504:             * array.</p>
0505:             *
0506:             * @param array  the array to add to the <code>toString</code>
0507:             * @return this
0508:             */
0509:            public ToStringBuilder append(short[] array) {
0510:                style.append(buffer, null, array, null);
0511:                return this ;
0512:            }
0513:
0514:            /**
0515:             * <p>Append to the <code>toString</code> a <code>boolean</code>
0516:             * value.</p>
0517:             *
0518:             * @param fieldName  the field name
0519:             * @param value  the value to add to the <code>toString</code>
0520:             * @return this
0521:             */
0522:            public ToStringBuilder append(String fieldName, boolean value) {
0523:                style.append(buffer, fieldName, value);
0524:                return this ;
0525:            }
0526:
0527:            /**
0528:             * <p>Append to the <code>toString</code> a <code>boolean</code>
0529:             * array.</p>
0530:             *
0531:             * @param fieldName  the field name
0532:             * @param array  the array to add to the <code>hashCode</code>
0533:             * @return this
0534:             */
0535:            public ToStringBuilder append(String fieldName, boolean[] array) {
0536:                style.append(buffer, fieldName, array, null);
0537:                return this ;
0538:            }
0539:
0540:            /**
0541:             * <p>Append to the <code>toString</code> a <code>boolean</code>
0542:             * array.</p>
0543:             *
0544:             * <p>A boolean parameter controls the level of detail to show.
0545:             * Setting <code>true</code> will output the array in full. Setting
0546:             * <code>false</code> will output a summary, typically the size of
0547:             * the array.</p>
0548:             *
0549:             * @param fieldName  the field name
0550:             * @param array  the array to add to the <code>toString</code>
0551:             * @param fullDetail  <code>true</code> for detail, <code>false</code>
0552:             *  for summary info
0553:             * @return this
0554:             */
0555:            public ToStringBuilder append(String fieldName, boolean[] array,
0556:                    boolean fullDetail) {
0557:                style.append(buffer, fieldName, array, BooleanUtils
0558:                        .toBooleanObject(fullDetail));
0559:                return this ;
0560:            }
0561:
0562:            /**
0563:             * <p>Append to the <code>toString</code> an <code>byte</code>
0564:             * value.</p>
0565:             *
0566:             * @param fieldName  the field name
0567:             * @param value  the value to add to the <code>toString</code>
0568:             * @return this
0569:             */
0570:            public ToStringBuilder append(String fieldName, byte value) {
0571:                style.append(buffer, fieldName, value);
0572:                return this ;
0573:            }
0574:
0575:            /**
0576:             * <p>Append to the <code>toString</code> a <code>byte</code> array.</p>
0577:             *
0578:             * @param fieldName  the field name
0579:             * @param array  the array to add to the <code>toString</code>
0580:             * @return this
0581:             */
0582:            public ToStringBuilder append(String fieldName, byte[] array) {
0583:                style.append(buffer, fieldName, array, null);
0584:                return this ;
0585:            }
0586:
0587:            /**
0588:             * <p>Append to the <code>toString</code> a <code>byte</code>
0589:             * array.</p>
0590:             *
0591:             * <p>A boolean parameter controls the level of detail to show.
0592:             * Setting <code>true</code> will output the array in full. Setting
0593:             * <code>false</code> will output a summary, typically the size of
0594:             * the array.
0595:             *
0596:             * @param fieldName  the field name
0597:             * @param array  the array to add to the <code>toString</code>
0598:             * @param fullDetail  <code>true</code> for detail, <code>false</code>
0599:             *  for summary info
0600:             * @return this
0601:             */
0602:            public ToStringBuilder append(String fieldName, byte[] array,
0603:                    boolean fullDetail) {
0604:                style.append(buffer, fieldName, array, BooleanUtils
0605:                        .toBooleanObject(fullDetail));
0606:                return this ;
0607:            }
0608:
0609:            /**
0610:             * <p>Append to the <code>toString</code> a <code>char</code>
0611:             * value.</p>
0612:             *
0613:             * @param fieldName  the field name
0614:             * @param value  the value to add to the <code>toString</code>
0615:             * @return this
0616:             */
0617:            public ToStringBuilder append(String fieldName, char value) {
0618:                style.append(buffer, fieldName, value);
0619:                return this ;
0620:            }
0621:
0622:            /**
0623:             * <p>Append to the <code>toString</code> a <code>char</code>
0624:             * array.</p>
0625:             *
0626:             * @param fieldName  the field name
0627:             * @param array  the array to add to the <code>toString</code>
0628:             * @return this
0629:             */
0630:            public ToStringBuilder append(String fieldName, char[] array) {
0631:                style.append(buffer, fieldName, array, null);
0632:                return this ;
0633:            }
0634:
0635:            /**
0636:             * <p>Append to the <code>toString</code> a <code>char</code>
0637:             * array.</p>
0638:             *
0639:             * <p>A boolean parameter controls the level of detail to show.
0640:             * Setting <code>true</code> will output the array in full. Setting
0641:             * <code>false</code> will output a summary, typically the size of
0642:             * the array.</p>
0643:             *
0644:             * @param fieldName  the field name
0645:             * @param array  the array to add to the <code>toString</code>
0646:             * @param fullDetail  <code>true</code> for detail, <code>false</code>
0647:             *  for summary info
0648:             * @return this
0649:             */
0650:            public ToStringBuilder append(String fieldName, char[] array,
0651:                    boolean fullDetail) {
0652:                style.append(buffer, fieldName, array, BooleanUtils
0653:                        .toBooleanObject(fullDetail));
0654:                return this ;
0655:            }
0656:
0657:            /**
0658:             * <p>Append to the <code>toString</code> a <code>double</code>
0659:             * value.</p>
0660:             *
0661:             * @param fieldName  the field name
0662:             * @param value  the value to add to the <code>toString</code>
0663:             * @return this
0664:             */
0665:            public ToStringBuilder append(String fieldName, double value) {
0666:                style.append(buffer, fieldName, value);
0667:                return this ;
0668:            }
0669:
0670:            /**
0671:             * <p>Append to the <code>toString</code> a <code>double</code>
0672:             * array.</p>
0673:             *
0674:             * @param fieldName  the field name
0675:             * @param array  the array to add to the <code>toString</code>
0676:             * @return this
0677:             */
0678:            public ToStringBuilder append(String fieldName, double[] array) {
0679:                style.append(buffer, fieldName, array, null);
0680:                return this ;
0681:            }
0682:
0683:            /**
0684:             * <p>Append to the <code>toString</code> a <code>double</code>
0685:             * array.</p>
0686:             *
0687:             * <p>A boolean parameter controls the level of detail to show.
0688:             * Setting <code>true</code> will output the array in full. Setting
0689:             * <code>false</code> will output a summary, typically the size of
0690:             * the array.</p>
0691:             *
0692:             * @param fieldName  the field name
0693:             * @param array  the array to add to the <code>toString</code>
0694:             * @param fullDetail  <code>true</code> for detail, <code>false</code>
0695:             *  for summary info
0696:             * @return this
0697:             */
0698:            public ToStringBuilder append(String fieldName, double[] array,
0699:                    boolean fullDetail) {
0700:                style.append(buffer, fieldName, array, BooleanUtils
0701:                        .toBooleanObject(fullDetail));
0702:                return this ;
0703:            }
0704:
0705:            /**
0706:             * <p>Append to the <code>toString</code> an <code>float</code>
0707:             * value.</p>
0708:             *
0709:             * @param fieldName  the field name
0710:             * @param value  the value to add to the <code>toString</code>
0711:             * @return this
0712:             */
0713:            public ToStringBuilder append(String fieldName, float value) {
0714:                style.append(buffer, fieldName, value);
0715:                return this ;
0716:            }
0717:
0718:            /**
0719:             * <p>Append to the <code>toString</code> a <code>float</code>
0720:             * array.</p>
0721:             *
0722:             * @param fieldName  the field name
0723:             * @param array  the array to add to the <code>toString</code>
0724:             * @return this
0725:             */
0726:            public ToStringBuilder append(String fieldName, float[] array) {
0727:                style.append(buffer, fieldName, array, null);
0728:                return this ;
0729:            }
0730:
0731:            /**
0732:             * <p>Append to the <code>toString</code> a <code>float</code>
0733:             * array.</p>
0734:             *
0735:             * <p>A boolean parameter controls the level of detail to show.
0736:             * Setting <code>true</code> will output the array in full. Setting
0737:             * <code>false</code> will output a summary, typically the size of
0738:             * the array.</p>
0739:             *
0740:             * @param fieldName  the field name
0741:             * @param array  the array to add to the <code>toString</code>
0742:             * @param fullDetail  <code>true</code> for detail, <code>false</code>
0743:             *  for summary info
0744:             * @return this
0745:             */
0746:            public ToStringBuilder append(String fieldName, float[] array,
0747:                    boolean fullDetail) {
0748:                style.append(buffer, fieldName, array, BooleanUtils
0749:                        .toBooleanObject(fullDetail));
0750:                return this ;
0751:            }
0752:
0753:            /**
0754:             * <p>Append to the <code>toString</code> an <code>int</code>
0755:             * value.</p>
0756:             *
0757:             * @param fieldName  the field name
0758:             * @param value  the value to add to the <code>toString</code>
0759:             * @return this
0760:             */
0761:            public ToStringBuilder append(String fieldName, int value) {
0762:                style.append(buffer, fieldName, value);
0763:                return this ;
0764:            }
0765:
0766:            /**
0767:             * <p>Append to the <code>toString</code> an <code>int</code>
0768:             * array.</p>
0769:             *
0770:             * @param fieldName  the field name
0771:             * @param array  the array to add to the <code>toString</code>
0772:             * @return this
0773:             */
0774:            public ToStringBuilder append(String fieldName, int[] array) {
0775:                style.append(buffer, fieldName, array, null);
0776:                return this ;
0777:            }
0778:
0779:            /**
0780:             * <p>Append to the <code>toString</code> an <code>int</code>
0781:             * array.</p>
0782:             *
0783:             * <p>A boolean parameter controls the level of detail to show.
0784:             * Setting <code>true</code> will output the array in full. Setting
0785:             * <code>false</code> will output a summary, typically the size of
0786:             * the array.</p>
0787:             *
0788:             * @param fieldName  the field name
0789:             * @param array  the array to add to the <code>toString</code>
0790:             * @param fullDetail  <code>true</code> for detail, <code>false</code>
0791:             *  for summary info
0792:             * @return this
0793:             */
0794:            public ToStringBuilder append(String fieldName, int[] array,
0795:                    boolean fullDetail) {
0796:                style.append(buffer, fieldName, array, BooleanUtils
0797:                        .toBooleanObject(fullDetail));
0798:                return this ;
0799:            }
0800:
0801:            /**
0802:             * <p>Append to the <code>toString</code> a <code>long</code>
0803:             * value.</p>
0804:             *
0805:             * @param fieldName  the field name
0806:             * @param value  the value to add to the <code>toString</code>
0807:             * @return this
0808:             */
0809:            public ToStringBuilder append(String fieldName, long value) {
0810:                style.append(buffer, fieldName, value);
0811:                return this ;
0812:            }
0813:
0814:            /**
0815:             * <p>Append to the <code>toString</code> a <code>long</code>
0816:             * array.</p>
0817:             *
0818:             * @param fieldName  the field name
0819:             * @param array  the array to add to the <code>toString</code>
0820:             * @return this
0821:             */
0822:            public ToStringBuilder append(String fieldName, long[] array) {
0823:                style.append(buffer, fieldName, array, null);
0824:                return this ;
0825:            }
0826:
0827:            /**
0828:             * <p>Append to the <code>toString</code> a <code>long</code>
0829:             * array.</p>
0830:             *
0831:             * <p>A boolean parameter controls the level of detail to show.
0832:             * Setting <code>true</code> will output the array in full. Setting
0833:             * <code>false</code> will output a summary, typically the size of
0834:             * the array.</p>
0835:             *
0836:             * @param fieldName  the field name
0837:             * @param array  the array to add to the <code>toString</code>
0838:             * @param fullDetail  <code>true</code> for detail, <code>false</code>
0839:             *  for summary info
0840:             * @return this
0841:             */
0842:            public ToStringBuilder append(String fieldName, long[] array,
0843:                    boolean fullDetail) {
0844:                style.append(buffer, fieldName, array, BooleanUtils
0845:                        .toBooleanObject(fullDetail));
0846:                return this ;
0847:            }
0848:
0849:            /**
0850:             * <p>Append to the <code>toString</code> an <code>Object</code>
0851:             * value.</p>
0852:             *
0853:             * @param fieldName  the field name
0854:             * @param obj  the value to add to the <code>toString</code>
0855:             * @return this
0856:             */
0857:            public ToStringBuilder append(String fieldName, Object obj) {
0858:                style.append(buffer, fieldName, obj, null);
0859:                return this ;
0860:            }
0861:
0862:            /**
0863:             * <p>Append to the <code>toString</code> an <code>Object</code>
0864:             * value.</p>
0865:             *
0866:             * @param fieldName  the field name
0867:             * @param obj  the value to add to the <code>toString</code>
0868:             * @param fullDetail  <code>true</code> for detail,
0869:             *  <code>false</code> for summary info
0870:             * @return this
0871:             */
0872:            public ToStringBuilder append(String fieldName, Object obj,
0873:                    boolean fullDetail) {
0874:                style.append(buffer, fieldName, obj, BooleanUtils
0875:                        .toBooleanObject(fullDetail));
0876:                return this ;
0877:            }
0878:
0879:            /**
0880:             * <p>Append to the <code>toString</code> an <code>Object</code>
0881:             * array.</p>
0882:             *
0883:             * @param fieldName  the field name
0884:             * @param array  the array to add to the <code>toString</code>
0885:             * @return this
0886:             */
0887:            public ToStringBuilder append(String fieldName, Object[] array) {
0888:                style.append(buffer, fieldName, array, null);
0889:                return this ;
0890:            }
0891:
0892:            /**
0893:             * <p>Append to the <code>toString</code> an <code>Object</code>
0894:             * array.</p>
0895:             *
0896:             * <p>A boolean parameter controls the level of detail to show.
0897:             * Setting <code>true</code> will output the array in full. Setting
0898:             * <code>false</code> will output a summary, typically the size of
0899:             * the array.</p>
0900:             *
0901:             * @param fieldName  the field name
0902:             * @param array  the array to add to the <code>toString</code>
0903:             * @param fullDetail  <code>true</code> for detail, <code>false</code>
0904:             *  for summary info
0905:             * @return this
0906:             */
0907:            public ToStringBuilder append(String fieldName, Object[] array,
0908:                    boolean fullDetail) {
0909:                style.append(buffer, fieldName, array, BooleanUtils
0910:                        .toBooleanObject(fullDetail));
0911:                return this ;
0912:            }
0913:
0914:            /**
0915:             * <p>Append to the <code>toString</code> an <code>short</code>
0916:             * value.</p>
0917:             *
0918:             * @param fieldName  the field name
0919:             * @param value  the value to add to the <code>toString</code>
0920:             * @return this
0921:             */
0922:            public ToStringBuilder append(String fieldName, short value) {
0923:                style.append(buffer, fieldName, value);
0924:                return this ;
0925:            }
0926:
0927:            /**
0928:             * <p>Append to the <code>toString</code> a <code>short</code>
0929:             * array.</p>
0930:             *
0931:             * @param fieldName  the field name
0932:             * @param array  the array to add to the <code>toString</code>
0933:             * @return this
0934:             */
0935:            public ToStringBuilder append(String fieldName, short[] array) {
0936:                style.append(buffer, fieldName, array, null);
0937:                return this ;
0938:            }
0939:
0940:            /**
0941:             * <p>Append to the <code>toString</code> a <code>short</code>
0942:             * array.</p>
0943:             *
0944:             * <p>A boolean parameter controls the level of detail to show.
0945:             * Setting <code>true</code> will output the array in full. Setting
0946:             * <code>false</code> will output a summary, typically the size of
0947:             * the array.
0948:             *
0949:             * @param fieldName  the field name
0950:             * @param array  the array to add to the <code>toString</code>
0951:             * @param fullDetail  <code>true</code> for detail, <code>false</code>
0952:             *  for summary info
0953:             * @return this
0954:             */
0955:            public ToStringBuilder append(String fieldName, short[] array,
0956:                    boolean fullDetail) {
0957:                style.append(buffer, fieldName, array, BooleanUtils
0958:                        .toBooleanObject(fullDetail));
0959:                return this ;
0960:            }
0961:
0962:            /**
0963:             * <p>Appends with the same format as the default <code>Object toString()
0964:             * </code> method. Appends the class name followed by 
0965:             * {@link System#identityHashCode(java.lang.Object)}.</p>
0966:             * 
0967:             * @param object  the <code>Object</code> whose class name and id to output
0968:             * @return this
0969:             * @since 2.0
0970:             */
0971:            public ToStringBuilder appendAsObjectToString(Object object) {
0972:                ObjectUtils.appendIdentityToString(this .getStringBuffer(),
0973:                        object);
0974:                return this ;
0975:            }
0976:
0977:            //----------------------------------------------------------------------------
0978:
0979:            /**
0980:             * <p>Append the <code>toString</code> from the superclass.</p>
0981:             * 
0982:             * <p>This method assumes that the superclass uses the same <code>ToStringStyle</code>
0983:             * as this one.</p>
0984:             * 
0985:             * <p>If <code>superToString</code> is <code>null</code>, no change is made.</p>
0986:             *
0987:             * @param superToString  the result of <code>super.toString()</code>
0988:             * @return this
0989:             * @since 2.0
0990:             */
0991:            public ToStringBuilder appendSuper(String super ToString) {
0992:                if (super ToString != null) {
0993:                    style.appendSuper(buffer, super ToString);
0994:                }
0995:                return this ;
0996:            }
0997:
0998:            /**
0999:             * <p>Append the <code>toString</code> from another object.</p>
1000:             * 
1001:             * <p>This method is useful where a class delegates most of the implementation of
1002:             * its properties to another class. You can then call <code>toString()</code> on
1003:             * the other class and pass the result into this method.</p>
1004:             * 
1005:             * <pre>
1006:             *   private AnotherObject delegate;
1007:             *   private String fieldInThisClass;
1008:             * 
1009:             *   public String toString() {
1010:             *     return new ToStringBuilder(this).
1011:             *       appendToString(delegate.toString()).
1012:             *       append(fieldInThisClass).
1013:             *       toString();
1014:             *   }</pre>
1015:             * 
1016:             * <p>This method assumes that the other object uses the same <code>ToStringStyle</code>
1017:             * as this one.</p>
1018:             * 
1019:             * <p>If the <code>toString</code> is <code>null</code>, no change is made.</p>
1020:             *
1021:             * @param toString  the result of <code>toString()</code> on another object
1022:             * @return this
1023:             * @since 2.0
1024:             */
1025:            public ToStringBuilder appendToString(String toString) {
1026:                if (toString != null) {
1027:                    style.appendToString(buffer, toString);
1028:                }
1029:                return this ;
1030:            }
1031:
1032:            /**
1033:             * <p>Returns the <code>Object</code> being output.</p>
1034:             * 
1035:             * @return The object being output.
1036:             * @since 2.0
1037:             */
1038:            public Object getObject() {
1039:                return object;
1040:            }
1041:
1042:            /**
1043:             * <p>Gets the <code>StringBuffer</code> being populated.</p>
1044:             * 
1045:             * @return the <code>StringBuffer</code> being populated
1046:             */
1047:            public StringBuffer getStringBuffer() {
1048:                return buffer;
1049:            }
1050:
1051:            //----------------------------------------------------------------------------
1052:
1053:            /**
1054:             * <p>Gets the <code>ToStringStyle</code> being used.</p>
1055:             * 
1056:             * @return the <code>ToStringStyle</code> being used
1057:             * @since 2.0
1058:             */
1059:            public ToStringStyle getStyle() {
1060:                return style;
1061:            }
1062:
1063:            /**
1064:             * <p>Returns the built <code>toString</code>.</p>
1065:             * 
1066:             * <p>This method appends the end of data indicator, and can only be called once.
1067:             * Use {@link #getStringBuffer} to get the current string state.</p>
1068:             * 
1069:             * <p>If the object is <code>null</code>, return the style's <code>nullText</code></p>
1070:             * 
1071:             * @return the String <code>toString</code>
1072:             */
1073:            public String toString() {
1074:                if (this.getObject() == null) {
1075:                    this.getStringBuffer()
1076:                            .append(this.getStyle().getNullText());
1077:                } else {
1078:                    style.appendEnd(this.getStringBuffer(), this.getObject());
1079:                }
1080:                return this.getStringBuffer().toString();
1081:            }
1082:
1083:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.