fc.util
Class ToString

java.lang.Object
  extended by fc.util.ToString

public class ToString
extends Object

Makes creating toString methods easier. (For example, provides ability to introspect and write field values). Idea inspired by a similar apache/jakarta utility.

Methods of the form append(String, type) imply the name specified by the string (typically a field name) is shown with value specified by type.

Example usage:
foo and bar are fields of this object.

public String toString() {
        return new ToString(this).
                append("foo","some-value").
                append("bar",123).
                render();
        }
Another example:
Automatically prints this entire object using reflection.
public String toString() {
        return new ToString(this).reflect().
        render();
        }
Note: Don't forget the render() call at the end.

The class only needs to be instantiated once so here's a more efficient approach:

{ //instance initializer
ToString tostr = new ToString(this);
}
public String toString() {
        return tostr.reflect().
        render();
        }


Nested Class Summary
static class ToString.Style
          Drives the formatting behavior.
 
Constructor Summary
ToString(Object obj)
          Creates a ToString for the specified object, using the default ToString.Style.
ToString(Object obj, ToString.Style.VisibleLevel level)
          Creates a ToString for the specified object with the specified visibility level.
ToString(Object obj, ToString.Style style)
          Creates a ToString for the specified object using the specified style
 
Method Summary
 ToString append(Object str)
          Appends an arbitrary string to the result.
 ToString append(String fieldName, boolean val)
           
 ToString append(String fieldName, boolean[] val)
           
 ToString append(String fieldName, byte val)
           
 ToString append(String fieldName, byte[] val)
           
 ToString append(String fieldName, char val)
           
 ToString append(String fieldName, char[] val)
           
 ToString append(String fieldName, double val)
           
 ToString append(String fieldName, double[] val)
           
 ToString append(String fieldName, float val)
           
 ToString append(String fieldName, float[] val)
           
 ToString append(String fieldName, int val)
           
 ToString append(String fieldName, int[] val)
           
 ToString append(String fieldName, long val)
           
 ToString append(String fieldName, long[] val)
           
 ToString append(String fieldName, Object val)
           
 ToString append(String fieldName, Object[] val)
           
 ToString append(String fieldName, short val)
           
 ToString append(String fieldName, short[] val)
           
 ToString append(String fieldName, String val)
           
 StringBuffer getBuffer()
          Returns the internal buffer used to create the string
static ToString.Style getDefaultStyle()
          Returns the default style object.
 ToString.Style getStyle()
          Returns the style being currently used by this instance.
static void main(String[] args)
          Unit test
 ToString reflect()
          Uses reflection to get the contents of the object.
 String render()
          Renders the string
static void setDefaultStyle(ToString.Style style)
          Sets the style object to use as the default.
 String toString()
          Returns information about the current state of the ToString object itself.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ToString

public ToString(Object obj)
Creates a ToString for the specified object, using the default ToString.Style.


ToString

public ToString(Object obj,
                ToString.Style style)
Creates a ToString for the specified object using the specified style

Parameters:
obj - the target object
style - the formatting style

ToString

public ToString(Object obj,
                ToString.Style.VisibleLevel level)
Creates a ToString for the specified object with the specified visibility level.

Parameters:
obj - the target object
style - the formatting style
Method Detail

getDefaultStyle

public static ToString.Style getDefaultStyle()
Returns the default style object. Changes to this will affect the default formatting


setDefaultStyle

public static void setDefaultStyle(ToString.Style style)
Sets the style object to use as the default. This style will be used by default by all new instances of ToString.

Parameters:
style - the default style

getStyle

public ToString.Style getStyle()
Returns the style being currently used by this instance. Modifications to this style this will affect rendering output appropriately.


getBuffer

public StringBuffer getBuffer()
Returns the internal buffer used to create the string


reflect

public ToString reflect()
Uses reflection to get the contents of the object. Reflection does not expand and print all the array values even if the current style's expandArray is set to true. To print all array values, use the append methods.


render

public String render()
Renders the string


toString

public String toString()
Returns information about the current state of the ToString object itself. To get the target object's string, use the render method.

Overrides:
toString in class Object

main

public static void main(String[] args)
Unit test


append

public ToString append(Object str)
Appends an arbitrary string to the result. This can be used as a prologue, epilogue etc.


append

public ToString append(String fieldName,
                       Object val)

append

public ToString append(String fieldName,
                       String val)

append

public ToString append(String fieldName,
                       long val)

append

public ToString append(String fieldName,
                       int val)

append

public ToString append(String fieldName,
                       short val)

append

public ToString append(String fieldName,
                       byte val)

append

public ToString append(String fieldName,
                       double val)

append

public ToString append(String fieldName,
                       float val)

append

public ToString append(String fieldName,
                       char val)

append

public ToString append(String fieldName,
                       boolean val)

append

public ToString append(String fieldName,
                       Object[] val)

append

public ToString append(String fieldName,
                       long[] val)

append

public ToString append(String fieldName,
                       int[] val)

append

public ToString append(String fieldName,
                       short[] val)

append

public ToString append(String fieldName,
                       byte[] val)

append

public ToString append(String fieldName,
                       char[] val)

append

public ToString append(String fieldName,
                       double[] val)

append

public ToString append(String fieldName,
                       float[] val)

append

public ToString append(String fieldName,
                       boolean[] val)