- All Implemented Interfaces:
ProcessorLinkagePREVIEW
,StringProcessorPREVIEW
,TemplateProcessorPREVIEW<String>
,ValidatingProcessorPREVIEW<String,
RuntimeException>
FormatProcessor
is a preview API of the Java platform.
StringProcessor
PREVIEW constructs a String
result using
Formatter
specifications and values found in the StringTemplate
PREVIEW.
Unlike Formatter
, FormatProcessor
PREVIEW uses the value from the
embedded expression that immediately follows, no whitespace, after 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"
.
FormatProcessor
PREVIEW format specification uses and exceptions 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
PREVIEW 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."%d\{x} + %d\{y} = %d\{x + y}";
result
will be
"๑๐ + ๒๐ = ๓๐"
.
For day to day use, the predefined FMT
FormatProcessor
PREVIEW
is available. 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"
.- Since:
- 21
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final FormatProcessorPREVIEW
This predefinedFormatProcessor
PREVIEW instance constructs aString
result using the Locale.ROOTLocale
. -
Method Summary
Modifier and TypeMethodDescriptionstatic FormatProcessorPREVIEW
Create a newFormatProcessor
PREVIEW using the specified locale.linkage
(List<String> fragments, MethodType type) Constructs aMethodHandle
that when supplied with the values from aStringTemplate
PREVIEW will produce a result equivalent to that provided byprocess(StringTemplate)
.final String
process
(StringTemplatePREVIEW stringTemplate) Constructs aString
based on the fragments, format specifications found in the fragments and values in the suppliedStringTemplate
PREVIEW object.
-
Field Details
-
FMT
This predefinedFormatProcessor
PREVIEW instance constructs aString
result using the Locale.ROOTLocale
. SeeFormatProcessor
PREVIEW for more details. 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"
.- See Also:
-
-
Method Details
-
create
Create a newFormatProcessor
PREVIEW using the specified locale.- Parameters:
locale
-Locale
used to format- Returns:
- a new instance of
FormatProcessor
PREVIEW - Throws:
NullPointerException
- if locale is null
-
process
Constructs aString
based on the fragments, format specifications found in the fragments and values in the suppliedStringTemplate
PREVIEW object. This method constructs a format string from the fragments, gathers up the values and evaluates the expressionnew Formatter(locale).format(format, values).toString()
.If an embedded expression is not immediately preceded by a specifier then a
%s
is inserted in the format.- Specified by:
process
in interfaceStringProcessorPREVIEW
- Specified by:
process
in interfaceTemplateProcessorPREVIEW<String>
- Specified by:
process
in interfaceValidatingProcessorPREVIEW<String,
RuntimeException> - Parameters:
stringTemplate
- aStringTemplate
PREVIEW instance- Returns:
- constructed
String
- Throws:
IllegalFormatException
- If a format specifier contains an illegal syntax, a format specifier that is incompatible with the given arguments, a specifier not followed immediately by an embedded expression or other illegal conditions. For specification of all possible formatting errors, see the details section of the formatter class specification.- See Also:
-
linkage
Constructs aMethodHandle
that when supplied with the values from aStringTemplate
PREVIEW will produce a result equivalent to that provided byprocess(StringTemplate)
. ThisMethodHandle
is used byFMT
and the ilk to perform a more specialized composition of a result. This is specialization is done by prescanning the fragments and value types of aStringTemplate
PREVIEW.Process template expressions can be specialized when the processor is of type
ProcessorLinkage
PREVIEW and fetched from a static constant as isFMT
(static final FormatProcessor
).Other
FormatProcessor
PREVIEW can be specialized if stored as static final. For example:FormatProcessor THAI_FMT = FormatProcessor.create(Locale.forLanguageTag("th-TH-u-nu-thai"));
THAI_FMT
will now produce specializedMethodHandles
by way oflinkage(List, MethodType)
. Seeprocess(StringTemplate)
for more information.- Specified by:
linkage
in interfaceProcessorLinkagePREVIEW
- Parameters:
fragments
- string template fragmentstype
- method type- Returns:
MethodHandle
for the processor applied to template- Throws:
IllegalFormatException
- If a format specifier contains an illegal syntax, a format specifier that is incompatible with the given arguments, a specifier not followed immediately by an embedded expression or other illegal conditions. For specification of all possible formatting errors, see the details section of the formatter class specification.- See Also:
-
FormatProcessor
when preview features are enabled.