Interface MessageTemplate
- All Known Implementing Classes:
AbstractMessageTemplate,MessageTemplate0,MessageTemplate1,MessageTemplate2,MessageTemplate3,MessageTemplate4
public interface MessageTemplate
The
Each
Example creating a
MessageTemplate concept supports structured messages with typed parameters.Each
MessageTemplate instance has a unique getKey() that clearly identifies the MessageTemplate.MessageTemplate keys can be nested, to support message hierarchies:
// Has key: "ESSENTIALS"
MessageTemplate0 ROOT = MessageTemplates.root("ESSENTIALS");
// Has key: "ESSENTIALS.VALIDATION"
MessageTemplate0 VALIDATION = ROOT.subKey("VALIDATION");
// Has key: "ESSENTIALS.VALIDATION.AMOUNT_TOO_HIGH"
MessageTemplate2<BigDecimal, BigDecimal> AMOUNT_TOO_HIGH = VALIDATION.key2("AMOUNT_TOO_HIGH",
"Amount {0} is higher than {1}");
// Has key: "ESSENTIALS.VALIDATION.AMOUNT_TOO_LOW"
MessageTemplate2<BigDecimal, BigDecimal> AMOUNT_TOO_LOW = VALIDATION.key2("AMOUNT_TOO_LOW",
"Amount {0} is lower than {1}");
There are multiple MessageTemplate subclasses that support a different number of parameters:
MessageTemplate0which supports 0 parameters (mostly used forMessageTemplates.root(String)or nested keys)MessageTemplate1which supports 1 parameterMessageTemplate2which supports 2 parametersMessageTemplate3which supports 3 parametersMessageTemplate4which supports 4 parameters
Example defining a MessageTemplate4's:
// Has key: "ESSENTIALS"
MessageTemplate0 ROOT = MessageTemplates.root("ESSENTIALS");
// Has key: "ESSENTIALS.ACCOUNT_OVERDRAWN"
MessageTemplate4<String, BigDecimal, BigDecimal, LocalDate> ACCOUNT_OVERDRAWN = ROOT.key4("ACCOUNT_OVERDRAWN",
"Account {0} is overdrawn by ${1}. A fee of ${2} will be debited on the {3}");
From a MessageTemplate we can create Message instances, which are useful for e.g. error reporting.
A Message is an instance of a MessageTemplate with parameters bound to it.Example creating a
Message from a MessageTemplate4:
MessageTemplate4<String, BigDecimal, BigDecimal, LocalDate> ACCOUNT_OVERDRAWN = ROOT.key4("ACCOUNT_OVERDRAWN",
"Account {0} is overdrawn by ${1}. A fee of ${2} will be debited on the {3}");
String accountId = "Account1";
BigDecimal overdrawnAmount = new BigDecimal("125");
BigDecimal feeAmount = new BigDecimal("10");
LocalDate feeDebitDate = LocalDate.of(2023, 2, 25);
Message msg = ACCOUNT_OVERDRAWN.create(accountId,
overdrawnAmount,
feeAmount,
feeDebitDate);
will create a Message with Message.getMessage():
"Account Account1 is overdrawn by $125. A fee of $10 will be debited on the 2023-2-25" (date formatting is dependent on the Locale)- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionThe default Message for the given message key
E.g.getKey()The message key for thisMessageTemplate.
-
Method Details
-
getKey
String getKey()The message key for thisMessageTemplate. Message key's can be used for multiple purposes:- A Message key is the identifier for a message or error (e.g. INVENTORY_ITEM.OUT_OF_STOCK)
- A Message key can be used to lookup translations (e.g. in a Resource bundle)
- Returns:
- the message key for this
MessageTemplate
-
getDefaultMessage
String getDefaultMessage()The default Message for the given message key
E.g. say thegetKey()isSALES.INVENTORY_ITEM.OUT_OF_STOCKand the concreteMessageTemplateis aMessageTemplate1then a default english message for this key could be:Inventory item {0} is out of Stock, which would be defined as:MessageTemplate1<ProductName> INVENTORY_OUT_OF_STOCK = MessageTemplates.key1("INVENTORY_ITEM.OUT_OF_STOCK", "Inventory item {0} is out of Stock");- Returns:
- the default message for the given message key
-