Class NameUtils


  • public class NameUtils
    extends java.lang.Object

    Naming convention utilities.

    Since:
    1.0
    • Constructor Summary

      Constructors 
      Constructor Description
      NameUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String camelCase​(java.lang.String str)
      The camel case version of the string with the first letter in lower case.
      static java.lang.String camelCase​(java.lang.String str, boolean lowerCaseFirstLetter)
      The camel case version of the string with the first letter in lower case.
      static java.lang.String capitalize​(java.lang.String name)
      Converts a property name to class name according to the JavaBean convention.
      static java.lang.String decapitalize​(java.lang.String name)
      Decapitalizes a given string according to the rule: If the first or only character is Upper Case, it is made Lower Case UNLESS the second character is also Upper Case, when the String is returned unchanged.
      static java.lang.String decapitalizeWithoutSuffix​(java.lang.String name, java.lang.String... suffixes)
      Converts class name to property name using JavaBean decapitalization.
      static java.lang.String dehyphenate​(java.lang.String name)
      Converts hyphenated, lower-case form to camel-case form.
      static java.lang.String environmentName​(java.lang.String camelCase)
      Returns the underscore separated version of the given camel case string.
      static java.lang.String extension​(java.lang.String filename)
      Retrieves the extension of a file name.
      static java.lang.String filename​(java.lang.String path)
      Retrieves the fileName of a file without extension.
      static java.lang.String getPackageName​(java.lang.String className)
      Returns the package name for a class represented as string.
      static java.lang.String getPropertyNameForGetter​(java.lang.String getterName)
      Get the equivalent property name for the given getter.
      static java.lang.String getPropertyNameForGetter​(java.lang.String getterName, java.lang.String readPrefix)
      Get the equivalent property name for the given getter and read prefix.
      static java.lang.String getPropertyNameForGetter​(java.lang.String getterName, java.lang.String[] readPrefixes)
      Get the equivalent property name for the given getter and read prefixes.
      static java.lang.String getPropertyNameForSetter​(java.lang.String setterName)
      Get the equivalent property name for the given setter.
      static java.lang.String getPropertyNameForSetter​(java.lang.String setterName, java.lang.String writePrefix)
      Get the equivalent property name for the given setter and write prefix.
      static java.lang.String getPropertyNameForSetter​(java.lang.String setterName, java.lang.String[] writePrefixes)
      Get the equivalent property name for the given setter and write prefixes.
      static java.lang.String getSimpleName​(java.lang.String className)
      Returns the simple name for a class represented as string.
      static java.lang.String getterNameFor​(java.lang.String propertyName)
      Get the equivalent getter name for the given property.
      static java.lang.String getterNameFor​(java.lang.String propertyName, boolean isBoolean)
      Get the equivalent getter name for the given property.
      static java.lang.String getterNameFor​(java.lang.String propertyName, java.lang.Class<?> type)
      Get the equivalent getter name for the given property.
      static java.lang.String getterNameFor​(java.lang.String propertyName, java.lang.String prefix)
      Get the equivalent getter name for the given property and a prefix.
      static java.lang.String getterNameFor​(java.lang.String propertyName, java.lang.String[] prefixes)
      Get the equivalent getter name for the given property and a the first prefix.
      static java.lang.String hyphenate​(java.lang.String name)
      Converts camel case to hyphenated, lowercase form.
      static java.lang.String hyphenate​(java.lang.String name, boolean lowerCase)
      Converts camel case to hyphenated, lowercase form.
      static boolean isEnvironmentName​(java.lang.String str)
      Checks whether the string is a valid environment-style property name.
      static boolean isGetterName​(java.lang.String methodName)
      Is the given method name a valid getter name.
      static boolean isHyphenatedLowerCase​(java.lang.String name)
      Checks whether the given name is a valid service identifier.
      static boolean isReaderName​(java.lang.String methodName, java.lang.String readPrefix)
      Is the given method name a valid reader name.
      static boolean isReaderName​(java.lang.String methodName, java.lang.String[] readPrefixes)
      Is the given method name a valid reader name.
      static boolean isSetterName​(java.lang.String methodName)
      Is the given method name a valid setter name.
      static boolean isValidHyphenatedPropertyName​(java.lang.String str)
      Checks whether the string is a valid hyphenated (kebab-case) property name.
      static boolean isWriterName​(java.lang.String methodName, java.lang.String writePrefix)
      Is the given method name a valid writer name for the prefix.
      static boolean isWriterName​(java.lang.String methodName, java.lang.String[] writePrefixes)
      Is the given method name a valid writer name for any of the prefixes.
      static java.lang.String setterNameFor​(java.lang.String propertyName)
      Get the equivalent setter name for the given property.
      static java.lang.String setterNameFor​(java.lang.String propertyName, java.lang.String prefix)
      Get the equivalent setter name for the given property and a prefix.
      static java.lang.String setterNameFor​(java.lang.String propertyName, java.lang.String[] prefixes)
      Get the equivalent setter name for the given property and a the first prefix.
      static java.lang.String trimSuffix​(java.lang.String string, java.lang.String... suffixes)
      Trims the given suffixes.
      static java.lang.String underscoreSeparate​(java.lang.String camelCase)
      Returns the underscore separated version of the given camel case string.
      • Methods inherited from class java.lang.Object

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

      • NameUtils

        public NameUtils()
    • Method Detail

      • isHyphenatedLowerCase

        public static boolean isHyphenatedLowerCase​(java.lang.String name)
        Checks whether the given name is a valid service identifier.
        Parameters:
        name - The name
        Returns:
        True if it is
      • decapitalizeWithoutSuffix

        public static java.lang.String decapitalizeWithoutSuffix​(java.lang.String name,
                                                                 java.lang.String... suffixes)
        Converts class name to property name using JavaBean decapitalization.
        Parameters:
        name - The class name
        suffixes - The suffix to remove
        Returns:
        The decapitalized name
      • trimSuffix

        public static java.lang.String trimSuffix​(java.lang.String string,
                                                  java.lang.String... suffixes)
        Trims the given suffixes.
        Parameters:
        string - The string to trim
        suffixes - The suffixes
        Returns:
        The trimmed string
      • capitalize

        public static java.lang.String capitalize​(java.lang.String name)
        Converts a property name to class name according to the JavaBean convention.
        Parameters:
        name - The property name
        Returns:
        The class name
      • hyphenate

        public static java.lang.String hyphenate​(java.lang.String name)
        Converts camel case to hyphenated, lowercase form.
        Parameters:
        name - The name
        Returns:
        The hyphenated string
      • hyphenate

        public static java.lang.String hyphenate​(java.lang.String name,
                                                 boolean lowerCase)
        Converts camel case to hyphenated, lowercase form.
        Parameters:
        name - The name
        lowerCase - Whether the result should be converted to lower case
        Returns:
        The hyphenated string
      • dehyphenate

        public static java.lang.String dehyphenate​(java.lang.String name)
        Converts hyphenated, lower-case form to camel-case form.
        Parameters:
        name - The hyphenated string
        Returns:
        The camel case form
      • getPackageName

        public static java.lang.String getPackageName​(java.lang.String className)
        Returns the package name for a class represented as string.
        Parameters:
        className - The class name
        Returns:
        The package name
      • underscoreSeparate

        public static java.lang.String underscoreSeparate​(java.lang.String camelCase)
        Returns the underscore separated version of the given camel case string.
        Parameters:
        camelCase - The camel case name
        Returns:
        The underscore separated version
      • environmentName

        public static java.lang.String environmentName​(java.lang.String camelCase)
        Returns the underscore separated version of the given camel case string.
        Parameters:
        camelCase - The camel case name
        Returns:
        The underscore separated version
      • getSimpleName

        public static java.lang.String getSimpleName​(java.lang.String className)
        Returns the simple name for a class represented as string.
        Parameters:
        className - The class name
        Returns:
        The simple name of the class
      • isSetterName

        public static boolean isSetterName​(java.lang.String methodName)
        Is the given method name a valid setter name.
        Parameters:
        methodName - The method name
        Returns:
        True if it is a valid setter name
      • isWriterName

        public static boolean isWriterName​(@NonNull
                                           java.lang.String methodName,
                                           @NonNull
                                           java.lang.String writePrefix)
        Is the given method name a valid writer name for the prefix.
        Parameters:
        methodName - The method name
        writePrefix - The write prefix
        Returns:
        True if it is a valid writer name
        Since:
        3.3.0
      • isWriterName

        public static boolean isWriterName​(@NonNull
                                           java.lang.String methodName,
                                           @NonNull
                                           java.lang.String[] writePrefixes)
        Is the given method name a valid writer name for any of the prefixes.
        Parameters:
        methodName - The method name
        writePrefixes - The write prefixes
        Returns:
        True if it is a valid writer name
        Since:
        3.3.0
      • getPropertyNameForSetter

        public static java.lang.String getPropertyNameForSetter​(java.lang.String setterName)
        Get the equivalent property name for the given setter.
        Parameters:
        setterName - The setter
        Returns:
        The property name
      • getPropertyNameForSetter

        @NonNull
        public static java.lang.String getPropertyNameForSetter​(@NonNull
                                                                java.lang.String setterName,
                                                                @NonNull
                                                                java.lang.String writePrefix)
        Get the equivalent property name for the given setter and write prefix.
        Parameters:
        setterName - The setter name
        writePrefix - The write prefix
        Returns:
        The property name
        Since:
        3.3.0
      • getPropertyNameForSetter

        @NonNull
        public static java.lang.String getPropertyNameForSetter​(@NonNull
                                                                java.lang.String setterName,
                                                                @NonNull
                                                                java.lang.String[] writePrefixes)
        Get the equivalent property name for the given setter and write prefixes.
        Parameters:
        setterName - The setter name
        writePrefixes - The write prefixes
        Returns:
        The property name
        Since:
        3.3.0
      • setterNameFor

        @NonNull
        public static java.lang.String setterNameFor​(@NonNull
                                                     java.lang.String propertyName)
        Get the equivalent setter name for the given property.
        Parameters:
        propertyName - The property name
        Returns:
        The setter name
      • setterNameFor

        @NonNull
        public static java.lang.String setterNameFor​(@NonNull
                                                     java.lang.String propertyName,
                                                     @NonNull
                                                     java.lang.String[] prefixes)
        Get the equivalent setter name for the given property and a the first prefix.
        Parameters:
        propertyName - The property name
        prefixes - The prefixes
        Returns:
        The setter name for the first prefix
        Since:
        3.3.0
      • setterNameFor

        @NonNull
        public static java.lang.String setterNameFor​(@NonNull
                                                     java.lang.String propertyName,
                                                     @NonNull
                                                     java.lang.String prefix)
        Get the equivalent setter name for the given property and a prefix.
        Parameters:
        propertyName - The property name
        prefix - The prefix
        Returns:
        The setter name
        Since:
        3.3.0
      • isGetterName

        public static boolean isGetterName​(java.lang.String methodName)
        Is the given method name a valid getter name.
        Parameters:
        methodName - The method name
        Returns:
        True if it is a valid getter name
      • isReaderName

        public static boolean isReaderName​(@NonNull
                                           java.lang.String methodName,
                                           @NonNull
                                           java.lang.String readPrefix)
        Is the given method name a valid reader name.
        Parameters:
        methodName - The method name
        readPrefix - The read prefix
        Returns:
        True if it is a valid read name
        Since:
        3.3.0
      • isReaderName

        public static boolean isReaderName​(@NonNull
                                           java.lang.String methodName,
                                           @NonNull
                                           java.lang.String[] readPrefixes)
        Is the given method name a valid reader name.
        Parameters:
        methodName - The method name
        readPrefixes - The valid read prefixes
        Returns:
        True if it is a valid reader name
        Since:
        3.3.0
      • getPropertyNameForGetter

        public static java.lang.String getPropertyNameForGetter​(java.lang.String getterName)
        Get the equivalent property name for the given getter.
        Parameters:
        getterName - The getter
        Returns:
        The property name
      • getPropertyNameForGetter

        @NonNull
        public static java.lang.String getPropertyNameForGetter​(@NonNull
                                                                java.lang.String getterName,
                                                                @NonNull
                                                                java.lang.String readPrefix)
        Get the equivalent property name for the given getter and read prefix.
        Parameters:
        getterName - The getter
        readPrefix - The read prefix
        Returns:
        The property name
        Since:
        3.3.0
      • getPropertyNameForGetter

        @NonNull
        public static java.lang.String getPropertyNameForGetter​(@NonNull
                                                                java.lang.String getterName,
                                                                @NonNull
                                                                java.lang.String[] readPrefixes)
        Get the equivalent property name for the given getter and read prefixes.
        Parameters:
        getterName - The getter
        readPrefixes - The read prefixes
        Returns:
        The property name
        Since:
        3.3.0
      • getterNameFor

        @NonNull
        public static java.lang.String getterNameFor​(@NonNull
                                                     java.lang.String propertyName)
        Get the equivalent getter name for the given property.
        Parameters:
        propertyName - The property name
        Returns:
        The getter name
      • getterNameFor

        @NonNull
        public static java.lang.String getterNameFor​(@NonNull
                                                     java.lang.String propertyName,
                                                     @NonNull
                                                     java.lang.String[] prefixes)
        Get the equivalent getter name for the given property and a the first prefix.
        Parameters:
        propertyName - The property name
        prefixes - The prefixes
        Returns:
        The getter name for the first prefix
        Since:
        3.3.0
      • getterNameFor

        @NonNull
        public static java.lang.String getterNameFor​(@NonNull
                                                     java.lang.String propertyName,
                                                     @NonNull
                                                     java.lang.String prefix)
        Get the equivalent getter name for the given property and a prefix.
        Parameters:
        propertyName - The property name
        prefix - The prefix
        Returns:
        The getter name for the prefix
        Since:
        3.3.0
      • getterNameFor

        @NonNull
        public static java.lang.String getterNameFor​(@NonNull
                                                     java.lang.String propertyName,
                                                     @NonNull
                                                     java.lang.Class<?> type)
        Get the equivalent getter name for the given property.
        Parameters:
        propertyName - The property name
        type - The type of the property
        Returns:
        The getter name
      • getterNameFor

        public static java.lang.String getterNameFor​(@NonNull
                                                     java.lang.String propertyName,
                                                     boolean isBoolean)
        Get the equivalent getter name for the given property.
        Parameters:
        propertyName - The property name
        isBoolean - Is the property a boolean
        Returns:
        The getter name
      • decapitalize

        public static java.lang.String decapitalize​(java.lang.String name)
        Decapitalizes a given string according to the rule:
        • If the first or only character is Upper Case, it is made Lower Case
        • UNLESS the second character is also Upper Case, when the String is returned unchanged.
        Parameters:
        name - The String to decapitalize
        Returns:
        The decapitalized version of the String
      • extension

        public static java.lang.String extension​(java.lang.String filename)
        Retrieves the extension of a file name. Ex: index.html -> html
        Parameters:
        filename - The name of the file
        Returns:
        The file extension
      • camelCase

        public static java.lang.String camelCase​(java.lang.String str)
        The camel case version of the string with the first letter in lower case.
        Parameters:
        str - The string
        Returns:
        The new string in camel case
      • camelCase

        public static java.lang.String camelCase​(java.lang.String str,
                                                 boolean lowerCaseFirstLetter)
        The camel case version of the string with the first letter in lower case.
        Parameters:
        str - The string
        lowerCaseFirstLetter - Whether the first letter is in upper case or lower case
        Returns:
        The new string in camel case
      • filename

        public static java.lang.String filename​(java.lang.String path)
        Retrieves the fileName of a file without extension. Ex: index.html -> index
        Parameters:
        path - The path of the file
        Returns:
        The file name without extension
      • isValidHyphenatedPropertyName

        public static boolean isValidHyphenatedPropertyName​(java.lang.String str)
        Checks whether the string is a valid hyphenated (kebab-case) property name.
        Parameters:
        str - The string to check
        Returns:
        Whether is valid kebab-case or not
      • isEnvironmentName

        public static boolean isEnvironmentName​(java.lang.String str)
        Checks whether the string is a valid environment-style property name.
        Parameters:
        str - The string to check
        Returns:
        Whether is valid environment-style property name or not