HI,

While needing to add more diagnostic messages to debug skinning issues, we needed quite a few toString() implementations that needed these common aspects:

1. Add information about the class - class name and identity hash code
2. Add the rudimentary field-value state from the objects of the class
3. Formatting needed to make the string representations uniform.

Proposal is to add a utility class (API) "ToStringHelper" that takes care of this requirement. Please review the API and discuss here if you have comments.

API proposal:
==========

package org.apache.myfaces.trinidad.util;

/**
 * Helper class to build strings for diagnostic log messages, and in toString() 
implementations.
 * This helper class takes care of few niceties' like:
 *  1. Adding the identity (classname and hashcode) of an object
 *  2. Adding fields and values of an object
 *  3. Formatting in string in JSON style
 *  4. Adopts a builder pattern, hence easy to use
 *
 */
public final class ToStringHelper
{
  /**
   * Zero arg constructor
   */
  public ToStringHelper()

  /**
   * Constructor that takes in the object whose information is to be added to 
the string
   * @param object The object whose classname and hashcode is to be included in 
the string
   */
  public ToStringHelper(Object object);

  /**
   * Appends the field name and value (as returned by 'toString()' of the 
supplied value object),
   *  formatting it in JSON style
   * @param fieldName The name of the field to append
   * @param value The value object of the field to append
   * @return The helper object after appending
   */
  public ToStringHelper append(String fieldName, Object value);
}

Thanks,
Prakash

Reply via email to