Class MoreStringUtils


  • public class MoreStringUtils
    extends java.lang.Object
    Inevitably we need to write StringUtils functions that are not defined elsewhere. To make things more convenient for ourselves, we extend from Apache's version.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static com.google.common.io.BaseEncoding BASE64
      Standard base 64 encoding
      static com.google.common.io.BaseEncoding BASE64_OMIT_PADDING
      Base 64 encoding (without padding).
      static com.google.common.base.Joiner COMMA_JOINER
      Comma joiner
      static com.google.common.base.Splitter COMMA_SPLITTER
      Splitter for comma separated values
      static com.google.common.base.Joiner COMMA_WHITESPACE_JOINER
      Comma whitespace joiner
      static com.google.common.base.Splitter COMMA_WHITESPACE_SPLITTER
      Splitter for comma separated values with whitespite
      static java.lang.String[] EMPTY_STRINGS
      Useful constant to avoid returning nulls
      static com.google.common.io.BaseEncoding HEX_LOWERCASE
      Hex encoding with lowercase letters
      static com.google.common.io.BaseEncoding HEX_UPPERCASE
      Hex encoding with lowercase letters
      static com.google.common.base.Joiner SPACE_JOINER
      Space separated joiner
      static char SYSTEM_SEPARATOR
      The system separator character.
      static char UNIX_SEPARATOR
      The Unix separator character.
      static java.util.regex.Pattern UUID_PATTERN
      UUID
      static char WINDOWS_SEPARATOR
      The Windows separator character.
    • Constructor Summary

      Constructors 
      Constructor Description
      MoreStringUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.io.File asFile​(java.io.File parent, java.lang.String filename)
      Returns a File from the given filename.
      static java.io.File asFile​(java.lang.String filename)
      Returns a File from the given filename.
      static boolean isUuid​(java.lang.String uuid)
      Returns true if the given input is a UUID (accepts UUIDs with or without hyphens)
      static java.lang.String maskExcept​(java.lang.String s, int unmaskedLength)
      Returns a masked string, leaving only the given number of characters unmasked.
      static java.lang.String maskExcept​(java.lang.String s, int unmaskedLength, char maskChar)
      Returns a masked string, leaving only the given number of characters unmasked.
      static java.lang.String normalizeSeparators​(java.lang.String filename)
      Returns a normalized filename, where the incoming filename has backslashes converted to forward slashes (on Unix OS), and vice-versa on Windows OS, otherwise, the path separation methods of File will not work.
      static java.lang.String normalizeTypography​(java.lang.String value)  
      static java.lang.String replaceNonPrintableControlCharacters​(java.lang.String value)
      Returns a string where non-printable control characters are replaced by whitespace.
      static java.lang.String shortUuid()
      Returns a short UUID which is encoding use base 64 characters instead of hexadecimal.
      static java.lang.String[] split​(java.lang.String value)
      Parses a comma-separated string and returns an array of String values.
      static java.util.List<java.lang.String> splitToList​(java.lang.String value)
      Parses a comma-separated string and returns a list of String values.
      static java.lang.String toString​(long duration, java.util.concurrent.TimeUnit units)  
      static java.lang.String toString​(java.util.UUID value)
      Converts the given UUID to a String (without hyphens).
      static java.lang.String[] toStringArray​(java.util.Collection<java.lang.String> collection)
      Returns a String array from the given collection.
      static java.lang.String trimWhitespace​(java.lang.String s)
      Strip leading and trailing whitespace from a String.
      static java.lang.String trimWhitespaceToNull​(java.lang.String s)
      Strip leading and trailing whitespace from a String.
      static java.lang.String uuid()
      Returns a random UUID as String (without hyphens).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • COMMA_SPLITTER

        public static final com.google.common.base.Splitter COMMA_SPLITTER
        Splitter for comma separated values
      • COMMA_WHITESPACE_SPLITTER

        public static final com.google.common.base.Splitter COMMA_WHITESPACE_SPLITTER
        Splitter for comma separated values with whitespite
      • SPACE_JOINER

        public static final com.google.common.base.Joiner SPACE_JOINER
        Space separated joiner
      • COMMA_JOINER

        public static final com.google.common.base.Joiner COMMA_JOINER
        Comma joiner
      • COMMA_WHITESPACE_JOINER

        public static final com.google.common.base.Joiner COMMA_WHITESPACE_JOINER
        Comma whitespace joiner
      • EMPTY_STRINGS

        public static final java.lang.String[] EMPTY_STRINGS
        Useful constant to avoid returning nulls
      • HEX_UPPERCASE

        public static final com.google.common.io.BaseEncoding HEX_UPPERCASE
        Hex encoding with lowercase letters
      • HEX_LOWERCASE

        public static final com.google.common.io.BaseEncoding HEX_LOWERCASE
        Hex encoding with lowercase letters
      • BASE64_OMIT_PADDING

        public static final com.google.common.io.BaseEncoding BASE64_OMIT_PADDING
        Base 64 encoding (without padding). Warning: Some clients can't parse this
      • BASE64

        public static final com.google.common.io.BaseEncoding BASE64
        Standard base 64 encoding
      • UUID_PATTERN

        public static final java.util.regex.Pattern UUID_PATTERN
        UUID
      • UNIX_SEPARATOR

        public static final char UNIX_SEPARATOR
        The Unix separator character.
        See Also:
        Constant Field Values
      • WINDOWS_SEPARATOR

        public static final char WINDOWS_SEPARATOR
        The Windows separator character.
        See Also:
        Constant Field Values
      • SYSTEM_SEPARATOR

        public static final char SYSTEM_SEPARATOR
        The system separator character.
    • Constructor Detail

      • MoreStringUtils

        public MoreStringUtils()
    • Method Detail

      • asFile

        public static java.io.File asFile​(java.io.File parent,
                                          java.lang.String filename)
        Returns a File from the given filename. Ensures that the incoming filename has backslashes converted to forward slashes (on Unix OS), and vice-versa on Windows OS, otherwise, the path separation methods of File will not work.
        Parameters:
        parent - parent file name
        filename - filename to be used
        Returns:
        a File from the given filename.
      • asFile

        public static java.io.File asFile​(java.lang.String filename)
        Returns a File from the given filename. Ensures that the incoming filename has backslashes converted to forward slashes (on Unix OS), and vice-versa on Windows OS, otherwise, the path separation methods of File will not work.
        Parameters:
        filename - filename to be used
        Returns:
        a File from the given filename.
      • isUuid

        public static boolean isUuid​(java.lang.String uuid)
        Returns true if the given input is a UUID (accepts UUIDs with or without hyphens)
        Parameters:
        uuid - candidate UUID
        Returns:
        UUID
      • maskExcept

        public static java.lang.String maskExcept​(java.lang.String s,
                                                  int unmaskedLength)
        Returns a masked string, leaving only the given number of characters unmasked. The mask character is an asterisk.
        Parameters:
        s - string that requires masking
        unmaskedLength - number of characters to leave unmasked; if positive, the unmasked characters are at the end of the string, otherwise the unmasked characters are at the start of the string.
        Returns:
        a masked string
      • maskExcept

        public static java.lang.String maskExcept​(java.lang.String s,
                                                  int unmaskedLength,
                                                  char maskChar)
        Returns a masked string, leaving only the given number of characters unmasked.
        Parameters:
        s - string that requires masking
        unmaskedLength - number of characters to leave unmasked; if positive, the unmasked characters are at the end of the string, otherwise the unmasked characters are at the start of the string.
        maskChar - character to be used for masking
        Returns:
        a masked string
      • normalizeSeparators

        public static java.lang.String normalizeSeparators​(java.lang.String filename)
        Returns a normalized filename, where the incoming filename has backslashes converted to forward slashes (on Unix OS), and vice-versa on Windows OS, otherwise, the path separation methods of File will not work. Note that unlike FilenameUtils.normalize(String), this method does not change or remove any relative paths, etc, which could cause the return value to become null.
        Parameters:
        filename - filename whose separators should be normalized
        Returns:
        filename with normalized separators
      • normalizeTypography

        public static java.lang.String normalizeTypography​(java.lang.String value)
      • replaceNonPrintableControlCharacters

        public static java.lang.String replaceNonPrintableControlCharacters​(java.lang.String value)
        Returns a string where non-printable control characters are replaced by whitespace.
        Parameters:
        value - string which may contain non-printable characters
        Returns:
        a string where non-printable control characters are replaced by whitespace.
      • shortUuid

        public static java.lang.String shortUuid()
        Returns a short UUID which is encoding use base 64 characters instead of hexadecimal. This saves 10 bytes per UUID.
        Returns:
        short UUID which is encoding use base 64 characters instead of hexadecimal
      • split

        public static java.lang.String[] split​(java.lang.String value)
        Parses a comma-separated string and returns an array of String values.
        Parameters:
        value - comma-separated string
        Returns:
        array of String values
      • splitToList

        public static java.util.List<java.lang.String> splitToList​(java.lang.String value)
        Parses a comma-separated string and returns a list of String values.
        Parameters:
        value - comma-separated string
        Returns:
        list of String values
      • toString

        public static java.lang.String toString​(long duration,
                                                java.util.concurrent.TimeUnit units)
      • toString

        public static java.lang.String toString​(java.util.UUID value)
        Converts the given UUID to a String (without hyphens). UUID.toString().replace("-","") works ok functionally, but we can make it go much faster (by 4x with micro-benchmark). NOTE: This method was lifted from Jackson's UUIDSerializer.
        Parameters:
        value - UUID value
        Returns:
        a String representation of the UUID
      • toStringArray

        public static java.lang.String[] toStringArray​(java.util.Collection<java.lang.String> collection)
        Returns a String array from the given collection. The return value is never null.
        Parameters:
        collection - collection of Strings. May be null.
        Returns:
        array of Strings
      • trimWhitespace

        public static java.lang.String trimWhitespace​(java.lang.String s)
        Strip leading and trailing whitespace from a String. Spring's version of StringUtils.trimWhitespace needlessly creates a StringBuilder when whitespace characters are not present, and java.lang.String.trim() removes non-printable characters in a non-unicode safe way.
        Parameters:
        s - string to be trimmed
        Returns:
        input string with leading and trailing whitespace removed.
      • trimWhitespaceToNull

        public static java.lang.String trimWhitespaceToNull​(java.lang.String s)
        Strip leading and trailing whitespace from a String. If the resulting string is empty, this method returns null.
        Parameters:
        s - string to be trimmed
        Returns:
        input string with leading and trailing whitespace removed.
      • uuid

        public static java.lang.String uuid()
        Returns a random UUID as String (without hyphens).
        Returns:
        a random UUID as String (without hyphens).