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
Modifier and TypeClassDescriptionstatic class
AMessageFmt.Segment
within aMessageFormat
pattern that formats one of the arguments.static class
AnMessageFmt.ArgumentSegment
that formats its argument using aChoiceFormat
.static class
AnMessageFmt.ArgumentSegment
that formats its argument using theNumberFormat
currency instance for the locale.static class
AMessageFormat
argument segment that formats the argument using aDateFormat
.static enum
Enumerates the standard, pre-defined formats for dates and times inDateFormat
.static class
AnMessageFmt.ArgumentSegment
that formats its argument using aDecimalFormat
.static class
AnMessageFmt.ArgumentSegment
that simply formats the argument using the default formatting for its type.static class
AnMessageFmt.ArgumentSegment
that formats its argument using the defaultNumberFormat
for the locale.static class
MessageFmt.FormatArgumentSegment<T extends Format>
AnMessageFmt.ArgumentSegment
that formats its argument using aFormat
of some kind.static class
AnMessageFmt.ArgumentSegment
that formats its argument using theNumberFormat
integer instance for the locale.static class
AnMessageFmt.ArgumentSegment
that formats its argument using aNumberFormat
.static class
AnMessageFmt.ArgumentSegment
that formats its argument using theNumberFormat
percent instance for the locale.static class
Represents one atomic portion of aMessageFormat
pattern string.static interface
Visitor pattern interface forMessageFmt.Segment
subclasses.static class
Adapter class forMessageFmt.SegmentSwitch
implementations.static class
AMessageFormat
argument segment that formats the argument using aSimpleDateFormat
.static class
AnMessageFmt.ArgumentSegment
that formats its argument using one of the standardDateFormat
date instances for the locale.static class
AnMessageFmt.ArgumentSegment
that formats its argument using one of the standardDateFormat
time instances for the locale.static class
Represents a stretch of plain text in aMessageFormat
pattern string. -
Constructor Summary
ConstructorDescriptionDefault constructor.MessageFmt
(MessageFormat format) Create an instance modeling the givenMessageFormat
.MessageFmt
(MessageFormat format, boolean captureLocaleDefaults) Create an instance modeling the givenMessageFormat
with optional capturing ofLocale
defaults.MessageFmt
(MessageFmt.Segment... segments) Create an instance from explicitly givenMessageFmt.Segment
s. -
Method Summary
Modifier and TypeMethodDescriptionvoid
checkValid
(jakarta.validation.ConstraintValidatorContext context) Validate this instance.boolean
static String
EscapeMessageFormat
special characters.@NotNull @Valid List<@NotNull MessageFmt.Segment>
Get the individual components of this message format.int
hashCode()
void
setSegments
(List<MessageFmt.Segment> segments) Build theMessageFormat
represented by this instance using the default format locale.toMessageFormat
(Locale locale) Build theMessageFormat
represented by this instance using the specifiedLocale
.Build theMessageFormat
pattern string represented by this instance.toString()
static String
Un-escape escapedMessageFormat
special characters.
-
Constructor Details
-
MessageFmt
public MessageFmt()Default constructor.Creates an empty instance.
-
MessageFmt
Create an instance from explicitly givenMessageFmt.Segment
s.- Parameters:
segments
- message components- Throws:
IllegalArgumentException
- ifsegments
or any element thereof is null
-
MessageFmt
Create an instance modeling the givenMessageFormat
.Equivalent to:
MessageFmt
(format, false)
.- Parameters:
format
- source message format- Throws:
IllegalArgumentException
- ifformat
is null
-
MessageFmt
Create an instance modeling the givenMessageFormat
with optional capturing ofLocale
defaults.The
captureLocaleDefaults
parameter controls whetherMessageFmt.FormatArgumentSegment
s that refer toLocale
defaults are allowed. Such segments produce different results depending on the locale; seeFormatArgumentSegment.of()
. IfcaptureLocaleDefaults
is 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/20
Note that regardless of
captureLocaleDefaults
, someMessageFormat
arguments are alwaysLocale
-dependent. For example, a simple argument parameter like{0}
, when applied to a numerical argument, is always formatted using theLocale
default number format.- Parameters:
format
- source message formatcaptureLocaleDefaults
- true to capture locale defaults, false to allow implicit locale defaults- Throws:
IllegalArgumentException
- ifformat
is nullRuntimeException
- if reflective access intoMessageFormat
is denied- See Also:
-
-
Method Details
-
getSegments
Get the individual components of this message format.- Returns:
- message format segments
-
setSegments
-
toMessageFormat
Build theMessageFormat
represented 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 theMessageFormat
represented by this instance using the specifiedLocale
.This method is equivalent to:
new MessageFormat(this.
toPattern
(), locale)
.- Parameters:
locale
- locale forMessageFormat
- Returns:
- an equivalent
MessageFormat
using the givenLocale
- See Also:
-
toPattern
Build theMessageFormat
pattern string represented by this instance.- Returns:
- an equivalent
MessageFormat
pattern string
-
escape
EscapeMessageFormat
special characters. The characters that are special forMessageFormat
are opening and closing curly braces. However, due to lenient parsing byMessageFormat
we only need to escape opening curly braces.- Parameters:
string
- unescaped input string- Returns:
string
with charactersMessageFormat
considers special escaped
-
unescape
Un-escape escapedMessageFormat
special characters.This takes the output from
MessageFmt.escape()
and returns the original string.- Parameters:
string
- escaped input string- Returns:
string
with escaping added byMessageFmt.escape()
reverted
-
checkValid
public void checkValid(jakarta.validation.ConstraintValidatorContext context) throws SelfValidationException Description copied from interface:SelfValidating
Validate this instance.- Specified by:
checkValid
in interfaceSelfValidating
- Parameters:
context
- validation context- Throws:
SelfValidationException
- to indicate this instance is invalid
-
equals
-
hashCode
public int hashCode() -
toString
-