Class StringTemplateFunction

  • All Implemented Interfaces:
    Expression, Function

    public class StringTemplateFunction
    extends Object
    implements Function
    StringTemplate function, applies a regular expression with capturing groups, and them uses the groups to build a new string based on the provided template.

    This function expects four expressions in input, the first three of which are mandatory.

    1. The string to be matched
    2. A regular expression, possibly using capturing groups
    3. A template using ${0} for the fully matched string, ${n} for the n-th matching group
    4. An optional value returned if the regular expresion does not match the input string
    Here are some examples using CQL as the expression language:
    1. stringTemplate('2002-03-01T13:00:00Z', '\d{4}-\d{2}-\d{2}T(\d{2}):\d{2}:\d{2}', '${1}') will result in 13 (the only matching group returns the hour of the timestamp)
    2. stringTemplate('abcd', '\d{4}-\d{2}-\d{2}T(\d{2}):\d{2}:\d{2}', '${1}') will return null (no match)
    3. stringTemplate('abcd', '\d{4}-\d{2}-\d{2}T(\d{2}):\d{2}:\d{2}', '${1}', 'default') will return default (no match, but there is a default value
    • Field Detail

      • NAME

        public static final FunctionName NAME
        Make the instance of FunctionName available in a consistent spot.
    • Constructor Detail

      • StringTemplateFunction

        public StringTemplateFunction()
      • StringTemplateFunction

        public StringTemplateFunction​(List<Expression> parameters,
                                      Literal fallback)
    • Method Detail

      • getName

        public String getName()
        Description copied from interface: Function
        Returns the name of the function to be called. For example, this might be "cos" or "atan2".

        You can use this name to look up the number of required parameters in a FilterCapabilities data structure. For the specific meaning of the required parameters you will need to consult the documentation.

        Specified by:
        getName in interface Function
      • getFunctionName

        public FunctionName getFunctionName()
        Description copied from interface: Function
        Access to the FunctionName description as used in a FilterCapabilities document.
        Specified by:
        getFunctionName in interface Function
        Returns:
        FunctionName description, if available.
      • getParameters

        public List<Expression> getParameters()
        Description copied from interface: Function
        Returns the list subexpressions that will be evaluated to provide the parameters to the function.
        Specified by:
        getParameters in interface Function
      • accept

        public Object accept​(ExpressionVisitor visitor,
                             Object extraData)
        Description copied from interface: Expression
        Accepts a visitor. Subclasses must implement with a method whose content is the following:
        return visitor.visit(this, extraData);
        Specified by:
        accept in interface Expression
      • evaluate

        public Object evaluate​(Object object)
        Description copied from interface: Expression
        Evaluates the given expression based on the content of the given object.
        Specified by:
        evaluate in interface Expression
        Returns:
        computed value
      • evaluate

        public <T> T evaluate​(Object object,
                              Class<T> context)
        Description copied from interface: Expression
        Evaluates the given expressoin based on the content of the given object and the context type.

        The context parameter is used to control the type of the result of the expression. A particular expression may not be able to evaluate to an instance of context. Therefore to be safe calling code should do a null check on the return value of this method, and call Expression.evaluate(Object) if neccessary. Example:

          Object input = ...;
          String result = expression.evaluate( input, String.class );
          if ( result == null ) {
             result = expression.evalute( input ).toString();
          }
          ...
         

        Implementations that can not return a result as an instance of context should return null.

        Specified by:
        evaluate in interface Expression
        Type Parameters:
        T - The type of the returned object.
        Parameters:
        object - The object to evaluate the expression against.
        context - The type of the resulting value of the expression.
        Returns:
        Evaluates the given expression based on the content of the given object an an instance of context.
      • getFallbackValue

        public Literal getFallbackValue()
        Description copied from interface: Function
        The value of the fallbackValue attribute is used as a default value, if the SE implementation does not support the function. If the implementation supports the function, then the result value is determined by executing the function.
        Specified by:
        getFallbackValue in interface Function
        Returns:
        Optional literal to use if an implementation for this function is not available.
      • toString

        public String toString()
        Creates a String representation of this Function with the function name and the arguments.
        Overrides:
        toString in class Object