- 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.
-
Field Summary
Modifier and TypeFieldDescriptionprotected char[]
protected int
protected boolean
protected int
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
align
(int i) Sets the alignment of the object.static ObjectFormat
Creates the proper ObjectFormat object according to parameters, with a different default align for text and anything else.static ObjectFormat
Creates the proper ObjectFormat object according to parameters.static ObjectFormat
Creates the proper ObjectFormat object according to format.protected final String
expandLeftToWidth
(String field, char cjust) protected final String
expandRightToWidth
(String field) abstract String
Formats an object according to the format specification set bysetFormat
.int
Gets the alignment.protected final void
rightAppend
(StringBuilder sb, String s, char fillchar, int minchars, int maxchars) abstract Object
sample()
Produce a sample of this format.protected boolean
setFlag
(char flag, int i) Sets the flags specific to this Class.protected void
setFlags
(char[] flags) protected char
Sets 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:
true
if the flag was understood,false
otherwise.
-
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:Alignable
Sets the alignment of the object. -
getAlignment
public int getAlignment()Description copied from interface:Alignable
Gets the alignment.- Specified by:
getAlignment
in 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.
-