org.dellroad.stuff.string
Class NamedArgumentFormatter

java.lang.Object
  extended by 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 Summary
protected  ArrayList<String> fieldList
          The list of argument names in the order they are used.
protected  HashMap<String,Integer> fieldMap
          Mapping from argument name to argument list index.
protected  String indexedFormat
          The modified format string containing indexes instead of argument names.
protected  String originalFormat
          The original format string provided to the constructor.
 
Constructor Summary
NamedArgumentFormatter(String format)
          Constructor.
 
Method Summary
 String format(Map<String,Object> argMap)
          Format the string using the given arguments.
 Set<String> getArgumentNames()
          Get the argument names found in the configured format string.
 String getFormat()
          Get the original format string provided to the constructor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

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 Detail

NamedArgumentFormatter

public NamedArgumentFormatter(String format)
Constructor.

Parameters:
format - format string containing argument names instead of indicies
Method Detail

format

public String format(Map<String,Object> argMap)
Format the string using the given arguments.

Parameters:
argMap - mapping from argument name to argument value
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.


getArgumentNames

public Set<String> getArgumentNames()
Get the argument names found in the configured format string.

Returns:
argument names as a unmodifiable set