Class StringUtils


  • public class StringUtils
    extends java.lang.Object
    Since:
    0.1.0
    • Constructor Summary

      Constructors 
      Constructor Description
      StringUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.String applyQuotes​(java.lang.String string)  
      static java.lang.String capitalize​(java.lang.String str)
      Capitalizes a String (makes the first char uppercase) taking care of blank strings and single character strings.
      private static java.lang.String escape​(char character)  
      static java.lang.String escapeRegexChars​(java.lang.String str)  
      static java.lang.String getCapitalizedName​(java.lang.String name)
      Converts foo-bar into Foo Bar.
      static java.lang.String getClassName​(java.lang.String logicalName, java.lang.String trailingName)
      Returns the class name for the given logical name and trailing name.
      static java.lang.String getClassNameForLowerCaseHyphenSeparatedName​(java.lang.String name)
      Converts foo-bar into FooBar.
      static java.lang.String getClassNameRepresentation​(java.lang.String name)
      Returns the class name representation of the given name
      static java.lang.String getFilename​(java.lang.String path)  
      static java.lang.String getFilename​(java.lang.String path, java.util.Collection<java.lang.String> extensions)  
      static java.lang.String getFilenameExtension​(java.lang.String path)  
      static java.lang.String getGetterName​(java.lang.String propertyName)
      Calculate the name for a getter method to retrieve the specified property
      static java.lang.String getHyphenatedName​(java.lang.Class<?> clazz)
      Retrieves the hyphenated name representation of the supplied class.
      static java.lang.String getHyphenatedName​(java.lang.String name)
      Retrieves the hyphenated name representation of the given class name.
      static java.lang.String getLogicalName​(java.lang.Class<?> clazz, java.lang.String trailingName)
      Retrieves the logical class name of a Griffon artifact given the Griffon class and a specified trailing name
      static java.lang.String getLogicalName​(java.lang.String name, java.lang.String trailingName)
      Retrieves the logical name of the class without the trailing name
      static java.lang.String getLogicalPropertyName​(java.lang.String className, java.lang.String trailingName)  
      static java.lang.String getNaturalName​(java.lang.String name)
      Converts a property name into its natural language equivalent eg ('firstName' becomes 'First Name')
      private static java.lang.String getPadding​(java.lang.String padding, int length)  
      static java.lang.String getPropertyName​(java.lang.Class<?> clazz)
      Shorter version of getPropertyNameRepresentation
      static java.lang.String getPropertyName​(java.lang.reflect.Method method)
      Returns the property name representation of the given Method
      static java.lang.String getPropertyName​(java.lang.String name)
      Shorter version of getPropertyNameRepresentation
      static java.lang.String getPropertyNameForLowerCaseHyphenSeparatedName​(java.lang.String name)
      Converts foo-bar into fooBar
      static java.lang.String getPropertyNameRepresentation​(java.lang.Class<?> targetClass)
      Returns the property name equivalent for the specified class
      static java.lang.String getPropertyNameRepresentation​(java.lang.String name)
      Returns the property name representation of the given name
      static java.lang.String getSetterName​(java.lang.String propertyName)
      Retrieves the name of a setter for the specified property name
      static java.lang.String getShortName​(java.lang.Class<?> targetClass)
      Returns the class name without the package prefix
      static java.lang.String getShortName​(java.lang.String className)
      Returns the class name without the package prefix
      static boolean isBlank​(java.lang.String str)
      Determines whether a given string is null, empty, or only contains whitespace.
      static boolean isFalse​(java.lang.Object o)  
      static boolean isNotBlank​(java.lang.String str)
      Determines whether a given string is not null, empty, or only contains whitespace.
      static boolean isTrue​(java.lang.Object o)  
      static java.lang.String normalizeRegexPattern​(java.lang.String str)  
      static java.lang.String padLeft​(java.lang.String str, int numChars)  
      static java.lang.String padLeft​(java.lang.String str, int numChars, java.lang.String padding)  
      static java.lang.String padRight​(java.lang.String str, int numChars)  
      static java.lang.String padRight​(java.lang.String str, int numChars, java.lang.String padding)  
      static java.lang.String quote​(java.lang.String str)
      Applies single or double quotes to a string if it contains whitespace characters
      static java.lang.String requireNonBlank​(java.lang.String str)
      Checks that the specified String is not blank.
      static java.lang.String requireNonBlank​(java.lang.String str, java.lang.String message)
      Checks that the specified String is not blank and throws a customized IllegalArgumentException if it is.
      static java.lang.Object splitValue​(java.lang.String value)  
      static java.lang.String stripMargin​(java.lang.String str)  
      static java.lang.String times​(java.lang.String str, int num)  
      static java.util.regex.Pattern toSafePattern​(java.lang.String str)  
      static java.lang.String toSafeRegexPattern​(java.lang.String str)  
      static java.lang.String uncapitalize​(java.lang.String str)
      Uncapitalizes a String (makes the first char lowercase) taking care of blank strings and single character strings.
      static java.lang.String unquote​(java.lang.String str)
      Removes single or double quotes from a String
      • Methods inherited from class java.lang.Object

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

      • PROPERTY_SET_PREFIX

        private static final java.lang.String PROPERTY_SET_PREFIX
        See Also:
        Constant Field Values
      • PROPERTY_GET_PREFIX

        private static final java.lang.String PROPERTY_GET_PREFIX
        See Also:
        Constant Field Values
      • GETTER_PATTERN_1

        private static final java.util.regex.Pattern GETTER_PATTERN_1
      • GETTER_PATTERN_2

        private static final java.util.regex.Pattern GETTER_PATTERN_2
      • SETTER_PATTERN

        private static final java.util.regex.Pattern SETTER_PATTERN
      • ERROR_METHOD_NULL

        private static final java.lang.String ERROR_METHOD_NULL
        See Also:
        Constant Field Values
      • REGEX_CHARS

        private static final java.util.regex.Pattern REGEX_CHARS
    • Constructor Detail

      • StringUtils

        public StringUtils()
    • Method Detail

      • capitalize

        public static java.lang.String capitalize​(java.lang.String str)
        Capitalizes a String (makes the first char uppercase) taking care of blank strings and single character strings.
        Parameters:
        str - The String to be capitalized
        Returns:
        Capitalized version of the target string if it is not blank
      • getFilenameExtension

        public static java.lang.String getFilenameExtension​(java.lang.String path)
      • getFilename

        public static java.lang.String getFilename​(java.lang.String path)
      • getFilename

        public static java.lang.String getFilename​(java.lang.String path,
                                                   java.util.Collection<java.lang.String> extensions)
      • getSetterName

        public static java.lang.String getSetterName​(java.lang.String propertyName)
        Retrieves the name of a setter for the specified property name
        Parameters:
        propertyName - The property name
        Returns:
        The setter equivalent
      • getGetterName

        public static java.lang.String getGetterName​(java.lang.String propertyName)
        Calculate the name for a getter method to retrieve the specified property
        Parameters:
        propertyName - The property name
        Returns:
        The name for the getter method for this property, if it were to exist, i.e. getConstraints
      • getClassName

        public static java.lang.String getClassName​(java.lang.String logicalName,
                                                    java.lang.String trailingName)
        Returns the class name for the given logical name and trailing name. For example "person" and "Controller" would evaluate to "PersonController"
        Parameters:
        logicalName - The logical name
        trailingName - The trailing name
        Returns:
        The class name
      • getClassNameRepresentation

        public static java.lang.String getClassNameRepresentation​(java.lang.String name)
        Returns the class name representation of the given name
        Parameters:
        name - The name to convert
        Returns:
        The property name representation
      • getClassNameForLowerCaseHyphenSeparatedName

        public static java.lang.String getClassNameForLowerCaseHyphenSeparatedName​(java.lang.String name)
        Converts foo-bar into FooBar. Empty and null strings are returned as-is.
        Parameters:
        name - The lower case hyphen separated name
        Returns:
        The class name equivalent.
      • getCapitalizedName

        public static java.lang.String getCapitalizedName​(java.lang.String name)
        Converts foo-bar into Foo Bar. Empty and null strings are returned as-is.
        Parameters:
        name - The lower case hyphen separated name
        Returns:
        The capitalized name equivalent.
      • getLogicalName

        public static java.lang.String getLogicalName​(java.lang.Class<?> clazz,
                                                      java.lang.String trailingName)
        Retrieves the logical class name of a Griffon artifact given the Griffon class and a specified trailing name
        Parameters:
        clazz - The class
        trailingName - The trailing name such as "Controller" or "TagLib"
        Returns:
        The logical class name
      • getLogicalName

        public static java.lang.String getLogicalName​(java.lang.String name,
                                                      java.lang.String trailingName)
        Retrieves the logical name of the class without the trailing name
        Parameters:
        name - The name of the class
        trailingName - The trailing name
        Returns:
        The logical name
      • getLogicalPropertyName

        public static java.lang.String getLogicalPropertyName​(java.lang.String className,
                                                              java.lang.String trailingName)
      • getPropertyName

        public static java.lang.String getPropertyName​(java.lang.String name)
        Shorter version of getPropertyNameRepresentation
        Parameters:
        name - The name to convert
        Returns:
        The property name version
      • getPropertyName

        public static java.lang.String getPropertyName​(java.lang.Class<?> clazz)
        Shorter version of getPropertyNameRepresentation
        Parameters:
        clazz - The clazz to convert
        Returns:
        The property name version
      • getPropertyName

        public static java.lang.String getPropertyName​(java.lang.reflect.Method method)
        Returns the property name representation of the given Method
        Parameters:
        method - The method to inspect
        Returns:
        The property name representation
        Since:
        3.0.0
      • getPropertyNameRepresentation

        public static java.lang.String getPropertyNameRepresentation​(java.lang.Class<?> targetClass)
        Returns the property name equivalent for the specified class
        Parameters:
        targetClass - The class to get the property name for
        Returns:
        A property name representation of the class name (eg. MyClass becomes myClass)
      • getPropertyNameRepresentation

        public static java.lang.String getPropertyNameRepresentation​(java.lang.String name)
        Returns the property name representation of the given name
        Parameters:
        name - The name to convert
        Returns:
        The property name representation
      • getPropertyNameForLowerCaseHyphenSeparatedName

        public static java.lang.String getPropertyNameForLowerCaseHyphenSeparatedName​(java.lang.String name)
        Converts foo-bar into fooBar
        Parameters:
        name - The lower case hyphen separated name
        Returns:
        The property name equivalent
      • getShortName

        public static java.lang.String getShortName​(java.lang.Class<?> targetClass)
        Returns the class name without the package prefix
        Parameters:
        targetClass - The class to get a short name for
        Returns:
        The short name of the class
      • getShortName

        public static java.lang.String getShortName​(java.lang.String className)
        Returns the class name without the package prefix
        Parameters:
        className - The class name to get a short name for
        Returns:
        The short name of the class
      • getNaturalName

        public static java.lang.String getNaturalName​(java.lang.String name)
        Converts a property name into its natural language equivalent eg ('firstName' becomes 'First Name')
        Parameters:
        name - The property name to convert
        Returns:
        The converted property name
      • isBlank

        public static boolean isBlank​(java.lang.String str)

        Determines whether a given string is null, empty, or only contains whitespace. If it contains anything other than whitespace then the string is not considered to be blank and the method returns false.

        Parameters:
        str - The string to test.
        Returns:
        true if the string is null, or blank.
      • isNotBlank

        public static boolean isNotBlank​(java.lang.String str)

        Determines whether a given string is not null, empty, or only contains whitespace. If it contains anything other than whitespace then the string is not considered to be blank and the method returns true.

        Parameters:
        str - The string to test.
        Returns:
        true if the string is not null, nor blank.
      • requireNonBlank

        public static java.lang.String requireNonBlank​(java.lang.String str)
        Checks that the specified String is not blank. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
          Foo(String str) {*     this.str = GriffonNameUtils.requireNonBlank(str)
         }* 
        Parameters:
        str - the String to check for blank
        Returns:
        str if not blank
        Throws:
        java.lang.IllegalArgumentException - if str is blank
      • requireNonBlank

        public static java.lang.String requireNonBlank​(java.lang.String str,
                                                       java.lang.String message)
        Checks that the specified String is not blank and throws a customized IllegalArgumentException if it is. This method is designed primarily for doing parameter validation in methods and constructors with multiple parameters, as demonstrated below:
          Foo(String str) {*     this.str = GriffonNameUtils.requireNonBlank(str, "str must not be null")
         }* 
        Parameters:
        str - the String to check for blank
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        str if not blank
        Throws:
        java.lang.IllegalArgumentException - if str is blank
      • getHyphenatedName

        public static java.lang.String getHyphenatedName​(java.lang.Class<?> clazz)
        Retrieves the hyphenated name representation of the supplied class. For example MyFunkyGriffonThingy would be my-funky-griffon-thingy.
        Parameters:
        clazz - The class to convert
        Returns:
        The hyphenated name representation
      • getHyphenatedName

        public static java.lang.String getHyphenatedName​(java.lang.String name)
        Retrieves the hyphenated name representation of the given class name. For example MyFunkyGriffonThingy would be my-funky-griffon-thingy.
        Parameters:
        name - The class name to convert.
        Returns:
        The hyphenated name representation.
      • uncapitalize

        public static java.lang.String uncapitalize​(java.lang.String str)
        Uncapitalizes a String (makes the first char lowercase) taking care of blank strings and single character strings.
        Parameters:
        str - The String to be uncapitalized
        Returns:
        Uncapitalized version of the target string if it is not blank
      • splitValue

        public static java.lang.Object splitValue​(java.lang.String value)
      • escape

        private static java.lang.String escape​(char character)
      • padLeft

        public static java.lang.String padLeft​(java.lang.String str,
                                               int numChars)
      • padLeft

        public static java.lang.String padLeft​(java.lang.String str,
                                               int numChars,
                                               java.lang.String padding)
      • padRight

        public static java.lang.String padRight​(java.lang.String str,
                                                int numChars)
      • padRight

        public static java.lang.String padRight​(java.lang.String str,
                                                int numChars,
                                                java.lang.String padding)
      • getPadding

        private static java.lang.String getPadding​(java.lang.String padding,
                                                   int length)
      • times

        public static java.lang.String times​(java.lang.String str,
                                             int num)
      • stripMargin

        public static java.lang.String stripMargin​(java.lang.String str)
      • escapeRegexChars

        public static java.lang.String escapeRegexChars​(java.lang.String str)
      • toSafeRegexPattern

        public static java.lang.String toSafeRegexPattern​(java.lang.String str)
      • normalizeRegexPattern

        public static java.lang.String normalizeRegexPattern​(java.lang.String str)
      • toSafePattern

        public static java.util.regex.Pattern toSafePattern​(java.lang.String str)
      • isTrue

        public static boolean isTrue​(java.lang.Object o)
      • isFalse

        public static boolean isFalse​(java.lang.Object o)
      • quote

        public static java.lang.String quote​(java.lang.String str)
        Applies single or double quotes to a string if it contains whitespace characters
        Parameters:
        str - the String to be surrounded by quotes
        Returns:
        a copy of the original String, surrounded by quotes
      • unquote

        public static java.lang.String unquote​(java.lang.String str)
        Removes single or double quotes from a String
        Parameters:
        str - the String from which quotes will be removed
        Returns:
        the unquoted String
      • applyQuotes

        private static java.lang.String applyQuotes​(java.lang.String string)