Class NameUtils

java.lang.Object
io.micronaut.core.naming.NameUtils

public class NameUtils extends Object

Naming convention utilities.

Since:
1.0
Author:
Graeme Rocher
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static @NonNull String
    camelCase(@NonNull String str)
    The camel case version of the string with the first letter in lower case.
    static @NonNull String
    camelCase(@NonNull String str, boolean lowerCaseFirstLetter)
    The camel case version of the string with the first letter in lower case.
    static @NonNull String
    capitalize(@NonNull String name)
    Converts a property name to class name according to the JavaBean convention.
    static @Nullable String
    decapitalize(@Nullable 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 @NonNull String
    decapitalizeWithoutSuffix(@NonNull String name, String... suffixes)
    Converts class name to property name using JavaBean decapitalization.
    static @NonNull String
    dehyphenate(@NonNull String name)
    Converts hyphenated, lower-case form to camel-case form.
    static @NonNull String
    environmentName(@NonNull String camelCase)
    Returns the underscore separated version of the given camel case string.
    static @NonNull String
    extension(@NonNull String filename)
    Retrieves the extension of a file name.
    static @NonNull String
    filename(@NonNull String path)
    Retrieves the fileName of a file without extension.
    static @NonNull String
    getPackageName(@NonNull String className)
    Returns the package name for a class represented as string.
    static @NonNull String
    getPropertyNameForGetter(@NonNull String getterName)
    Get the equivalent property name for the given getter.
    static @NonNull String
    getPropertyNameForGetter(@NonNull String getterName, @NonNull String readPrefix)
    Get the equivalent property name for the given getter and read prefix.
    static @NonNull String
    getPropertyNameForGetter(@NonNull String getterName, @NonNull String[] readPrefixes)
    Get the equivalent property name for the given getter and read prefixes.
    static @NonNull String
    getPropertyNameForSetter(@NonNull String setterName)
    Get the equivalent property name for the given setter.
    static @NonNull String
    getPropertyNameForSetter(@NonNull String setterName, @NonNull String writePrefix)
    Get the equivalent property name for the given setter and write prefix.
    static @NonNull String
    getPropertyNameForSetter(@NonNull String setterName, @NonNull String[] writePrefixes)
    Get the equivalent property name for the given setter and write prefixes.
    static @NonNull String
    getShortenedName(@NonNull String typeName)
    Returns the shortened fully-qualified name for a class represented as a string.
    static @NonNull String
    getSimpleName(@NonNull String className)
    Returns the simple name for a class represented as string.
    static @NonNull String
    getterNameFor(@NonNull String propertyName)
    Get the equivalent getter name for the given property.
    static String
    getterNameFor(@NonNull String propertyName, boolean isBoolean)
    Get the equivalent getter name for the given property.
    static @NonNull String
    getterNameFor(@NonNull String propertyName, @NonNull Class<?> type)
    Get the equivalent getter name for the given property.
    static @NonNull String
    getterNameFor(@NonNull String propertyName, @NonNull String prefix)
    Get the equivalent getter name for the given property and a prefix.
    static @NonNull String
    getterNameFor(@NonNull String propertyName, @NonNull String[] prefixes)
    Get the equivalent getter name for the given property and the first prefix.
    static @NonNull String
    hyphenate(@NonNull String name)
    Converts camel case to hyphenated, lowercase form.
    static @NonNull String
    hyphenate(@NonNull String name, boolean lowerCase)
    Converts camel case to hyphenated, lowercase form.
    static boolean
    isEnvironmentName(@NonNull String str)
    Checks whether the string is a valid environment-style property name.
    static boolean
    isGetterName(@NonNull String methodName)
    Is the given method name a valid getter name.
    static boolean
    isHyphenatedLowerCase(@Nullable String name)
    Checks whether the given name is a valid service identifier.
    static boolean
    isReaderName(@NonNull String methodName, @NonNull String readPrefix)
    Is the given method name a valid reader name.
    static boolean
    isReaderName(@NonNull String methodName, @NonNull String[] readPrefixes)
    Is the given method name a valid reader name.
    static boolean
    isSetterName(@NonNull String methodName)
    Is the given method name a valid setter name.
    static boolean
    Checks whether the string is a valid hyphenated (kebab-case) property name.
    static boolean
    isWriterName(@NonNull String methodName, @NonNull String writePrefix)
    Is the given method name a valid writer name for the prefix.
    static boolean
    isWriterName(@NonNull String methodName, @NonNull String[] writePrefixes)
    Is the given method name a valid writer name for any of the prefixes.
    static @NonNull String
    setterNameFor(@NonNull String propertyName)
    Get the equivalent setter name for the given property.
    static @NonNull String
    setterNameFor(@NonNull String propertyName, @NonNull String prefix)
    Get the equivalent setter name for the given property and a prefix.
    static @NonNull String
    setterNameFor(@NonNull String propertyName, @NonNull String[] prefixes)
    Get the equivalent setter name for the given property and the first prefix.
    static @NonNull String
    trimSuffix(@NonNull String string, String... suffixes)
    Trims the given suffixes.
    static @NonNull String
    underscoreSeparate(@NonNull String camelCase)
    Returns the underscore separated version of the given camel case string.
    static @NonNull String
    underscoreSeparate(@NonNull String camelCase, boolean lowercase)
    Returns the underscore separated version of the given camel case string, optionally with lowercase result.

    Methods inherited from class java.lang.Object

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

    • NameUtils

      public NameUtils()
  • Method Details

    • isHyphenatedLowerCase

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

      public static @NonNull String decapitalizeWithoutSuffix(@NonNull String name, 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 @NonNull String trimSuffix(@NonNull String string, String... suffixes)
      Trims the given suffixes.
      Parameters:
      string - The string to trim
      suffixes - The suffixes
      Returns:
      The trimmed string
    • capitalize

      public static @NonNull String capitalize(@NonNull 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 @NonNull String hyphenate(@NonNull String name)
      Converts camel case to hyphenated, lowercase form.
      Parameters:
      name - The name
      Returns:
      The hyphenated string
    • hyphenate

      public static @NonNull String hyphenate(@NonNull 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 @NonNull String dehyphenate(@NonNull String name)
      Converts hyphenated, lower-case form to camel-case form.
      Parameters:
      name - The hyphenated string
      Returns:
      The camel case form
    • getPackageName

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

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

      public static @NonNull String underscoreSeparate(@NonNull String camelCase, boolean lowercase)
      Returns the underscore separated version of the given camel case string, optionally with lowercase result.
      Parameters:
      camelCase - The camel case name
      lowercase - true to lowercase the result
      Returns:
      The underscore separated version
    • environmentName

      public static @NonNull String environmentName(@NonNull 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 @NonNull String getSimpleName(@NonNull String className)
      Returns the simple name for a class represented as string.
      Parameters:
      className - The class name
      Returns:
      The simple name of the class
    • getShortenedName

      public static @NonNull String getShortenedName(@NonNull String typeName)
      Returns the shortened fully-qualified name for a class represented as a string. Shortened name would have package names and owner objects reduced to a single letter. For example, com.example.Owner$Inner would become c.e.O$Inner. IDEs would still be able to recognize these types, but they would take less space visually.
      Parameters:
      typeName - The fully-qualified type name
      Returns:
      The shortened type name
      Since:
      4.8.x
    • isSetterName

      public static boolean isSetterName(@NonNull 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 String methodName, @NonNull 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 String methodName, @NonNull 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 @NonNull String getPropertyNameForSetter(@NonNull String setterName)
      Get the equivalent property name for the given setter.
      Parameters:
      setterName - The setter
      Returns:
      The property name
    • getPropertyNameForSetter

      public static @NonNull String getPropertyNameForSetter(@NonNull String setterName, @NonNull 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

      public static @NonNull String getPropertyNameForSetter(@NonNull String setterName, @NonNull 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

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

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

      public static @NonNull String setterNameFor(@NonNull String propertyName, @NonNull 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(@NonNull 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 String methodName, @NonNull 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 String methodName, @NonNull 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 @NonNull String getPropertyNameForGetter(@NonNull String getterName)
      Get the equivalent property name for the given getter.
      Parameters:
      getterName - The getter
      Returns:
      The property name
    • getPropertyNameForGetter

      public static @NonNull String getPropertyNameForGetter(@NonNull String getterName, @NonNull 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

      public static @NonNull String getPropertyNameForGetter(@NonNull String getterName, @NonNull 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

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

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

      public static @NonNull String getterNameFor(@NonNull String propertyName, @NonNull 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

      public static @NonNull String getterNameFor(@NonNull String propertyName, @NonNull 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 String getterNameFor(@NonNull 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 @Nullable String decapitalize(@Nullable 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 @NonNull String extension(@NonNull 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 @NonNull String camelCase(@NonNull 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 @NonNull String camelCase(@NonNull 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 @NonNull String filename(@NonNull 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(@NonNull 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(@NonNull 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