Class StringUtils

java.lang.Object
net.solarnetwork.util.StringUtils

public final class StringUtils extends Object
Common string helper utilities.
Version:
1.11
Author:
matt
  • Field Details

    • NAMES_PATTERN

      public static final Pattern NAMES_PATTERN
      Pattern to capture template variable names of the form {name}.
    • INTEGER_PATTERN

      public static Pattern INTEGER_PATTERN
      A pattern to match integer number values.
      Since:
      1.11
    • DECIMAL_PATTERN

      public static Pattern DECIMAL_PATTERN
      A pattern to match decimal number values.
      Since:
      1.11
    • DIGEST_PREFIX_PATTERN

      public static final Pattern DIGEST_PREFIX_PATTERN
      A pattern for matching {type-len}digest style digest strings.
      Since:
      1.7
    • NOT_SIMPLE_ID_CHARACTER_PATTERN

      public static final Pattern NOT_SIMPLE_ID_CHARACTER_PATTERN
      A pattern that matches any character not allowed in simpleIdValue(String).
      Since:
      1.8
    • UNDERSCORE_PREFIX_OR_SUFFIX

      public static final Pattern UNDERSCORE_PREFIX_OR_SUFFIX
      A pattern that matches any _ at the start or end of a string.
      Since:
      1.8
  • Method Details

    • expandTemplateString

      public static String expandTemplateString(String source, Map<String,?> variables)
      Replace variables in a string template with corresponding values.

      Template variables are encoded like {name:default} where the :default part is optional. The name value is treated as a key in the provided variables map, and any corresponding value found is turned into a string and replaces the template variable in the resulting string. The optional default value, if provided, will be used as the variable value if name is not found in variables.

      Adapted from the org.springframework.web.util.UriComponents class, mimicking URI path variable substitutions.

      Parameters:
      source - the template string to replace variables in
      variables - the variables
      Returns:
      the string with variables replaced, or null if source is null
      Since:
      1.4
    • commaDelimitedStringFromCollection

      public static String commaDelimitedStringFromCollection(Collection<?> set)
      Get a comma-delimited string from a collection of objects.
      Parameters:
      set - the set
      Returns:
      the comma-delimited string
      See Also:
    • delimitedStringFromCollection

      public static String delimitedStringFromCollection(Collection<?> set, String delim)
      Get a delimited string from a collection of objects.

      This will call the Object.toString() method on each object in the set, using the natural iteration ordering of the set.No attempt to escape delimiters within the set's values is done.

      Parameters:
      set - the set
      delim - the delimiter
      Returns:
      the delimited string
    • delimitedStringFromMap

      public static String delimitedStringFromMap(Map<?,?> map)
      Get a delimited string from a map of objects.

      This will call delimitedStringFromMap(Map, String, String) using a = key value delimiter and a , pair delimiter.

      Parameters:
      map - the map
      Returns:
      the string
    • delimitedStringFromMap

      public static String delimitedStringFromMap(Map<?,?> map, String keyValueDelim, String pairDelim)
      Get a delimited string from a map of objects.

      This will call the Object.toString() method on each key and value in the map, using the natural iteration ordering of the map. No attempt to escape delimiters within the map's values is done.

      Parameters:
      map - the map
      keyValueDelim - the delimited to use between keys and values
      pairDelim - the delimiter to use betwen key/value pairs
      Returns:
      the string
    • commaDelimitedStringToSet

      public static Set<String> commaDelimitedStringToSet(String list)
      Get a Set via a comma-delimited string value.
      Parameters:
      list - the comma-delimited string
      Returns:
      the Set, or null if list is null or an empty string
      See Also:
    • delimitedStringToSet

      public static Set<String> delimitedStringToSet(String list, String delim)
      Get a string Set via a delimited String value.

      The format of the list String should be a delimited list of values. Whitespace is permitted around the delimiter, and will be stripped from the values. Whitespace is also trimmed from the start and end of the input string. The list order is preserved in the iteration order of the returned Set.

      Parameters:
      list - the delimited text
      delim - the delimiter to split the list with
      Returns:
      the Set, or null if list is null or an empty string
    • commaDelimitedStringToMap

      public static Map<String,String> commaDelimitedStringToMap(String mapping)
      Get string Map via a comma-delimited String value.

      The format of the mapping String should be:

       key=val[,key=val,...]
       
      Parameters:
      mapping - the delimited text
      Returns:
      the map, or null if mapping is null or empty
      See Also:
    • delimitedStringToMap

      public static Map<String,String> delimitedStringToMap(String mapping, String recordDelim, String fieldDelim)
      Get a string Map via a delimited String value.

      The format of the mapping String should be:

       key=val[,key=val,...]
       

      The record and field delimiters are passed as parameters to this method. Whitespace is permitted around all delimiters, and will be stripped from the keys and values. Whitespace is also trimmed from the start and end of the input string.

      Parameters:
      mapping - the delimited text
      recordDelim - the key+value record delimiter
      fieldDelim - the key+value delimiter
      Returns:
      the map, or null if mapping is null or empty
    • patterns

      public static Pattern[] patterns(String[] expressions, int flags)
      Create an array of regular expressions from strings. If expressions is null or empty, the result will be null. Pass 0 for flags if no special flags are desired.
      Parameters:
      expressions - the array of expressions to compile into Pattern objects
      flags - the Pattern flags to use, or 0 for no flags
      Returns:
      the compiled regular expressions, in the same order as expressions, or null if no expressions supplied
      Throws:
      PatternSyntaxException - If an expression's syntax is invalid
    • expressions

      public static String[] expressions(Pattern[] patterns)
      Create an array of expression strings from Pattern objects. If patterns is null or empty, the result will be null.
      Parameters:
      patterns - the array of Pattern objects to convert to strings (may be null)
      Returns:
      the string expressions, in the same order as patterns, or null if no patterns supplied
    • matches

      public static Matcher matches(Pattern[] patterns, String text)
      Test if a string matches any one of a list of patterns. The patterns list will be tested one at a time, in array order. The first result that matches will be returned. If no match is found, null is returned.
      Parameters:
      patterns - the patterns to test (may be null)
      text - the string to test (may be null)
      Returns:
      a Matcher that matches text or null if no match was found
    • parseBoolean

      public static boolean parseBoolean(String s)
      Get a boolean value from a String.

      This method is more generous than Boolean.parseBoolean(String). The following values are considered true, all ignoring case:

      • 1
      • t
      • true
      • y
      • yes

      All other values (or a missing value) is considered false.

      Parameters:
      s - the string to parse as a boolean
      Returns:
      the parsed boolean result
      Since:
      1.6
    • sha256Base64Value

      public static final String sha256Base64Value(String propertyValue)
      Compute a Base64-encoded SHA-256 digest of a string value with a random salt.
      Parameters:
      propertyValue - the current property value
      Returns:
      a Base64 encoded SHA-256 digest with a {SSHA-256} prefix
      Since:
      1.7
    • sha256Base64Value

      public static final String sha256Base64Value(String propertyValue, byte[] salt)
      Compute a Base64-encoded SHA-256 digest of a string value with optional salt.

      When salt is provided, the digest is computed from propertyValue + salt and then the returned Base64 value contains digest + salt. The length of the salt can be determined after decoding the Base64 value, as decodedLength - 32.

      Parameters:
      propertyValue - the current property value
      salt - the optional salt to add
      Returns:
      a Base64 encoded SHA-256 digest with a {SSHA-256} prefix (if salt provided) or {SHA-256} if no salt provided
      Since:
      1.7
    • decodeBase64DigestComponents

      public static final KeyValuePair decodeBase64DigestComponents(String digest)
      Decode a digest string in the form {key}value into a key-value pair composed of digest (key) and salt (value) hex-encoded values.

      The returned pair's value will be null if no salt was included in the digest. Both values will be returned as hex-encoded strings.

      Parameters:
      digest - a Base64-encoded digest string, in the form returned by sha256Base64Value(String)
      Returns:
      a key/value pair of the
      Since:
      1.7
    • sha256MaskedMap

      public static <K, V> Map<K,V> sha256MaskedMap(Map<K,V> map, Set<K> maskKeys)
      "Mask" a set of map values by replacing them with SHA-256 digest values.

      This method will return a new map instance, unless no values need masking in which case map itself will be returned. For any key in maskKeys found in map, the returned map's value will be the SHA-256 digest value computed from the string form of the value passed to sha256Base64Value(String).

      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      map - the map of values to mask
      maskKeys - the set of map keys whose values should be masked
      Returns:
      either a new map instance with one or more values masked, or map when no values need masking
      Since:
      1.7
      See Also:
    • simpleIdValue

      public static String simpleIdValue(String text)
      Generate a "simple" ID out of a string.

      A simple ID is created by taking text and:

      1. leading and trailing whitespace is removed
      2. change to lower case
      3. replace any runs of characters other than a-zA-Z0-9_ with a _
      Parameters:
      text - the text to derive the simple ID from
      Returns:
      the simple ID, or null if text is null
      Since:
      1.8
    • utf8length

      public static int utf8length(CharSequence text)
      Calcualte the UTF-8 byte length of a given string.

      This is faster than converting the string to a byte array in the UTF-8 encoding.

      Parameters:
      text - the text to calcualte the UTF-8 length of
      Returns:
      the length
      Since:
      1.10
    • numberValue

      public static Number numberValue(String text)
      Parse a number value if possible.

      This method will return either a BigInteger or BigDecimal value.

      Parameters:
      text - the string to parse
      Returns:
      a number instance if text can be parsed as a number, or null otherwise
      Since:
      1.11
    • match

      public static String[] match(Pattern pattern, String text)
      Test if a string matches a pattern, returning the text along with any capture groups as an array if there is a match.

      Note that Matcher.find() is used, so the pattern matches anywhere in text by default.

      Parameters:
      pattern - the pattern
      text - the string to test against pattern
      Returns:
      if the string does not match or either argument is null, null; otherwise an array whose first element is text and any additional elements are pattern capture values
      Since:
      1.11