public static class InternalFormat.Spec
extends java.lang.Object
Spec
objects may be merged such that one Spec
provides values,
during the construction of a new Spec
, for attributes that are unspecified in a
primary source.
This structure is returned by factory method InternalFormat.fromText(String)
, and having public
final members is freely accessed by formatters such as FloatFormatter
, and the
__format__ methods of client object types.
The fields correspond to the elements of a format specification. The grammar of a format specification is:
[[fill]align][sign][#][0][width][,][.precision][type]A typical idiom is:
private static final InternalFormatSpec FLOAT_DEFAULTS = InternalFormatSpec.from(">"); ... InternalFormat.Spec spec = InternalFormat.fromText(specString); spec = spec.withDefaults(FLOAT_DEFAULTS); ... // Validation of spec.type, and other attributes, for this type. FloatFormatter f = new FloatFormatter(spec); String result = f.format(value).getResult();
Modifier and Type | Field | Description |
---|---|---|
char |
align |
Alignment indicator is one of {
'<', '^', '>', '=' , or U+FFFF if
unspecified. |
boolean |
alternate |
The alternative format flag '#' was given.
|
char |
fill |
The fill character specified, or U+FFFF if unspecified.
|
boolean |
grouping |
Insert the grouping separator (which in Python always indicates a group-size of 3).
|
static char |
NONE |
Non-character code point used to represent "no value" in
char attributes. |
static InternalFormat.Spec |
NUMERIC |
Defaults applicable to most numeric types.
|
int |
precision |
Precision decoded from the format, or -1 if unspecified.
|
char |
sign |
Sign-handling flag, one of
'+' , '-' , or ' ' , or
U+FFFF if unspecified. |
static InternalFormat.Spec |
STRING |
Defaults applicable to string types.
|
char |
type |
Type key from the format, or U+FFFF if unspecified.
|
static int |
UNSPECIFIED |
Negative value used to represent "no value" in
int attributes. |
int |
width |
Width to which to pad the result, or -1 if unspecified.
|
Constructor | Description |
---|---|
Spec(char fill,
char align,
char sign,
boolean alternate,
int width,
boolean grouping,
int precision,
char type) |
Constructor to set all the fields in the format specifier.
|
Spec(int precision,
char type) |
Constructor offering just precision and type.
|
Modifier and Type | Method | Description |
---|---|---|
char |
getAlign(char defaultAlign) |
The alignment from the parsed format specification, or default.
|
char |
getFill(char defaultFill) |
The alignment from the parsed format specification, or default.
|
int |
getPrecision(int defaultPrecision) |
The precision from the parsed format specification, or default.
|
char |
getType(char defaultType) |
The type code from the parsed format specification, or default supplied.
|
static boolean |
specified(char c) |
Test to see if an attribute has been specified.
|
static boolean |
specified(int value) |
Test to see if an attribute has been specified.
|
java.lang.String |
toString() |
Return a format specifier (text) equivalent to the value of this Spec.
|
InternalFormat.Spec |
withDefaults(InternalFormat.Spec other) |
Return a merged
Spec object, in which any attribute of this object that is
specified (or true ), has the same value in the result, and any attribute of
this object that is unspecified (or false ), has the value that attribute
takes in the other object. |
public final char fill
public final char align
'<', '^', '>', '='
, or U+FFFF if
unspecified.public final char sign
'+'
, '-'
, or ' '
, or
U+FFFF if unspecified.public final boolean alternate
public final int width
public final boolean grouping
public final int precision
public final char type
public static final char NONE
char
attributes.public static final int UNSPECIFIED
int
attributes.public static final InternalFormat.Spec NUMERIC
public static final InternalFormat.Spec STRING
public Spec(char fill, char align, char sign, boolean alternate, int width, boolean grouping, int precision, char type)
[[fill]align][sign][#][0][width][,][.precision][type]
fill
- fill character (or NONE
align
- alignment indicator, one of {'<', '^', '>', '='
sign
- policy, one of '+'
, '-'
, or ' '
.alternate
- true to request alternate formatting mode ('#'
flag).width
- of field after padding or -1 to defaultgrouping
- true to request comma-separated groupsprecision
- (e.g. decimal places) or -1 to defaulttype
- indicator characterpublic Spec(int precision, char type)
[.precision][type]
precision
- (e.g. decimal places)type
- indicator characterpublic static final boolean specified(char c)
c
- attributeNONE
public static final boolean specified(int value)
value
- of attributepublic java.lang.String toString()
toString
in class java.lang.Object
public InternalFormat.Spec withDefaults(InternalFormat.Spec other)
Spec
object, in which any attribute of this object that is
specified (or true
), has the same value in the result, and any attribute of
this object that is unspecified (or false
), has the value that attribute
takes in the other object. Thus the second object supplies default values. (These
defaults may also be unspecified.) The use of this method is to allow a Spec
constructed from text to record exactly, and only, what was in the textual specification,
while the __format__ method of a client object supplies its type-specific defaults. Thus
"20" means "<20s" to a str
, ">20.12" to a float
and ">20.12g"
to a complex
.other
- defaults to merge where this object does not specify the attribute.public char getFill(char defaultFill)
public char getAlign(char defaultAlign)
public int getPrecision(int defaultPrecision)
public char getType(char defaultType)