Processor
constructs a String
result using
Formatter
specifications and values found in the StringTemplate
.
Unlike Formatter
, FormatProcessor
uses the value from the
embedded expression that immediately follows, without whitespace, the
format specifier.
For example:
FormatProcessor fmt = FormatProcessor.create(Locale.ROOT);
int x = 10;
int y = 20;
String result = fmt."%05d\{x} + %05d\{y} = %05d\{x + y}";
result
will be "00010 + 00020 = 00030"
.
Embedded expressions without a preceeding format specifier, use %s
by default.
FormatProcessor fmt = FormatProcessor.create(Locale.ROOT);
int x = 10;
int y = 20;
String result1 = fmt."\{x} + \{y} = \{x + y}";
String result2 = fmt."%s\{x} + %s\{y} = %s\{x + y}";
result1
and result2
will
both be "10 + 20 = 30"
.
The FormatProcessor
format specification used and exceptions thrown are the
same as those of Formatter
.
However, there are two significant differences related to the position of arguments.
An explict n$
and relative <
index will cause an exception due to
a missing argument list.
Whitespace appearing between the specification and the embedded expression will
also cause an exception.
FormatProcessor
allows the use of different locales. For example:
Locale locale = Locale.forLanguageTag("th-TH-u-nu-thai");
FormatProcessor thaiFMT = FormatProcessor.create(locale);
int x = 10;
int y = 20;
String result = thaiFMT."%4d\{x} + %4d\{y} = %5d\{x + y}";
result
will be
" ๑๐ + ๒๐ = ๓๐"
.
For day to day use, the predefined FormatProcessor#FMT
FormatProcessor
is available. FormatProcessor#FMT
is defined using the Locale#ROOT
.
Example:
int x = 10; int y = 20; String result = FMT."0x%04x\{x} + 0x%04x\{y} = 0x%04x\{x + y}";
result
will be "0x000a + 0x0014 = 0x001E"
.
Processor
Modifier and Type | Field and Description |
---|---|
public static final FormatProcessor | FMT
This predefined |
private final Locale |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public static FormatProcessor | Returns: a new instance ofFormatProcessor Locale used to formatCreate a new |
private static boolean | Returns: true if the specification is found and neededfragment to check fragment, boolean if the specification is needed needed)Find a format specification at the end of a fragment. |
public MethodHandle | linkage(List<String>
string template fragments fragments, MethodType method type, includes the StringTemplate receiver as
well as the value types type)Implements java. MethodHandle that when supplied with the values from
a StringTemplate will produce a result equivalent to that provided by
FormatProcessor#process(StringTemplate) .
|
public final String | Returns: constructedString a stringTemplate)StringTemplate instanceImplements java. String based on the fragments, format
specifications found in the fragments and values in the
supplied StringTemplate object.
|
private static String | Returns: format stringstring template fragments fragments)Convert |
FMT | back to summary |
---|---|
public static final FormatProcessor FMT This predefined int x = 10; int y = 20; String result = FMT."0x%04x\{x} + 0x%04x\{y} = 0x%04x\{x + y}"; result will be "0x000a + 0x0014 = 0x001E" .
|
locale | back to summary |
---|---|
private final Locale locale
|
FormatProcessor | back to summary |
---|---|
private FormatProcessor(Locale locale) Constructor. |
create | back to summary |
---|---|
public static FormatProcessor create(Locale locale) Create a new
|
findFormat | back to summary |
---|---|
private static boolean findFormat(String fragment, boolean needed) Find a format specification at the end of a fragment.
|
linkage | back to summary |
---|---|
public MethodHandle linkage(List<String> fragments, MethodType type) Implements java. Constructs a
Process template expressions can be specialized when the processor is
of type
Other FormatProcessor THAI_FMT = FormatProcessor.create(Locale.forLanguageTag("th-TH-u-nu-thai"));
THAI_FMT will now produce specialized MethodHandles by way
of FormatProcessor#linkage(List, MethodType) .
See FormatProcessor#process(StringTemplate) for more information.
|
process | back to summary |
---|---|
public final String process(StringTemplate stringTemplate) Implements java. Constructs a
If an embedded expression is not immediately preceded by a
specifier then a
|
stringTemplateFormat | back to summary |
---|---|
private static String stringTemplateFormat(List<String> fragments) Convert |