Class NamedArgumentFormatter

java.lang.Object
org.dellroad.stuff.string.NamedArgumentFormatter

public class NamedArgumentFormatter extends Object
Interpreter for printf-style formatting strings that identifies arguments using names rather than indicies.

Works just like Formatter but arguments are specified using names instead of numbers. A mapping from argument name to argument value must be provided during the format operation.

Since:
1.0.64
  • Field Details

    • fieldList

      protected final ArrayList<String> fieldList
      The list of argument names in the order they are used. This is the inverse mapping of fieldMap.
    • fieldMap

      protected final HashMap<String,Integer> fieldMap
      Mapping from argument name to argument list index. This is the inverse mapping of fieldList.
    • originalFormat

      protected final String originalFormat
      The original format string provided to the constructor.
    • indexedFormat

      protected final String indexedFormat
      The modified format string containing indexes instead of argument names.
  • Constructor Details

    • NamedArgumentFormatter

      public NamedArgumentFormatter(String format)
      Constructor.
      Parameters:
      format - format string containing argument names instead of indicies
  • Method Details

    • format

      public String format(Map<String,Object> argMap)
      Format the string using the given arguments.
      Parameters:
      argMap - mapping from argument name to argument value
      Returns:
      formatted string
      Throws:
      IllegalFormatException - if the format provided to the constructor contained illegal syntax
      IllegalFormatException - if an argument value is incompatible or missing (and null would be invalid)
    • getFormat

      public String getFormat()
      Get the original format string provided to the constructor.
      Returns:
      original format string
    • getArgumentNames

      public Set<String> getArgumentNames()
      Get the argument names found in the configured format string.
      Returns:
      argument names as a unmodifiable set