A convenient framework for toString()


It is a not only praxis, but best practice, to override Object.toString() in new classes. This article presents a convenient framework for preserving the look and feel of toString() outputs.


public static <T> Map<String,Object> formatArg(String name,T value)
{
    if(name == null || value == null)
    {
      throw new IllegalArgumentException("A parameter was null.");
    }
    Map<String,Object> m = newHashMap();
    m.put(Util.KEY_FORMAT_OBJECT_NAME,name);
    m.put(Util.KEY_FORMAT_OBJECT_VALUE,value);
    return m;
}
 
public static <T> String formatObjectToString(Class<T> clss,List<Map<String,Object>> maps)
{
 
    StringBuilder buf = new StringBuilder();
    if(clss != null)
    {
      buf.append(clss.getName() + "\n");
      for(Map<String,Object> map:maps)
      {
        String name = (String)map.get(KEY_FORMAT_OBJECT_NAME);
        Object value = map.get(KEY_FORMAT_OBJECT_VALUE);
        String id = name + "(" + value.getClass().getName() + ")";
        buf.append(String.format("%1$-60s %2$s\n",id,value));
      }
    }
    return buf.toString();
}

formatArg() creates a Map from its parameters, a map that contains a single entry, keyed by the name of the value to display. newHashMap() is a type-safe Map creation method:


public static <K,V> newHashMap()
{
  return new HashMap<K,V>();
}

formatObjectToString() returns a String given a List of Maps (which Maps can be created by formatArg()). This version has hard-coded formatting, but it would be simplicity itself to add a Pattern by which the formatting could be dynamically controlled.

By way of example, class BasicNumericDataCollectorAction from the Directory Server Log Analyzer uses formatArg() and formatObjectToString() in its overridden toString() method by presenting in String form the statistical items that are calculated by the class:


@Override
public String toString()
{
  List<java.util.Map<String,Object&gt> list = ClassUtils.getArrayList();
  list.add(Util.formatArg("size",getSize()));
  list.add(Util.formatArg("min",getMax()));
  return Util.formatObjectToString(getClass(),list);
}

Links

  1. convenience framework
  2. usage example

About Terry Gardner

Terry Gardner was a leading directory services architect with experience with many large scale directory services installations and messaging server installations, and was a Subject Matter Expert in the field of Directory Services and Solaris (operating system) performance. Mr. Gardner also participated in the open-source software community. Mr. Gardner passed away in December, 2013.
This entry was posted in Java and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s