Class InterpolateEngine

java.lang.Object
org.techbd.util.InterpolateEngine

public class InterpolateEngine extends Object
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 Details

    • InterpolateEngine

      public InterpolateEngine(Map<String,Object> vars, String... requiredPlaceholderNames)
      Constructs an Interpolate 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 an Interpolate instance with the given variables and configurable placeholder delimiters.
      Parameters:
      vars - a map of variables to be used for placeholder replacement
      prefix - the prefix for placeholders
      suffix - the suffix for placeholders
      defaultValueSeparator - the separator for default values within placeholders
      ignoreUnresolvablePlaceholders - whether to ignore unresolvable placeholders
  • Method Details

    • withValues

      public InterpolateEngine withValues(String... keyValues)
      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

      public String interpolate(String srcText)
      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

      public static String formattedDateNow(String dateFmt)
      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

      public static String env(String name)
      Return the value of an environment variable.
      Returns:
      String
    • cwd

      public static String cwd()
      Return the current working directory (cwd), usually where Java process was launched from.
      Returns:
      String