All Known Implementing Classes:
AbstractMessageTemplate, MessageTemplate0, MessageTemplate1, MessageTemplate2, MessageTemplate3, MessageTemplate4

public interface MessageTemplate
The 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:

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 Details

    • getKey

      String getKey()
      The message key for this MessageTemplate. 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 the getKey() is SALES.INVENTORY_ITEM.OUT_OF_STOCK and the concrete MessageTemplate is a MessageTemplate1 then 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