Package org.techbd.util
Class InterpolateEngine
java.lang.Object
org.techbd.util.InterpolateEngine
The
Interpolate
class provides a mechanism for replacing placeholders
within strings using a combination of simple key-value pairs and Spring
Expression Language (SpEL) expressions. It supports configurable placeholder
delimiters and allows extension via custom functions.
This class can be extended or inherited to add more custom functions. For example:
final var engine = new Interpolate(Map.of("fsHome", fsHome, "artifactId", artifactId)) { public String customFunction() { return "CustomValue"; } }; final var result = engine.interpolate("${fsHome}/${customFunction()}/${formattedDateNow('yyyy/MM/dd/HH')}/${artifactId}.json")
-
Constructor Summary
ConstructorDescriptionInterpolateEngine
(Map<String, Object> vars, String... requiredPlaceholderNames) Constructs anInterpolate
instance with the given variables and default placeholder delimiters.InterpolateEngine
(Map<String, Object> vars, String prefix, String suffix, String defaultValueSeparator, boolean ignoreUnresolvablePlaceholders, String... requiredPlaceholderNames) Constructs anInterpolate
instance with the given variables and configurable placeholder delimiters. -
Method Summary
Modifier and TypeMethodDescriptionstatic String
cwd()
Return the current working directory (cwd), usually where Java process was launched from.static String
Return the value of an environment variable.static String
formattedDateNow
(String dateFmt) Returns the current date formatted according to the given date pattern.interpolate
(String srcText) Interpolates the given source text by replacing placeholders with their corresponding values.withValues
(String... keyValues) Adds key-value pairs to the set of placeholders.
-
Constructor Details
-
InterpolateEngine
Constructs anInterpolate
instance with the given variables and default placeholder delimiters.- Parameters:
vars
- a map of variables to be used for placeholder replacement
-
InterpolateEngine
public InterpolateEngine(Map<String, Object> vars, String prefix, String suffix, String defaultValueSeparator, boolean ignoreUnresolvablePlaceholders, String... requiredPlaceholderNames) Constructs anInterpolate
instance with the given variables and configurable placeholder delimiters.- Parameters:
vars
- a map of variables to be used for placeholder replacementprefix
- the prefix for placeholderssuffix
- the suffix for placeholdersdefaultValueSeparator
- the separator for default values within placeholdersignoreUnresolvablePlaceholders
- whether to ignore unresolvable placeholders
-
-
Method Details
-
withValues
Adds key-value pairs to the set of placeholders.- Parameters:
keyValues
- an array of key-value pairs to be added- Returns:
- the current
Interpolate
instance for method chaining - Throws:
IllegalArgumentException
- if the key-values array is null or not in pairs
-
interpolate
Interpolates the given source text by replacing placeholders with their corresponding values.- Parameters:
srcText
- the source text containing placeholders- Returns:
- the interpolated text with placeholders replaced by their values
- Throws:
IllegalArgumentException
- if the source text is null
-
formattedDateNow
Returns the current date formatted according to the given date pattern. This function can be used within placeholders as${formattedDateNow('yyyy/MM/dd/HH')}
.- Parameters:
dateFmt
- the date format pattern- Returns:
- the formatted current date
- Throws:
IllegalArgumentException
- if the date format is null or empty
-
env
Return the value of an environment variable.- Returns:
- String
-
cwd
Return the current working directory (cwd), usually where Java process was launched from.- Returns:
- String
-