This
interface should be implemented by
classes that want to interact with the Formatter
class more intimately than is possible with the
toString method. When a
Formattable object is the argument for a
%s or %S conversion, its
formatTo( ) method is invoked rather than its
toString( ) method. formatTo( )
is responsible for formatting a textual representation of the object
to the specified formatter, subject to the
constraints imposed by the flags,
width, and
precision arguments.
The flags argument is a bitmask of zero or
more FormattableFlags constants. Each flag
provides information about the format specification that resulted in
the invocation of formatTo( ).
FormattableFlags.ALTERNATE indicates that the
# flag was used and that the
Formattable should format itself using some
alternate form. The interpretation of the alternate form is entirely
up to the Formattable implementation.
LEFT_JUSTIFY means that the -
flag was used and that the Formattable should pad
its output on the right, instead of on the left.
UPPERCASE indicates that the %S
conversion was used instead of %s and the
Formattable should output uppercase characters
instead of lowercase.
The width and
precision arguments specify the width and
precision specified along with the %s format
specifier, or -1 if no width and precision are specified. The
Formattable object should treat these values the
same way that Formatter does. The text to be
output should first be truncated to fit within
precision characters and then padded on
the left (or right if the LEFT_JUSTIFY flag is
set) with spaces for a total length of
width characters. Note that a
Formattable implementation may fulfill the
obligations imposed by the LEFT_JUSTIFY and
UPPERCASE flags and the
width and
precision arguments by constructing a
suitable format string to pass back to the specified
Formatter.
If a Formattable implementation wants to perform
locale-specific formatting, it can query the
Locale of the Formatter with
the locale( ) method. Note, however, that the
returned value is the locale specified when the
Formatter was created, not the
Locale, if any, passed to the format(
) method. There is no way for a
Formattable object to access that
Locale.
public interface Formattable {
// Public Instance Methods
void formatTo(java.util.Formatter formatter, int flags, int width, int precision);
}