- Type Parameters:
R
- Processor's process result typeE
- Exception thrown type
- All Known Subinterfaces:
StringProcessorPREVIEW
,TemplateProcessorPREVIEW<R>
- All Known Implementing Classes:
FormatProcessorPREVIEW
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
ValidatingProcessor
is a preview API of the Java platform.
process(StringTemplate)
is used to validate
and compose a result using a StringTemplate's
PREVIEW fragments and values lists.
For example:
class MyProcessor implements ValidatingProcessor<String, IllegalArgumentException> {
@Override
public String process(StringTemplate st) throws IllegalArgumentException {
StringBuilder sb = new StringBuilder();
Iterator<String> fragmentsIter = st.fragments().iterator();
for (Object value : st.values()) {
sb.append(fragmentsIter.next());
if (value instanceof Boolean) {
throw new IllegalArgumentException("I don't like Booleans");
}
sb.append(value);
}
sb.append(fragmentsIter.next());
return sb.toString();
}
}
MyProcessor myProcessor = new MyProcessor();
try {
int x = 10;
int y = 20;
String result = myProcessor."\{x} + \{y} = \{x + y}";
...
} catch (IllegalArgumentException ex) {
...
}
The user has the option of validating inputs used in composition. For example an SQL
processor could prevent injection vulnerabilities by sanitizing inputs or throwing an
exception of type E
if an SQL statement is a potential vulnerability.
Composing allows user control over how the result is assembled. Most often, a
user will construct a new string from the string template, with placeholders
replaced by string representations of value list elements. These string
representations are created as if invoking String.valueOf(java.lang.Object)
.
Transforming allows the processor to return something other than a string. For instance, a JSON processor could return a JSON object, by parsing the string created by composition, instead of the composed string.
ValidatingProcessor
PREVIEW is a FunctionalInterface
. This permits
declaration of a processor using lambda expressions;
ValidatingProcessor<String, RuntimeException> processor = st -> {
List<String> fragments = st.fragments();
List<Object> values = st.values();
// check or manipulate the fragments and/or values
...
return StringTemplate.interpolate(fragments, values);
};
FunctionalInterface
TemplateProcessor
PREVIEW is supplied to avoid
the use of checked exceptions;
TemplateProcessor<String> processor = st -> {
List<String> fragments = st.fragments();
List<Object> values = st.values();
// check or manipulate the fragments and/or values
...
return StringTemplate.interpolate(fragments, values);
};
FunctionalInterface
StringProcessor
PREVIEW is supplied if
the processor returns String
;
StringProcessor processor = st -> {
List<String> fragments = st.fragments();
List<Object> values = st.values();
// check or manipulate the fragments and/or values
...
return StringTemplate.interpolate(fragments, values);
};
StringTemplate.interpolate()
PREVIEW method is available for those processors
that just need to work with the string interpolation;
StringProcessor processor = StringTemplate::interpolate;
String
;
TemplateProcessor<JSONObject> jsonProcessor = st -> new JSONObject(st.interpolate());
- Implementation Note:
- The Java compiler automatically imports
StringTemplate.STR
PREVIEW - See Java Language Specification:
-
15.8.6 Process Template Expressions
- Since:
- 21
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionprocess
(StringTemplatePREVIEW stringTemplate) Constructs a result based on the template fragments and values in the suppliedstringTemplate
PREVIEW object.
-
Method Details
-
process
Constructs a result based on the template fragments and values in the suppliedstringTemplate
PREVIEW object.- Parameters:
stringTemplate
- aStringTemplate
PREVIEW instance- Returns:
- constructed object of type R
- Throws:
E
- exception thrown by the template processor when validation fails
-
ValidatingProcessor
when preview features are enabled.