Controls String
formatting for {@link ToStringBuilder}. The main public interface is always via ToStringBuilder
.
These classes are intended to be used as Singletons
. There is no need to instantiate a new style each time. A program will generally use one of the predefined constants on this class. Alternatively, the {@link StandardToStringStyle} class can be usedto set the individual settings. Thus most styles can be achieved without subclassing.
If required, a subclass can override as many or as few of the methods as it requires. Each object type (from boolean
to long
to Object
to int[]
) has its own methods to output it. Most have two versions, detail and summary.
For example, the detail version of the array based methods will output the whole array, whereas the summary method will just output the array length.
If you want to format the output of certain objects, such as dates, you must create a subclass and override a method.
public class MyStyle extends ToStringStyle { protected void appendDetail(StringBuffer buffer, String fieldName, Object value) { if (value instanceof Date) { value = new SimpleDateFormat("yyyy-MM-dd").format(value); } buffer.append(value); } }
@author Apache Software Foundation
@author Gary Gregory
@author Pete Gieser
@author Masato Tezuka
@since 1.0
@version $Id: ToStringStyle.java 907126 2010-02-05 23:11:43Z mbenson $