Package com.globalmentor.text
Class StringTemplate
- java.lang.Object
-
- com.globalmentor.text.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 theirObject.toString()
methods.Example:
new StringTemplate("ex", StringTemplate.STRING_PARAMETER, "le").apply("amp")
yields "example".- Author:
- Garret Wilson
- See Also:
String.format(String, Object...)
,MessageFormat
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
StringTemplate.Builder
Builder for a string template.static interface
StringTemplate.Generator
Content to be generated.static interface
StringTemplate.Parameter
A parameter to be replaced by an argument.static interface
StringTemplate.Replacement
A component to be replaced in a template.
-
Field Summary
Fields Modifier and Type Field Description static StringTemplate.Generator
NEWLINE
A generator for a newline appropriate for this system.static StringTemplate.Parameter
STRING_PARAMETER
The parameter indicating that a string argument should be expected.
-
Constructor Summary
Constructors Constructor Description StringTemplate(java.lang.Object... components)
Deprecated.in favor of the static factory methodof(Object...)
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
apply(java.lang.Object... arguments)
Applies the given template using the given arguments.static StringTemplate.Builder
builder()
Builder factory.static StringTemplate
of(java.lang.Object... components)
Creates a string template from the given components.
-
-
-
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 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:
java.lang.NullPointerException
- if the given components array or one of the components isnull
.
-
-
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; 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:
java.lang.NullPointerException
- if the given components array or one of the components isnull
.
-
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 isnull
.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.
-
-