Class StringExpressionVariable

java.lang.Object
com.broadleafcommerce.rulesengine.expression.util.StringExpressionVariable
All Implemented Interfaces:
ExpressionVariable

public class StringExpressionVariable extends Object implements ExpressionVariable

Common, null-safe, string operations

An instance of this class will usually be accessible as a variable within an expression via #strings. This is essentially a wrapper around StringUtils to provide a layer of abstraction and because StringUtils employs static methods. Note that not all methods of StringUtils have been implemented just those that seemed most relevant for expression evaluation.

Author:
Nathan Moore (nathanmoore).
  • Field Details

  • Constructor Details

    • StringExpressionVariable

      public StringExpressionVariable()
      Sets locale to Locale.ENGLISH
    • StringExpressionVariable

      public StringExpressionVariable(Locale locale)
  • Method Details

    • getName

      public final String getName()
      Specified by:
      getName in interface ExpressionVariable
      Returns:
      Name of the variable, must not be null
    • toString

      @Nullable public final String toString(@Nullable Object target)
      Converts the target to a string.
      Parameters:
      target - object to convert
      Returns:
      string representation of object
    • isEmpty

      public final boolean isEmpty(@Nullable Object o)
      Whether the string is null or empty ("")
    • isNotEmpty

      public final boolean isNotEmpty(@Nullable Object o)
      Whether the string is not null or empty ("")
    • isBlank

      public final boolean isBlank(@Nullable Object o)
      Whether the string is null, empty (""), or blank (" ")
    • isNotBlank

      public final boolean isNotBlank(@Nullable Object o)
      Whether the string is not null, empty(""),or blank (" ")
    • trim

      @Nullable public final String trim(@Nullable Object o)
      Removes control characters (char <= 32) from both ends of this String, handling null by returning null.
    • trimToNull

      @Nullable public final String trimToNull(@Nullable Object o)
      Returns null if the string is empty after trimming
    • trimToEmpty

      public final String trimToEmpty(@Nullable Object o)
      Returns an empty string ("") if the string is null or empty after trimming.
    • notEquals

      public final boolean notEquals(Object o, String str2)
      Return true if strings are not equal, else false.
    • notEqualsIgnoreCase

      public final boolean notEqualsIgnoreCase(Object o, String str2)
      Return true if strings are not equal, ignoring case, else false
    • truncate

      @Nullable public final String truncate(@Nullable Object o, int offset, int maxWidth)
      Creates a substring using the specified left-offset and max width.
    • strip

      @Nullable public final String strip(@Nullable Object o)
      Strips whitespace from the start and end of a String.
    • stripToNull

      @Nullable public final String stripToNull(@Nullable Object o)
      Returns null if the stripped string is empty.
    • stripToEmpty

      public final String stripToEmpty(@Nullable Object o)
      Returns an empty string ("") if the string is null or empty after stripping
    • stripChars

      @Nullable public final String stripChars(@Nullable Object o, @Nullable String stripChars)
      Strips the specified characters from the beginning and end of the string.
    • equals

      public final boolean equals(@Nullable Object o, @Nullable CharSequence cs2)
      Null-safe string equals.
    • equalsIgnoreCase

      public final boolean equalsIgnoreCase(@Nullable Object o, @Nullable CharSequence cs2)
      Null-safe string equals. Ignores case
    • compare

      public final int compare(@Nullable Object o, @Nullable String str2)
      Null-safe string compare.
    • compareIgnoreCase

      public final int compareIgnoreCase(@Nullable Object o, @Nullable String str2)
      Null-safe string compare. Ignores case
    • compareNullIsMore

      public final int compareNullIsMore(@Nullable Object o, @Nullable String str2)
      Null-safe string compare. Where null is greater than not null.
    • compareIgnoreCaseNullIsMore

      public final int compareIgnoreCaseNullIsMore(@Nullable Object o, @Nullable String str2)
      Null-safe string compare. Where null is greater than not null. Ignores case
    • indexOf

      public final int indexOf(@Nullable Object o, @Nullable CharSequence searchSeq)
      Returns the index of the given substring with the string starting at the given position.
    • indexOfIgnoreCase

      public final int indexOfIgnoreCase(@Nullable Object o, @Nullable CharSequence searchSeq)
      Returns the index of the given substring with the string starting at the given position. Ignores case.
    • lastIndexOf

      public final int lastIndexOf(@Nullable Object o, @Nullable CharSequence searchChar)
      Returns the index of the last occurrence of the given substring.
    • lastIndexOfIgnoreCase

      public final int lastIndexOfIgnoreCase(@Nullable Object o, @Nullable CharSequence searchChar)
      Returns the index of the last occurrence of the given substring. Ignores case.
    • contains

      public final boolean contains(@Nullable Object o, @Nullable CharSequence searchChar)
      Whether the string contains the substring
    • notContains

      public final boolean notContains(@Nullable Object o, @Nullable CharSequence searchChar)
    • containsIgnoreCase

      public final boolean containsIgnoreCase(@Nullable Object o, @Nullable CharSequence searchChar)
      Whether the string contains the substring. Ignores case.
    • notContainsIgnoreCase

      public final boolean notContainsIgnoreCase(@Nullable Object o, @Nullable CharSequence searchChar)
    • containsWhitespace

      public final boolean containsWhitespace(@Nullable Object o)
      Whether the string contains whitespace.
    • substring

      @Nullable public final String substring(@Nullable Object o, int start, int end)
      Gets a substring defined by start and end indices from the specified string safely.
    • leftMost

      @Nullable public final String leftMost(@Nullable Object o, int len)
      Gets a substring from the left-most characters.
    • rightMost

      @Nullable public final String rightMost(@Nullable Object o, int len)
      Gets a substring from the right-most characters.
    • split

      @Nullable public final String[] split(@Nullable Object o, @Nullable String separator)
      Splits the provided string into an array by the separator string specified.
      Parameters:
      str - String to split
      separator - Separator to separate on
      Returns:
      Split string array
    • splitToList

      public final List<String> splitToList(@Nullable Object o, @Nullable String separator)
      Splits the provided string into a list instead of an array.
    • join

      @SafeVarargs @Nullable public final <T> String join(@Nullable T... elements)
      Joins the elements of the provided array into a single String containing the provided of elements
    • joinWith

      @SafeVarargs public final <T> String joinWith(@Nullable String separator, T... elements)
      Joins the elements of the provided array into a single String containing the provided of elements, placing the given separator between elements
    • joinWith

      @Nullable public final String joinWith(@Nullable String separator, @Nullable Iterable<?> iterable)
      Joins the elements of the provided iterable into a single String containing the provided of elements, placing the given separator between elements
    • deleteWhitespace

      @Nullable public final String deleteWhitespace(@Nullable Object o)
      Deletes all whitespaces from a String as defined by Character.isWhitespace(char).
    • remove

      @Nullable public final String remove(@Nullable String str, @Nullable String removeStr)
      Removes all occurrences of a substring from within the source string.
    • removeIgnoreCase

      @Nullable public final String removeIgnoreCase(@Nullable String str, @Nullable String removeStr)
      Removes all occurrences of a substring from within the source string. Ignores case
    • replaceAll

      @Nullable public final String replaceAll(@Nullable String text, @Nullable String regex, @Nullable String replacement)

      Replaces each substring of the text String that matches the given regular expression with the given replacement.

      This method is a null safe equivalent to:

      • text.replaceAll(regex, replacement)
      • Pattern.compile(regex).matcher(text).replaceAll(replacement)

    • replace

      @Nullable public final String replace(@Nullable String text, @Nullable String replaceTxt, @Nullable String replacement)
      Replaces each substring of the text String that matches the given regular expression with the given replacement up to timesToReplace.
      Parameters:
      text - text to search and replace in
      replaceTxt - text to replace
      replacement - what to replace text with
      Returns:
      string with replacements
    • replaceIgnoreCase

      @Nullable public final String replaceIgnoreCase(@Nullable String text, @Nullable String replaceTxt, @Nullable String replacement)
      Replaces each substring of the text String that matches the given regular expression with the given replacement up to timesToReplace. Ignores case.
      Parameters:
      text - text to search and replace in
      replaceTxt - text to replace
      replacement - what to replace text with
      Returns:
      string with replacements
    • chomp

      public final String chomp(@Nullable String str)
      Removes one newline from end of the string if present
    • chop

      @Nullable public final String chop(@Nullable String str)
      Remove the last character from a string
    • length

      public final int length(@Nullable Object o)
      Null-safe get string length
    • toUpper

      @Nullable public final String toUpper(@Nullable Object o)
      Null-safe convert to uppercase
    • toLower

      @Nullable public final String toLower(@Nullable Object o)
      Null-safe convert to lowercase
    • capitalize

      @Nullable public final String capitalize(@Nullable Object o)
      Null-safe convert to title case
    • reverse

      @Nullable public final String reverse(@Nullable Object o)
      Null-safe string reverse
    • startsWith

      public final boolean startsWith(@Nullable Object o, @Nullable CharSequence prefix)
      Whether the text starts with the prefix
    • startsWithIgnoreCase

      public final boolean startsWithIgnoreCase(@Nullable Object o, @Nullable CharSequence prefix)
      Whether the text starts with the prefix. Ignores case.
    • endsWith

      public final boolean endsWith(@Nullable Object o, @Nullable CharSequence prefix)
      Whether the text ends with the prefix.
    • endsWithIgnoreCase

      public final boolean endsWithIgnoreCase(@Nullable Object o, @Nullable CharSequence prefix)
      Whether the text ends with the prefix. Ignores case.
    • wrap

      @Nullable public final String wrap(@Nullable Object o, @Nullable String wrapWith)
      Wraps a string with a string.
    • wrapIfMissing

      @Nullable public final String wrapIfMissing(@Nullable Object o, @Nullable String wrapWith)
      Wraps a string with a string if not already present.
    • unwrap

      @Nullable public final String unwrap(@Nullable Object o, @Nullable String wrapToken)
      Unwraps a given string from anther string.
    • convertObjectToString

      protected String convertObjectToString(@Nullable Object o)