Class MessageFmt
- All Implemented Interfaces:
SelfValidating
MessageFormat that models its internal structure, supporting arbitrary recursive
nesting of MessageFormat with ChoiceFormat.
The point of this class is to make it easier to work with MessageFormat instances by allowing for
structural introspection and eliminating complex quoting/escaping issues.
Locales
Unlike the MessageFormat class, instances of MessageFmt do not have an associated Locale; they
represent the structure of the format string only. However, some of that structure may implicitly refer to
Locale-provided defaults; for example, a MessageFmt.CurrencyArgumentSegment means "format as currency
using NumberFormat.getCurrencyInstance(Locale)", which produces a different result depending on the Locale.
So, in contrast to MessageFormat, the Locale to be used is always provided separately.
Having said that, it is possible to create MessageFmt instances that "capture" any Locale defaults
at the time of construction and therefore avoid the use of Locale-dependent segments such as
MessageFmt.CurrencyArgumentSegment; see MessageFmt(MessageFormat, boolean) for details.
Classes are annotated to support JSR 303 validation.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAMessageFmt.Segmentwithin aMessageFormatpattern that formats one of the arguments.static classAnMessageFmt.ArgumentSegmentthat formats its argument using aChoiceFormat.static classAnMessageFmt.ArgumentSegmentthat formats its argument using theNumberFormatcurrency instance for the locale.static classAMessageFormatargument segment that formats the argument using aDateFormat.static enumEnumerates the standard, pre-defined formats for dates and times inDateFormat.static classAnMessageFmt.ArgumentSegmentthat formats its argument using aDecimalFormat.static classAnMessageFmt.ArgumentSegmentthat simply formats the argument using the default formatting for its type.static classAnMessageFmt.ArgumentSegmentthat formats its argument using the defaultNumberFormatfor the locale.static classMessageFmt.FormatArgumentSegment<T extends Format>AnMessageFmt.ArgumentSegmentthat formats its argument using aFormatof some kind.static classAnMessageFmt.ArgumentSegmentthat formats its argument using theNumberFormatinteger instance for the locale.static classAnMessageFmt.ArgumentSegmentthat formats its argument using aNumberFormat.static classAnMessageFmt.ArgumentSegmentthat formats its argument using theNumberFormatpercent instance for the locale.static classRepresents one atomic portion of aMessageFormatpattern string.static interfaceVisitor pattern interface forMessageFmt.Segmentsubclasses.static classAdapter class forMessageFmt.SegmentSwitchimplementations.static classAMessageFormatargument segment that formats the argument using aSimpleDateFormat.static classAnMessageFmt.ArgumentSegmentthat formats its argument using one of the standardDateFormatdate instances for the locale.static classAnMessageFmt.ArgumentSegmentthat formats its argument using one of the standardDateFormattime instances for the locale.static classRepresents a stretch of plain text in aMessageFormatpattern string. -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.MessageFmt(MessageFormat format) Create an instance modeling the givenMessageFormat.MessageFmt(MessageFormat format, boolean captureLocaleDefaults) Create an instance modeling the givenMessageFormatwith optional capturing ofLocaledefaults.MessageFmt(MessageFmt.Segment... segments) Create an instance from explicitly givenMessageFmt.Segments. -
Method Summary
Modifier and TypeMethodDescriptionvoidcheckValid(jakarta.validation.ConstraintValidatorContext context) Validate this instance.booleanstatic StringEscapeMessageFormatspecial characters.@NotNull @Valid List<@NotNull MessageFmt.Segment>Get the individual components of this message format.inthashCode()voidsetSegments(List<MessageFmt.Segment> segments) Build theMessageFormatrepresented by this instance using the default format locale.toMessageFormat(Locale locale) Build theMessageFormatrepresented by this instance using the specifiedLocale.Build theMessageFormatpattern string represented by this instance.toString()static StringUn-escape escapedMessageFormatspecial characters.
-
Constructor Details
-
MessageFmt
public MessageFmt()Default constructor.Creates an empty instance.
-
MessageFmt
Create an instance from explicitly givenMessageFmt.Segments.- Parameters:
segments- message components- Throws:
IllegalArgumentException- ifsegmentsor any element thereof is null
-
MessageFmt
Create an instance modeling the givenMessageFormat.Equivalent to:
MessageFmt(format, false).- Parameters:
format- source message format- Throws:
IllegalArgumentException- ifformatis null
-
MessageFmt
Create an instance modeling the givenMessageFormatwith optional capturing ofLocaledefaults.The
captureLocaleDefaultsparameter controls whetherMessageFmt.FormatArgumentSegments that refer toLocaledefaults are allowed. Such segments produce different results depending on the locale; seeFormatArgumentSegment.of(). IfcaptureLocaleDefaultsis true, these implicit locale-dependent formats are not allowed; instead the actual formats are captured whenever possible.Here's a concrete example:
This would produce the following output:final Object[] args = new Object[] { new Date(1590000000000L) }; // May 20, 2020 final MessageFormat messageFormat = new MessageFormat("date = {0,date,short}", Locale.US); System.out.println("messageFormat -> " + messageFormat.format(args)); final MessageFmt messageFmt1 = new MessageFmt(messageFormat, false); // leave "date,short" alone; bind to locale later final MessageFmt messageFmt2 = new MessageFmt(messageFormat, true); // capture "date,short" in Locale.US System.out.println("messageFmt1.toPattern() = " + messageFmt1.toPattern()); System.out.println("messageFmt2.toPattern() = " + messageFmt2.toPattern()); final MessageFormat messageFormat1 = messageFmt1.toMessageFormat(Locale.FRANCE); final MessageFormat messageFormat2 = messageFmt2.toMessageFormat(Locale.FRANCE); System.out.println("messageFormat1 -> " + messageFormat1.format(args)); System.out.println("messageFormat2 -> " + messageFormat2.format(args));messageFormat -> date = 5/20/20 messageFmt1.toPattern() = date = {0,date,short} messageFmt2.toPattern() = date = {0,date,M/d/yy} messageFormat1 -> date = 20/05/20 messageFormat2 -> date = 5/20/20Note that regardless of
captureLocaleDefaults, someMessageFormatarguments are alwaysLocale-dependent. For example, a simple argument parameter like{0}, when applied to a numerical argument, is always formatted using theLocaledefault number format.- Parameters:
format- source message formatcaptureLocaleDefaults- true to capture locale defaults, false to allow implicit locale defaults- Throws:
IllegalArgumentException- ifformatis nullRuntimeException- if reflective access intoMessageFormatis denied- See Also:
-
-
Method Details
-
getSegments
Get the individual components of this message format.- Returns:
- message format segments
-
setSegments
-
toMessageFormat
Build theMessageFormatrepresented by this instance using the default format locale.This method is equivalent to:
new MessageFormat(this.toPattern()).- Returns:
- an equivalent
MessageFormat - See Also:
-
toMessageFormat
Build theMessageFormatrepresented by this instance using the specifiedLocale.This method is equivalent to:
new MessageFormat(this.toPattern(), locale).- Parameters:
locale- locale forMessageFormat- Returns:
- an equivalent
MessageFormatusing the givenLocale - See Also:
-
toPattern
Build theMessageFormatpattern string represented by this instance.- Returns:
- an equivalent
MessageFormatpattern string
-
escape
EscapeMessageFormatspecial characters. The characters that are special forMessageFormatare opening and closing curly braces. However, due to lenient parsing byMessageFormatwe only need to escape opening curly braces.- Parameters:
string- unescaped input string- Returns:
stringwith charactersMessageFormatconsiders special escaped
-
unescape
Un-escape escapedMessageFormatspecial characters.This takes the output from
MessageFmt.escape()and returns the original string.- Parameters:
string- escaped input string- Returns:
stringwith escaping added byMessageFmt.escape()reverted
-
checkValid
public void checkValid(jakarta.validation.ConstraintValidatorContext context) throws SelfValidationException Description copied from interface:SelfValidatingValidate this instance.- Specified by:
checkValidin interfaceSelfValidating- Parameters:
context- validation context- Throws:
SelfValidationException- to indicate this instance is invalid
-
equals
-
hashCode
public int hashCode() -
toString
-