Guava – CaseFormat Class

  • Post author:
  • Post category:Guava
  • Post comments:0 Comments
CaseFormat

CaseFormat is a utility class to provide conversion between various ASCII char formats.

Class Declaration

Following is the declaration for com.google.common.base.CaseFormat class −

@GwtCompatible
public enum CaseFormat
   extends Enum<CaseFormat>

Enum Constants

Sr.NoEnum Constant & Description
1LOWER_CAMELJava variable naming convention, e.g., “lowerCamel”.
2LOWER_HYPHENHyphenated variable naming convention, e.g., “lower-hyphen”.
3LOWER_UNDERSCOREC++ variable naming convention, e.g., “lower_underscore”.
4UPPER_CAMELJava and C++ class naming convention, e.g., “UpperCamel”.
5UPPER_UNDERSCOREJava and C++ constant naming convention, e.g., “UPPER_UNDERSCORE”.

Methods

Sr.NoMethod & Description
1Converter<String,String> converterTo(CaseFormat targetFormat)Returns a Converter that converts strings from this format to targetFormat.
2String to(CaseFormat format, String str)Converts the specified String str from this format to the specified format.
3static CaseFormat valueOf(String name)Returns the enum constant of this type with the specified name.
4static CaseFormat[] values()Returns an array containing the constants of this enum type, in the order they are declared.

Methods Inherited

This class inherits methods from the following classes −

  • java.lang.Enum
  • java.lang.Object

Example of CaseFormat Class

Create the following java program using any editor of your choice in say C:/> Guava.

GuavaTester.java

import com.google.common.base.CaseFormat;

public class GuavaTester {
   public static void main(String args[]) {
      GuavaTester tester = new GuavaTester();
      tester.testCaseFormat();
   }

   private void testCaseFormat() {
      String data = "test_data";
      System.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, "test-data"));
      System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "test_data"));
      System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "test_data"));
   }
}

Verify the Result

Compile the class using javac compiler as follows −

C:\Guava>javac GuavaTester.java

Now run the GuavaTester to see the result.

C:\Guava>java GuavaTester

See the result.

testData
testData
TestData

Previous Page:-Click Here

Leave a Reply