java.lang.Object
java.util.FormatProcessor
public final class FormatProcessor extends Object implements StringTemplate.ProcessorPREVIEW<String,RuntimeException>, StringTemplate.Processor.LinkagePREVIEW
FormatProcessor 是 Java 平台的预览 API。
预览功能可能会在将来的版本中被移除,或升级为 Java 平台的永久功能。
这个
在上面的示例中,
在上面的示例中,
在上面的示例中,
在上面的示例中,
StringTemplate.Processor预览使用Formatter规范和在StringTemplate预览中找到的值构造一个String结果。与Formatter不同,FormatProcessor预览使用紧随其后的嵌入表达式的值,而不使用空格,作为格式说明符。例如:
FormatProcessor fmt = FormatProcessor.create(Locale.ROOT);
int x = 10;
int y = 20;
String result = fmt."%05d\{x} + %05d\{y} = %05d\{x + y}";
result的值将是"00010 + 00020 = 00030"。
没有前导格式说明符的嵌入表达式,默认使用%s。
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和result2的值都将是"10 + 20 = 30"。
FormatProcessor预览使用的格式规范和抛出的异常与Formatter相同。
然而,与参数位置相关的两个显著差异。显式的n$和相对的<索引将导致由于缺少参数列表而引发异常。在规范和嵌入表达式之间出现的空格也会引发异常。
FormatProcessor预览允许使用不同的区域设置。例如:
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的值将是" ๑๐ + ๒๐ = ๓๐"。
对于日常使用,预定义的FMT FormatProcessor预览可用。使用Locale.ROOT定义了FMT。例如:
int x = 10;
int y = 20;
String result = FMT."0x%04x\{x} + 0x%04x\{y} = 0x%04x\{x + y}";
result的值将是"0x000a + 0x0014 = 0x001E"。
- 自 JDK 21 起:
- 21
- 参见:
-
Nested Class Summary
Nested classes/interfaces declared in interface java.lang.StringTemplate.ProcessorPREVIEW
StringTemplate.Processor.LinkagePREVIEW -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic FormatProcessorPREVIEW使用指定的区域设置创建一个新的FormatProcessor预览。linkage(List<String> fragments, MethodType type) final Stringprocess(StringTemplatePREVIEW stringTemplate)
-
Field Details
-
FMT
这个预定义的FormatProcessor预览实例使用 Locale.ROOTLocale构造一个String结果。更多详情请参阅FormatProcessor预览。例如:在上面的示例中,int x = 10; int y = 20; String result = FMT."0x%04x\{x} + 0x%04x\{y} = 0x%04x\{x + y}";result的值将是"0x000a + 0x0014 = 0x001E"。- 参见:
-
-
Method Details
-
create
使用指定的区域设置创建一个新的FormatProcessor预览。- 参数:
-
locale- 用于格式化的Locale - 返回:
-
一个新的
FormatProcessor预览实例 - 抛出:
-
NullPointerException- 如果 locale 为 null
-
process
基于片段、在片段中找到的格式规范和提供的StringTemplate预览对象中的值构造一个String。该方法从片段构造格式字符串,收集值并评估表达式,就像评估new Formatter(locale).format(format, values).toString()一样。如果嵌入表达式不是紧随着格式说明符,则在格式中插入
%s。- 指定者:
-
process在接口StringTemplate.Processor预览<String,RuntimeException> - 参数:
-
stringTemplate- 一个StringTemplate预览 实例 - 返回值:
-
构造的
String - 抛出:
-
IllegalFormatException- 如果格式说明符包含非法语法、与给定参数不兼容的格式说明符、格式说明符后面没有紧跟嵌入表达式或其他非法条件。有关所有可能的格式错误的规范,请参阅格式化程序类规范的 详细信息 部分。 -
NullPointerException- 如果 stringTemplate 为 null - 参见:
-
linkage
构造一个MethodHandle,当提供来自StringTemplate预览 的值时,将产生与process(StringTemplate)提供的结果等效的结果。这个MethodHandle被FMT和类似的工具用于执行更专业的结果组合。这种专业化是通过预扫描StringTemplate预览 的片段和值类型来完成的。当处理器是类型为
StringTemplate.Processor.Linkage预览 并且从静态常量中获取时,处理模板表达式可以被专门化,就像FMT一样(static final FormatProcessor)。其他
FormatProcessors预览 可以在存储为静态常量时被专门化。例如:FormatProcessor THAI_FMT = FormatProcessor.create(Locale.forLanguageTag("th-TH-u-nu-thai"));THAI_FMT现在将通过linkage(List, MethodType)生成专门化的MethodHandles。有关更多信息,请参阅process(StringTemplate)。- 指定者:
-
linkage在接口StringTemplate.Processor.Linkage预览 - 参数:
-
fragments- 字符串模板片段 -
type- 方法类型,包括 StringTemplate 接收器以及值类型 - 返回值:
-
应用于模板的处理器的
MethodHandle - 抛出:
-
IllegalFormatException- 如果格式说明符包含非法语法、与给定参数不兼容的格式说明符、格式说明符后面没有紧跟嵌入表达式或其他非法条件。有关所有可能的格式错误的规范,请参阅格式化程序类规范的 详细信息 部分。 -
NullPointerException- 如果 fragments 或 type 为 null - 参见:
-
FormatProcessor。