Package com.globalmentor.text
Class StringTemplate
java.lang.Object
com.globalmentor.text.StringTemplate
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:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Builder for a string template.static interface
Content to be generated.static interface
A parameter to be replaced by an argument.static interface
A component to be replaced in a template. -
Field Summary
Modifier and TypeFieldDescriptionstatic final StringTemplate.Generator
A generator for a newline appropriate for this system.static final StringTemplate.Parameter
The parameter indicating that a string argument should be expected. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionApplies the given template using the given arguments.static StringTemplate.Builder
builder()
Builder factory.static StringTemplate
Creates a string template from the given components.
-
Field Details
-
STRING_PARAMETER
The parameter indicating that a string argument should be expected. -
NEWLINE
A generator for a newline appropriate for this system.- See Also:
-
-
Constructor Details
-
StringTemplate
Deprecated.in favor of the static factory methodof(Object...)
.Component constructor.- Parameters:
components
- The components that make up the template; anyStringTemplate.Replacement
instance will result in replaced content, while any other object will be included verbatim usingObject.toString()
when the template is applied.- Throws:
NullPointerException
- if the given components array or one of the components isnull
.
-
-
Method Details
-
of
Creates a string template from the given components.- Parameters:
components
- The components that make up the template; anyStringTemplate.Replacement
instance will result in replaced content, while any other object will be included verbatim usingObject.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 isnull
.
-
builder
Builder factory.- Returns:
- A new builder for a string template.
-
apply
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 isnull
.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.
-
of(Object...)
.