FormatProcessor
FormatProcessor (문자열 템플릿 형식 프로세서)
StringTemplate.Processor (PREVIEW)로서, StringTemplate (PREVIEW)에 있는 값에 Formatter 사양을 적용해 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가 있어요. FMT는 Locale.ROOT로 정의돼요.
int x = 10;
int y = 20;
String result = FMT."0x%04x\{x} + 0x%04x\{y} = 0x%04x\{x + y}";
위 예에서 result의 값은 "0x000a + 0x0014 = 0x001E"이 돼요. 코드와 시그니처는 원문 그대로 보존돼요.