- All Implemented Interfaces:
Alignable
- Direct Known Subclasses:
NumberFormat,StringFormat,TemplateDateFormat,TextFormat
printf function.
Synopsis:
ObjectFormat of;
String formatted;
of = ObjectFormat.create("6d");
formatted = of.format(Integer.valueOf(3527));
[...]
of = ObjectFormat.create("-15s");
formatted = of.format("This is a test");
[...]
of = ObjectFormat.create("-5T");
formatted = of.format("This is another test");
Current supported formats are:
- d, i, u Integer formats
- o Octal format
- x Hexadecimal format
- f, g, G, e, E floating-point formats
- s, S text formats
- T text format
- D date format
The T text format is an extension that right- or left- justifies a given text. The D format uses a SimpleDateFormat format for a Date object. The usual flags and modifiers can be used, though full compatibility with typical C behavior is not guaranteed.
Subclasses must implement the setFormat method, and can implement its own setFlag method. Only in rare cases will need to override setFlags.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected char[]protected intprotected booleanprotected int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidalign(int i) Sets the alignment of the object.static ObjectFormatCreates the proper ObjectFormat object according to parameters, with a different default align for text and anything else.static ObjectFormatCreates the proper ObjectFormat object according to parameters.static ObjectFormatCreates the proper ObjectFormat object according to format.protected final StringexpandLeftToWidth(String field, char cjust) protected final StringexpandRightToWidth(String field) abstract StringFormats an object according to the format specification set bysetFormat.intGets the alignment.protected final voidrightAppend(StringBuilder sb, String s, char fillchar, int minchars, int maxchars) abstract Objectsample()Produce a sample of this format.protected booleansetFlag(char flag, int i) Sets the flags specific to this Class.protected voidsetFlags(char[] flags) protected charSets the format of the object.
-
Field Details
-
flags
protected char[] flags -
ralign
protected boolean ralign -
width
protected int width -
precision
protected int precision
-
-
Constructor Details
-
ObjectFormat
protected ObjectFormat()
-
-
Method Details
-
create
Creates the proper ObjectFormat object according to format.- Parameters:
format- the format- Returns:
- the ObjectFormat object for
format. - Throws:
SpecParsingException- if the format is not known.
-
create
public static ObjectFormat create(Class<?> clase, int width, int precision) throws SpecParsingException Creates the proper ObjectFormat object according to parameters, with a different default align for text and anything else.- Parameters:
clase- the class of the object to be formatted.width- see class description.precision- see class description.- Returns:
- the ObjectFormat object
- Throws:
SpecParsingException- if the class to be formatted is not known.
-
create
public static ObjectFormat create(Class<?> clase, int width, int precision, boolean right_align) throws SpecParsingException Creates the proper ObjectFormat object according to parameters.- Parameters:
clase- the class of the object to be formatted.width- see class description.precision- see class description.right_align- true to default right-align text and anything else, false otherwise.- Returns:
- the ObjectFormat object
- Throws:
SpecParsingException- if the class to be formatted is not known.
-
setFormat
Sets the format of the object.- Parameters:
format- the format.- Returns:
- the format type
- Throws:
SpecParsingException- if the format contains unknown or wrong format specs.
-
setFlags
- Throws:
SpecParsingException
-
setFlag
protected boolean setFlag(char flag, int i) Sets the flags specific to this Class.- Parameters:
flag- the flagi- the position of the flag in the flags String- Returns:
trueif the flag was understood,falseotherwise.
-
rightAppend
protected final void rightAppend(StringBuilder sb, String s, char fillchar, int minchars, int maxchars) -
expandLeftToWidth
- Parameters:
field- the field to expandcjust- the character used to expand (generally ' ')- Returns:
- the field expanded to current width
-
expandRightToWidth
- Parameters:
field- the field to expand- Returns:
- the field expanded to current width
-
align
public void align(int i) Description copied from interface:AlignableSets the alignment of the object. -
getAlignment
public int getAlignment()Description copied from interface:AlignableGets the alignment.- Specified by:
getAlignmentin interfaceAlignable- Returns:
- the alignment.
-
format
Formats an object according to the format specification set bysetFormat.Must be overriden by subclasses.
- Parameters:
o- the object to be formatted.- Returns:
- a string with the formatted object.
-
sample
Produce a sample of this format.- Returns:
- a String with a (more or less) random object appropriate to be formatted according to current format specifications.
-