Class StringTemplate

java.lang.Object
com.globalmentor.text.StringTemplate

public final class StringTemplate extends Object
An efficient class for creating templates which can be repeatedly applied with string arguments. Non-string parameter components will be added to the string using their Object.toString() methods.

Example: new StringTemplate("ex", StringTemplate.STRING_PARAMETER, "le").apply("amp") yields "example".

Author:
Garret Wilson
See Also:
  • Field Details

  • Constructor Details

    • StringTemplate

      @Deprecated public StringTemplate(Object... components)
      Deprecated.
      in favor of the static factory method of(Object...).
      Component constructor.
      Parameters:
      components - The components that make up the template; any StringTemplate.Replacement instance will result in replaced content, while any other object will be included verbatim using Object.toString() when the template is applied.
      Throws:
      NullPointerException - if the given components array or one of the components is null.
  • Method Details

    • of

      public static StringTemplate of(Object... components)
      Creates a string template from the given components.
      Parameters:
      components - The components that make up the template; any StringTemplate.Replacement instance will result in replaced content, while any other object will be included verbatim using Object.toString() when the template is applied.
      Returns:
      A new string template from the given components.
      Throws:
      NullPointerException - if the given components array or one of the components is null.
    • builder

      public static StringTemplate.Builder builder()
      Builder factory.
      Returns:
      A new builder for a string template.
    • apply

      public String apply(Object... arguments)
      Applies the given template using the given arguments.
      Parameters:
      arguments - The arguments to be used when applying the template.
      Returns:
      A string indicating the applied template with the given arguments.
      Throws:
      NullPointerException - if the given arguments array or one of the arguments is null.
      ArrayIndexOutOfBoundsException - if there are insufficient arguments for the parameters of the given template.
      IllegalArgumentException - if there are too few arguments or more arguments than parameters of the given template.