Class StringTemplate


  • public final class StringTemplate
    extends java.lang.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:
    String.format(String, Object...), MessageFormat
    • Field Detail

      • STRING_PARAMETER

        public static final StringTemplate.Parameter STRING_PARAMETER
        The parameter indicating that a string argument should be expected.
      • NEWLINE

        public static final StringTemplate.Generator NEWLINE
        A generator for a newline appropriate for this system.
        See Also:
        System.lineSeparator()
    • Constructor Detail

      • StringTemplate

        @Deprecated
        public StringTemplate​(java.lang.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:
        java.lang.NullPointerException - if the given components array or one of the components is null.
    • Method Detail

      • of

        public static StringTemplate of​(java.lang.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:
        java.lang.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 java.lang.String apply​(java.lang.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:
        java.lang.NullPointerException - if the given arguments array or one of the arguments is null.
        java.lang.ArrayIndexOutOfBoundsException - if there are insufficient arguments for the parameters of the given template.
        java.lang.IllegalArgumentException - if there are too few arguments or more arguments than parameters of the given template.