1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowledgement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgement may appear in the software itself,
  24. * if and wherever such third-party acknowledgements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.commons.lang.builder;
  55. import org.apache.commons.lang.BooleanUtils;
  56. import org.apache.commons.lang.ObjectUtils;
  57. /**
  58. * <p>Assists in implementing {@link Object#toString()} methods.</p>
  59. *
  60. * <p>This class enables a good and consistent <code>toString()</code> to be built for any
  61. * class or object. This class aims to simplify the process by:</p>
  62. * <ul>
  63. * <li>allowing field names</li>
  64. * <li>handling all types consistently</li>
  65. * <li>handling nulls consistently</li>
  66. * <li>outputting arrays and multi-dimensional arrays</li>
  67. * <li>enabling the detail level to be controlled for Objects and Collections</li>
  68. * <li>handling class hierarchies</li>
  69. * </ul>
  70. *
  71. * <p>To use this class write code as follows:</p>
  72. *
  73. * <pre>
  74. * public class Person {
  75. * String name;
  76. * int age;
  77. * boolean isSmoker;
  78. *
  79. * ...
  80. *
  81. * public String toString() {
  82. * return new ToStringBuilder(this).
  83. * append("name", name).
  84. * append("age", age).
  85. * append("smoker", smoker).
  86. * toString();
  87. * }
  88. * }
  89. * </pre>
  90. *
  91. * <p>This will produce a toString of the format:
  92. * <code>Person@7f54[name=Stephen,age=29,smoker=false]</code></p>
  93. *
  94. * <p>To add the superclass <code>toString</code>, use {@link #appendSuper}.
  95. * To append the <code>toString</code> from an object that is delegated
  96. * to (or any other object), use {@link #appendToString}.</p>
  97. *
  98. * <p>Alternatively, there is a method that uses reflection to determine
  99. * the fields to test. Because these fields are usually private, the method,
  100. * <code>reflectionToString</code>, uses <code>AccessibleObject.setAccessible</code> to
  101. * change the visibility of the fields. This will fail under a security manager,
  102. * unless the appropriate permissions are set up correctly. It is also
  103. * slower than testing explicitly.</p>
  104. *
  105. * <p>A typical invocation for this method would look like:</p>
  106. *
  107. * <pre>
  108. * public String toString() {
  109. * return ToStringBuilder.reflectionToString(this);
  110. * }
  111. * </pre>
  112. *
  113. * <p>You can also use the builder to debug 3rd party objects:</p>
  114. *
  115. * <pre>
  116. * System.out.println("An object: " + ToStringBuilder.reflectionToString(anObject));
  117. * </pre>
  118. *
  119. * <p>The exact format of the <code>toString</code> is determined by
  120. * the {@link ToStringStyle} passed into the constructor.</p>
  121. *
  122. * @author Stephen Colebourne
  123. * @author Gary Gregory
  124. * @author Pete Gieser
  125. * @since 1.0
  126. * @version $Id: ToStringBuilder.java,v 1.29 2003/08/23 00:21:49 ggregory Exp $
  127. */
  128. public class ToStringBuilder {
  129. /**
  130. * The default style of output to use.
  131. */
  132. private static ToStringStyle defaultStyle = ToStringStyle.DEFAULT_STYLE;
  133. //----------------------------------------------------------------------------
  134. /**
  135. * <p>Gets the default <code>ToStringStyle</code> to use.</p>
  136. *
  137. * <p>This could allow the <code>ToStringStyle</code> to be
  138. * controlled for an entire application with one call.</p>
  139. *
  140. * <p>This might be used to have a verbose
  141. * <code>ToStringStyle</code> during development and a compact
  142. * <code>ToStringStyle</code> in production.</p>
  143. *
  144. * @return the default <code>ToStringStyle</code>
  145. */
  146. public static ToStringStyle getDefaultStyle() {
  147. return defaultStyle;
  148. }
  149. /**
  150. * <p>Forwards to <code>ReflectionToStringBuilder</code>.</p>
  151. *
  152. * @see ReflectionToStringBuilder#toString(Object)
  153. */
  154. public static String reflectionToString(Object object) {
  155. return ReflectionToStringBuilder.toString(object);
  156. }
  157. /**
  158. * <p>Forwards to <code>ReflectionToStringBuilder</code>.</p>
  159. *
  160. * @see ReflectionToStringBuilder#toString(Object,ToStringStyle)
  161. */
  162. public static String reflectionToString(Object object, ToStringStyle style) {
  163. return ReflectionToStringBuilder.toString(object, style);
  164. }
  165. /**
  166. * <p>Forwards to <code>ReflectionToStringBuilder</code>.</p>
  167. *
  168. * @see ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean)
  169. */
  170. public static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients) {
  171. return ReflectionToStringBuilder.toString(object, style, outputTransients, null);
  172. }
  173. /**
  174. * <p>Forwards to <code>ReflectionToStringBuilder</code>.</p>
  175. *
  176. * @see ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean,Class)
  177. * @since 2.0
  178. */
  179. public static String reflectionToString(
  180. Object object,
  181. ToStringStyle style,
  182. boolean outputTransients,
  183. Class reflectUpToClass) {
  184. return ReflectionToStringBuilder.toString(object, style, outputTransients, reflectUpToClass);
  185. }
  186. /**
  187. * <p>Sets the default <code>ToStringStyle</code> to use.</p>
  188. *
  189. * @param style the default <code>ToStringStyle</code>
  190. * @throws IllegalArgumentException if the style is <code>null</code>
  191. */
  192. public static void setDefaultStyle(ToStringStyle style) {
  193. if (style == null) {
  194. throw new IllegalArgumentException("The style must not be null");
  195. }
  196. defaultStyle = style;
  197. }
  198. /**
  199. * Current toString buffer.
  200. */
  201. private final StringBuffer buffer;
  202. /**
  203. * The object being output.
  204. */
  205. private final Object object;
  206. /**
  207. * The style of output to use.
  208. */
  209. private final ToStringStyle style;
  210. /**
  211. * <p>Constructor for <code>ToStringBuilder</code>.</p>
  212. *
  213. * <p>This constructor outputs using the default style set with
  214. * <code>setDefaultStyle</code>.</p>
  215. *
  216. * @param object the Object to build a <code>toString</code> for,
  217. * must not be <code>null</code>
  218. * @throws IllegalArgumentException if the Object passed in is
  219. * <code>null</code>
  220. */
  221. public ToStringBuilder(Object object) {
  222. this(object, getDefaultStyle(), null);
  223. }
  224. /**
  225. * <p>Constructor for <code>ToStringBuilder</code> specifying the
  226. * output style.</p>
  227. *
  228. * <p>If the style is <code>null</code>, the default style is used.</p>
  229. *
  230. * @param object the Object to build a <code>toString</code> for,
  231. * must not be <code>null</code>
  232. * @param style the style of the <code>toString</code> to create,
  233. * may be <code>null</code>
  234. * @throws IllegalArgumentException if the Object passed in is
  235. * <code>null</code>
  236. */
  237. public ToStringBuilder(Object object, ToStringStyle style) {
  238. this(object, style, null);
  239. }
  240. /**
  241. * <p>Constructor for <code>ToStringBuilder</code>.</p>
  242. *
  243. * <p>If the style is <code>null</code>, the default style is used.</p>
  244. *
  245. * <p>If the buffer is <code>null</code>, a new one is created.</p>
  246. *
  247. * @param object the Object to build a <code>toString</code> for,
  248. * must not be <code>null</code>
  249. * @param style the style of the <code>toString</code> to create,
  250. * may be <code>null</code>
  251. * @param buffer the <code>StringBuffer</code> to populate, may be
  252. * <code>null</code>
  253. * @throws IllegalArgumentException if the Object passed in is
  254. * <code>null</code>
  255. */
  256. public ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer) {
  257. super();
  258. if (object == null) {
  259. throw new IllegalArgumentException("The object to create a toString for must not be null");
  260. }
  261. if (style == null) {
  262. style = getDefaultStyle();
  263. }
  264. if (buffer == null) {
  265. buffer = new StringBuffer(512);
  266. }
  267. this.buffer = buffer;
  268. this.style = style;
  269. this.object = object;
  270. style.appendStart(buffer, object);
  271. }
  272. //----------------------------------------------------------------------------
  273. /**
  274. * <p>Append to the <code>toString</code> a <code>boolean</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(boolean value) {
  281. style.append(buffer, null, value);
  282. return this;
  283. }
  284. //----------------------------------------------------------------------------
  285. /**
  286. * <p>Append to the <code>toString</code> a <code>boolean</code>
  287. * array.</p>
  288. *
  289. * @param array the array to add to the <code>toString</code>
  290. * @return this
  291. */
  292. public ToStringBuilder append(boolean[] array) {
  293. style.append(buffer, null, array, null);
  294. return this;
  295. }
  296. //----------------------------------------------------------------------------
  297. /**
  298. * <p>Append to the <code>toString</code> a <code>byte</code>
  299. * value.</p>
  300. *
  301. * @param value the value to add to the <code>toString</code>
  302. * @return this
  303. */
  304. public ToStringBuilder append(byte value) {
  305. style.append(buffer, null, value);
  306. return this;
  307. }
  308. //----------------------------------------------------------------------------
  309. /**
  310. * <p>Append to the <code>toString</code> a <code>byte</code>
  311. * array.</p>
  312. *
  313. * @param array the array to add to the <code>toString</code>
  314. * @return this
  315. */
  316. public ToStringBuilder append(byte[] array) {
  317. style.append(buffer, null, array, null);
  318. return this;
  319. }
  320. //----------------------------------------------------------------------------
  321. /**
  322. * <p>Append to the <code>toString</code> a <code>char</code>
  323. * value.</p>
  324. *
  325. * @param value the value to add to the <code>toString</code>
  326. * @return this
  327. */
  328. public ToStringBuilder append(char value) {
  329. style.append(buffer, null, value);
  330. return this;
  331. }
  332. //----------------------------------------------------------------------------
  333. /**
  334. * <p>Append to the <code>toString</code> a <code>char</code>
  335. * array.</p>
  336. *
  337. * @param array the array to add to the <code>toString</code>
  338. * @return this
  339. */
  340. public ToStringBuilder append(char[] array) {
  341. style.append(buffer, null, array, null);
  342. return this;
  343. }
  344. //----------------------------------------------------------------------------
  345. /**
  346. * <p>Append to the <code>toString</code> a <code>double</code>
  347. * value.</p>
  348. *
  349. * @param value the value to add to the <code>toString</code>
  350. * @return this
  351. */
  352. public ToStringBuilder append(double value) {
  353. style.append(buffer, null, value);
  354. return this;
  355. }
  356. //----------------------------------------------------------------------------
  357. /**
  358. * <p>Append to the <code>toString</code> a <code>double</code>
  359. * array.</p>
  360. *
  361. * @param array the array to add to the <code>toString</code>
  362. * @return this
  363. */
  364. public ToStringBuilder append(double[] array) {
  365. style.append(buffer, null, array, null);
  366. return this;
  367. }
  368. //----------------------------------------------------------------------------
  369. /**
  370. * <p>Append to the <code>toString</code> a <code>float</code>
  371. * value.</p>
  372. *
  373. * @param value the value to add to the <code>toString</code>
  374. * @return this
  375. */
  376. public ToStringBuilder append(float value) {
  377. style.append(buffer, null, value);
  378. return this;
  379. }
  380. //----------------------------------------------------------------------------
  381. /**
  382. * <p>Append to the <code>toString</code> a <code>float</code>
  383. * array.</p>
  384. *
  385. * @param array the array to add to the <code>toString</code>
  386. * @return this
  387. */
  388. public ToStringBuilder append(float[] array) {
  389. style.append(buffer, null, array, null);
  390. return this;
  391. }
  392. //----------------------------------------------------------------------------
  393. /**
  394. * <p>Append to the <code>toString</code> an <code>int</code>
  395. * value.</p>
  396. *
  397. * @param value the value to add to the <code>toString</code>
  398. * @return this
  399. */
  400. public ToStringBuilder append(int value) {
  401. style.append(buffer, null, value);
  402. return this;
  403. }
  404. //----------------------------------------------------------------------------
  405. /**
  406. * <p>Append to the <code>toString</code> an <code>int</code>
  407. * array.</p>
  408. *
  409. * @param array the array to add to the <code>toString</code>
  410. * @return this
  411. */
  412. public ToStringBuilder append(int[] array) {
  413. style.append(buffer, null, array, null);
  414. return this;
  415. }
  416. //----------------------------------------------------------------------------
  417. /**
  418. * <p>Append to the <code>toString</code> a <code>long</code>
  419. * value.</p>
  420. *
  421. * @param value the value to add to the <code>toString</code>
  422. * @return this
  423. */
  424. public ToStringBuilder append(long value) {
  425. style.append(buffer, null, value);
  426. return this;
  427. }
  428. //----------------------------------------------------------------------------
  429. /**
  430. * <p>Append to the <code>toString</code> a <code>long</code>
  431. * array.</p>
  432. *
  433. * @param array the array to add to the <code>toString</code>
  434. * @return this
  435. */
  436. public ToStringBuilder append(long[] array) {
  437. style.append(buffer, null, array, null);
  438. return this;
  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 object) {
  449. style.append(buffer, null, object, null);
  450. return this;
  451. }
  452. //----------------------------------------------------------------------------
  453. /**
  454. * <p>Append to the <code>toString</code> an <code>Object</code>
  455. * array.</p>
  456. *
  457. * @param array the array to add to the <code>toString</code>
  458. * @return this
  459. */
  460. public ToStringBuilder append(Object[] array) {
  461. style.append(buffer, null, array, null);
  462. return this;
  463. }
  464. //----------------------------------------------------------------------------
  465. /**
  466. * <p>Append to the <code>toString</code> a <code>short</code>
  467. * value.</p>
  468. *
  469. * @param value the value to add to the <code>toString</code>
  470. * @return this
  471. */
  472. public ToStringBuilder append(short value) {
  473. style.append(buffer, null, value);
  474. return this;
  475. }
  476. //----------------------------------------------------------------------------
  477. /**
  478. * <p>Append to the <code>toString</code> a <code>short</code>
  479. * array.</p>
  480. *
  481. * @param array the array to add to the <code>toString</code>
  482. * @return this
  483. */
  484. public ToStringBuilder append(short[] array) {
  485. style.append(buffer, null, array, null);
  486. return this;
  487. }
  488. /**
  489. * <p>Append to the <code>toString</code> a <code>boolean</code>
  490. * value.</p>
  491. *
  492. * @param fieldName the field name
  493. * @param value the value to add to the <code>toString</code>
  494. * @return this
  495. */
  496. public ToStringBuilder append(String fieldName, boolean value) {
  497. style.append(buffer, fieldName, value);
  498. return this;
  499. }
  500. /**
  501. * <p>Append to the <code>toString</code> a <code>boolean</code>
  502. * array.</p>
  503. *
  504. * @param fieldName the field name
  505. * @param array the array to add to the <code>hashCode</code>
  506. * @return this
  507. */
  508. public ToStringBuilder append(String fieldName, boolean[] array) {
  509. style.append(buffer, fieldName, array, null);
  510. return this;
  511. }
  512. /**
  513. * <p>Append to the <code>toString</code> a <code>boolean</code>
  514. * array.</p>
  515. *
  516. * <p>A boolean parameter controls the level of detail to show.
  517. * Setting <code>true</code> will output the array in full. Setting
  518. * <code>false</code> will output a summary, typically the size of
  519. * the array.</p>
  520. *
  521. * @param fieldName the field name
  522. * @param array the array to add to the <code>toString</code>
  523. * @param fullDetail <code>true</code> for detail, <code>false</code>
  524. * for summary info
  525. * @return this
  526. */
  527. public ToStringBuilder append(String fieldName, boolean[] array, boolean fullDetail) {
  528. style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
  529. return this;
  530. }
  531. /**
  532. * <p>Append to the <code>toString</code> an <code>byte</code>
  533. * value.</p>
  534. *
  535. * @param fieldName the field name
  536. * @param value the value to add to the <code>toString</code>
  537. * @return this
  538. */
  539. public ToStringBuilder append(String fieldName, byte value) {
  540. style.append(buffer, fieldName, value);
  541. return this;
  542. }
  543. /**
  544. * <p>Append to the <code>toString</code> a <code>byte</code> array.</p>
  545. *
  546. * @param fieldName the field name
  547. * @param array the array to add to the <code>toString</code>
  548. * @return this
  549. */
  550. public ToStringBuilder append(String fieldName, byte[] array) {
  551. style.append(buffer, fieldName, array, null);
  552. return this;
  553. }
  554. /**
  555. * <p>Append to the <code>toString</code> a <code>byte</code>
  556. * array.</p>
  557. *
  558. * <p>A boolean parameter controls the level of detail to show.
  559. * Setting <code>true</code> will output the array in full. Setting
  560. * <code>false</code> will output a summary, typically the size of
  561. * the array.
  562. *
  563. * @param fieldName the field name
  564. * @param array the array to add to the <code>toString</code>
  565. * @param fullDetail <code>true</code> for detail, <code>false</code>
  566. * for summary info
  567. * @return this
  568. */
  569. public ToStringBuilder append(String fieldName, byte[] array, boolean fullDetail) {
  570. style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
  571. return this;
  572. }
  573. /**
  574. * <p>Append to the <code>toString</code> a <code>char</code>
  575. * value.</p>
  576. *
  577. * @param fieldName the field name
  578. * @param value the value to add to the <code>toString</code>
  579. * @return this
  580. */
  581. public ToStringBuilder append(String fieldName, char value) {
  582. style.append(buffer, fieldName, value);
  583. return this;
  584. }
  585. /**
  586. * <p>Append to the <code>toString</code> a <code>char</code>
  587. * array.</p>
  588. *
  589. * @param fieldName the field name
  590. * @param array the array to add to the <code>toString</code>
  591. * @return this
  592. */
  593. public ToStringBuilder append(String fieldName, char[] array) {
  594. style.append(buffer, fieldName, array, null);
  595. return this;
  596. }
  597. /**
  598. * <p>Append to the <code>toString</code> a <code>char</code>
  599. * array.</p>
  600. *
  601. * <p>A boolean parameter controls the level of detail to show.
  602. * Setting <code>true</code> will output the array in full. Setting
  603. * <code>false</code> will output a summary, typically the size of
  604. * the array.</p>
  605. *
  606. * @param fieldName the field name
  607. * @param array the array to add to the <code>toString</code>
  608. * @param fullDetail <code>true</code> for detail, <code>false</code>
  609. * for summary info
  610. * @return this
  611. */
  612. public ToStringBuilder append(String fieldName, char[] array, boolean fullDetail) {
  613. style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
  614. return this;
  615. }
  616. /**
  617. * <p>Append to the <code>toString</code> a <code>double</code>
  618. * value.</p>
  619. *
  620. * @param fieldName the field name
  621. * @param value the value to add to the <code>toString</code>
  622. * @return this
  623. */
  624. public ToStringBuilder append(String fieldName, double value) {
  625. style.append(buffer, fieldName, value);
  626. return this;
  627. }
  628. /**
  629. * <p>Append to the <code>toString</code> a <code>double</code>
  630. * array.</p>
  631. *
  632. * @param fieldName the field name
  633. * @param array the array to add to the <code>toString</code>
  634. * @return this
  635. */
  636. public ToStringBuilder append(String fieldName, double[] array) {
  637. style.append(buffer, fieldName, array, null);
  638. return this;
  639. }
  640. /**
  641. * <p>Append to the <code>toString</code> a <code>double</code>
  642. * array.</p>
  643. *
  644. * <p>A boolean parameter controls the level of detail to show.
  645. * Setting <code>true</code> will output the array in full. Setting
  646. * <code>false</code> will output a summary, typically the size of
  647. * the array.</p>
  648. *
  649. * @param fieldName the field name
  650. * @param array the array to add to the <code>toString</code>
  651. * @param fullDetail <code>true</code> for detail, <code>false</code>
  652. * for summary info
  653. * @return this
  654. */
  655. public ToStringBuilder append(String fieldName, double[] array, boolean fullDetail) {
  656. style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
  657. return this;
  658. }
  659. /**
  660. * <p>Append to the <code>toString</code> an <code>float</code>
  661. * value.</p>
  662. *
  663. * @param fieldName the field name
  664. * @param value the value to add to the <code>toString</code>
  665. * @return this
  666. */
  667. public ToStringBuilder append(String fieldName, float value) {
  668. style.append(buffer, fieldName, value);
  669. return this;
  670. }
  671. /**
  672. * <p>Append to the <code>toString</code> a <code>float</code>
  673. * array.</p>
  674. *
  675. * @param fieldName the field name
  676. * @param array the array to add to the <code>toString</code>
  677. * @return this
  678. */
  679. public ToStringBuilder append(String fieldName, float[] array) {
  680. style.append(buffer, fieldName, array, null);
  681. return this;
  682. }
  683. /**
  684. * <p>Append to the <code>toString</code> a <code>float</code>
  685. * array.</p>
  686. *
  687. * <p>A boolean parameter controls the level of detail to show.
  688. * Setting <code>true</code> will output the array in full. Setting
  689. * <code>false</code> will output a summary, typically the size of
  690. * the array.</p>
  691. *
  692. * @param fieldName the field name
  693. * @param array the array to add to the <code>toString</code>
  694. * @param fullDetail <code>true</code> for detail, <code>false</code>
  695. * for summary info
  696. * @return this
  697. */
  698. public ToStringBuilder append(String fieldName, float[] array, boolean fullDetail) {
  699. style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
  700. return this;
  701. }
  702. /**
  703. * <p>Append to the <code>toString</code> an <code>int</code>
  704. * value.</p>
  705. *
  706. * @param fieldName the field name
  707. * @param value the value to add to the <code>toString</code>
  708. * @return this
  709. */
  710. public ToStringBuilder append(String fieldName, int value) {
  711. style.append(buffer, fieldName, value);
  712. return this;
  713. }
  714. /**
  715. * <p>Append to the <code>toString</code> an <code>int</code>
  716. * array.</p>
  717. *
  718. * @param fieldName the field name
  719. * @param array the array to add to the <code>toString</code>
  720. * @return this
  721. */
  722. public ToStringBuilder append(String fieldName, int[] array) {
  723. style.append(buffer, fieldName, array, null);
  724. return this;
  725. }
  726. /**
  727. * <p>Append to the <code>toString</code> an <code>int</code>
  728. * array.</p>
  729. *
  730. * <p>A boolean parameter controls the level of detail to show.
  731. * Setting <code>true</code> will output the array in full. Setting
  732. * <code>false</code> will output a summary, typically the size of
  733. * the array.</p>
  734. *
  735. * @param fieldName the field name
  736. * @param array the array to add to the <code>toString</code>
  737. * @param fullDetail <code>true</code> for detail, <code>false</code>
  738. * for summary info
  739. * @return this
  740. */
  741. public ToStringBuilder append(String fieldName, int[] array, boolean fullDetail) {
  742. style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
  743. return this;
  744. }
  745. /**
  746. * <p>Append to the <code>toString</code> a <code>long</code>
  747. * value.</p>
  748. *
  749. * @param fieldName the field name
  750. * @param value the value to add to the <code>toString</code>
  751. * @return this
  752. */
  753. public ToStringBuilder append(String fieldName, long value) {
  754. style.append(buffer, fieldName, value);
  755. return this;
  756. }
  757. /**
  758. * <p>Append to the <code>toString</code> a <code>long</code>
  759. * array.</p>
  760. *
  761. * @param fieldName the field name
  762. * @param array the array to add to the <code>toString</code>
  763. * @return this
  764. */
  765. public ToStringBuilder append(String fieldName, long[] array) {
  766. style.append(buffer, fieldName, array, null);
  767. return this;
  768. }
  769. /**
  770. * <p>Append to the <code>toString</code> a <code>long</code>
  771. * array.</p>
  772. *
  773. * <p>A boolean parameter controls the level of detail to show.
  774. * Setting <code>true</code> will output the array in full. Setting
  775. * <code>false</code> will output a summary, typically the size of
  776. * the array.</p>
  777. *
  778. * @param fieldName the field name
  779. * @param array the array to add to the <code>toString</code>
  780. * @param fullDetail <code>true</code> for detail, <code>false</code>
  781. * for summary info
  782. * @return this
  783. */
  784. public ToStringBuilder append(String fieldName, long[] array, boolean fullDetail) {
  785. style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
  786. return this;
  787. }
  788. /**
  789. * <p>Append to the <code>toString</code> an <code>Object</code>
  790. * value.</p>
  791. *
  792. * @param fieldName the field name
  793. * @param object the value to add to the <code>toString</code>
  794. * @return this
  795. */
  796. public ToStringBuilder append(String fieldName, Object object) {
  797. style.append(buffer, fieldName, object, null);
  798. return this;
  799. }
  800. /**
  801. * <p>Append to the <code>toString</code> an <code>Object</code>
  802. * value.</p>
  803. *
  804. * @param fieldName the field name
  805. * @param object the value to add to the <code>toString</code>
  806. * @param fullDetail <code>true</code> for detail,
  807. * <code>false</code> for summary info
  808. * @return this
  809. */
  810. public ToStringBuilder append(String fieldName, Object object, boolean fullDetail) {
  811. style.append(buffer, fieldName, object, BooleanUtils.toBooleanObject(fullDetail));
  812. return this;
  813. }
  814. /**
  815. * <p>Append to the <code>toString</code> an <code>Object</code>
  816. * array.</p>
  817. *
  818. * @param fieldName the field name
  819. * @param array the array to add to the <code>toString</code>
  820. * @return this
  821. */
  822. public ToStringBuilder append(String fieldName, Object[] array) {
  823. style.append(buffer, fieldName, array, null);
  824. return this;
  825. }
  826. /**
  827. * <p>Append to the <code>toString</code> an <code>Object</code>
  828. * array.</p>
  829. *
  830. * <p>A boolean parameter controls the level of detail to show.
  831. * Setting <code>true</code> will output the array in full. Setting
  832. * <code>false</code> will output a summary, typically the size of
  833. * the array.</p>
  834. *
  835. * @param fieldName the field name
  836. * @param array the array to add to the <code>toString</code>
  837. * @param fullDetail <code>true</code> for detail, <code>false</code>
  838. * for summary info
  839. * @return this
  840. */
  841. public ToStringBuilder append(String fieldName, Object[] array, boolean fullDetail) {
  842. style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
  843. return this;
  844. }
  845. /**
  846. * <p>Append to the <code>toString</code> an <code>short</code>
  847. * value.</p>
  848. *
  849. * @param fieldName the field name
  850. * @param value the value to add to the <code>toString</code>
  851. * @return this
  852. */
  853. public ToStringBuilder append(String fieldName, short value) {
  854. style.append(buffer, fieldName, value);
  855. return this;
  856. }
  857. /**
  858. * <p>Append to the <code>toString</code> a <code>short</code>
  859. * array.</p>
  860. *
  861. * @param fieldName the field name
  862. * @param array the array to add to the <code>toString</code>
  863. * @return this
  864. */
  865. public ToStringBuilder append(String fieldName, short[] array) {
  866. style.append(buffer, fieldName, array, null);
  867. return this;
  868. }
  869. /**
  870. * <p>Append to the <code>toString</code> a <code>short</code>
  871. * array.</p>
  872. *
  873. * <p>A boolean parameter controls the level of detail to show.
  874. * Setting <code>true</code> will output the array in full. Setting
  875. * <code>false</code> will output a summary, typically the size of
  876. * the array.
  877. *
  878. * @param fieldName the field name
  879. * @param array the array to add to the <code>toString</code>
  880. * @param fullDetail <code>true</code> for detail, <code>false</code>
  881. * for summary info
  882. * @return this
  883. */
  884. public ToStringBuilder append(String fieldName, short[] array, boolean fullDetail) {
  885. style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
  886. return this;
  887. }
  888. /**
  889. * <p>Appends with the same format as the default <code>Object toString()
  890. * </code> method. Appends the class name followed by
  891. * {@link System#identityHashCode(java.lang.Object)}.</p>
  892. *
  893. * @param object the <code>Object</code> whose class name and id to output
  894. * @since 2.0
  895. */
  896. public ToStringBuilder appendAsObjectToString(Object object) {
  897. ObjectUtils.appendIdentityToString(this.getStringBuffer(), object);
  898. return this;
  899. }
  900. //----------------------------------------------------------------------------
  901. /**
  902. * <p>Append the <code>toString</code> from the superclass.</p>
  903. *
  904. * <p>This method assumes that the superclass uses the same <code>ToStringStyle</code>
  905. * as this one.</p>
  906. *
  907. * <p>If <code>superToString</code> is <code>null</code>, no change is made.</p>
  908. *
  909. * @param superToString the result of <code>super.toString()</code>
  910. * @return this
  911. * @since 2.0
  912. */
  913. public ToStringBuilder appendSuper(String superToString) {
  914. if (superToString != null) {
  915. style.appendSuper(buffer, superToString);
  916. }
  917. return this;
  918. }
  919. /**
  920. * <p>Append the <code>toString</code> from another object.</p>
  921. *
  922. * <p>This method is useful where a class delegates most of the implementation of
  923. * its properties to another class. You can then call <code>toString()</code> on
  924. * the other class and pass the result into this method.</p>
  925. *
  926. * <pre>
  927. * private AnotherObject delegate;
  928. * private String fieldInThisClass;
  929. *
  930. * public String toString() {
  931. * return new ToStringBuilder(this).
  932. * appendToString(delegate.toString()).
  933. * append(fieldInThisClass).
  934. * toString();
  935. * }</pre>
  936. *
  937. * <p>This method assumes that the other object uses the same <code>ToStringStyle</code>
  938. * as this one.</p>
  939. *
  940. * <p>If the <code>toString</code> is <code>null</code>, no change is made.</p>
  941. *
  942. * @param toString the result of <code>toString()</code> on another object
  943. * @return this
  944. * @since 2.0
  945. */
  946. public ToStringBuilder appendToString(String toString) {
  947. if (toString != null) {
  948. style.appendToString(buffer, toString);
  949. }
  950. return this;
  951. }
  952. /**
  953. * <p>Gets the <code>StringBuffer</code> being populated.</p>
  954. *
  955. * @return the <code>StringBuffer</code> being populated
  956. */
  957. public StringBuffer getStringBuffer() {
  958. return buffer;
  959. }
  960. //----------------------------------------------------------------------------
  961. /**
  962. * <p>Gets the <code>ToStringStyle</code> being used.</p>
  963. *
  964. * @return the <code>ToStringStyle</code> being used
  965. * @since 2.0
  966. */
  967. public ToStringStyle getStyle() {
  968. return style;
  969. }
  970. /**
  971. * <p>Returns the built <code>toString</code>.</p>
  972. *
  973. * <p>This method appends the end of data indicator, and can only be called once.
  974. * Use {@link #getStringBuffer} to get the current string state.</p>
  975. *
  976. * @return the String <code>toString</code>
  977. */
  978. public String toString() {
  979. style.appendEnd(buffer, object);
  980. return buffer.toString();
  981. }
  982. /**
  983. * <p>Returns the <code>Object</code> being output.</p>
  984. *
  985. * @return The object being output.
  986. * @since 2.0
  987. */
  988. public Object getObject() {
  989. return object;
  990. }
  991. }