Class CaseBreaker

java.lang.Object
com.google.mu.util.CaseBreaker

@CheckReturnValue @RequiresGuava public final class CaseBreaker extends Object
Utility to break and convert input strings (normally identifier strings) in camelCase, UpperCamelCase, snake_case, UPPER_SNAKE_CASE and dash-case etc.

Unlike CaseFormat, this class doesn't require you to know the input casing. You can take any string and then extract or convert into the target casing.

Warning: This class doesn't recognize supplementary code points.

Since:
6.0
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns a lazy stream of words split out from text, delimited by non-letter-digit ascii characters, and further split at lowerCamelCase and UpperCamelCase boundaries.
    static String
    toCase(com.google.common.base.CaseFormat caseFormat, String input)
    Converts input string to using the given CaseFormat.
    withLowerCaseChars(com.google.common.base.CharMatcher camelLower)
    Returns a new instance using camelLower to identify lower case characters (don't forget to include digits if they should also be treated as lower case).
    withPunctuationChars(com.google.common.base.CharMatcher punctuation)
    Returns a new instance using punctuation to identify punctuation characters (ones that separate words but aren't themselves included in the result), for example if you want to support dash-case using the en dash (–) character.

    Methods inherited from class java.lang.Object

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

    • CaseBreaker

      public CaseBreaker()
  • Method Details

    • withPunctuationChars

      public CaseBreaker withPunctuationChars(com.google.common.base.CharMatcher punctuation)
      Returns a new instance using punctuation to identify punctuation characters (ones that separate words but aren't themselves included in the result), for example if you want to support dash-case using the en dash (–) character.
      Since:
      7.0
    • withLowerCaseChars

      public CaseBreaker withLowerCaseChars(com.google.common.base.CharMatcher camelLower)
      Returns a new instance using camelLower to identify lower case characters (don't forget to include digits if they should also be treated as lower case).
    • breakCase

      public Stream<String> breakCase(CharSequence text)
      Returns a lazy stream of words split out from text, delimited by non-letter-digit ascii characters, and further split at lowerCamelCase and UpperCamelCase boundaries.

      Examples:

      
       breakCase("userId")            => ["user", "Id"]
       breakCase("field_name")        => ["field", "name"]
       breakCase("CONSTANT_NAME")     => ["CONSTANT", "NAME"]
       breakCase("dash-case")         => ["dash", "case"]
       breakCase("3 separate words")  => ["3", "separate", "words"]
       breakCase("TheURLs")           => ["The", "URLs"]
       breakCase("🅣ⓗⓔ🅤🅡🅛ⓢ")      => ["🅣ⓗⓔ", "🅤🅡🅛ⓢ""]
       breakCase("UpgradeIPv4ToIPv6") => ["Upgrade", "IPv4", "To", "IPv6"]
       

      By default, non-alphanumeric ascii characters are treated as case delimiter characters. And Java lower case characters and ascii digits are considered to be lower case when breaking up camel case.

      Besides used as case delimiters, non-letter-digit ascii characters are filtered out from the returned words.

      If the default setting doesn't work for you, it can be customized by using

      invalid reference
      #withCaseDelimiterChars
      and/or withLowerCaseChars(com.google.common.base.CharMatcher).
    • toCase

      public static String toCase(com.google.common.base.CaseFormat caseFormat, String input)
      Converts input string to using the given CaseFormat. input can be in snake_case, lowerCamelCase, UpperCamelCase, CONSTANT_CASE, dash-case or any combination thereof. For example:
      
       toCase(LOWER_CAMEL, "user_id")                 => "userId"
       toCase(LOWER_HYPHEN, "UserID")                 => "user-id"
       toCase(UPPER_UNDERSCORE, "orderId")            => "ORDER_ID"
       toCase(LOWER_UNDERSCORE, "primaryUser.userId") => "primary_user.user_id"
       

      Given that CaseFormat only handles ascii, characters outside of the range of [a-zA-Z0-9_-] (e.g. whitespaces, parenthesis, non-ascii) are passed through as is. If you need to support non-ascii camel case such as Greek upper case ('Β') and lower case ('β'), consider using breakCase(java.lang.CharSequence) to break up words in the source and then apply target casing manually using e.g. Character.toLowerCase(char).