Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

CaseFormat Class ใน Java (Guava Library)

CaseFormat เป็น utility class ใน Google Guava Library (com.google.common.base) ใช้สำหรับแปลงรูปแบบการเขียนชื่อตัวแปร/คลาสระหว่างรูปแบบต่างๆ (ASCII case formats)

รูปแบบที่รองรับ (5 formats)

Constant รูปแบบ ตัวอย่าง
LOWER_CAMEL lowerCamelCase myVariableName, userName
UPPER_CAMEL UpperCamelCase (PascalCase) MyClassName, UserName
LOWER_UNDERSCORE lower_case_with_underscores my_variable_name, user_name
UPPER_UNDERSCORE UPPER_CASE_WITH_UNDERSCORES MY_CONSTANT_NAME, USER_NAME
LOWER_HYPHEN lower-case-with-hyphens my-variable-name, user-name

เมธอดหลัก

เมธอด คำอธิบาย
String to(CaseFormat targetFormat, String source) แปลง source จาก format ปัจจุบันไป target format
String to(CaseFormat targetFormat, String source, char delimiter) แปลงด้วย delimiter ที่กำหนด (สำหรับ hyphen/underscore)

ตัวอย่างการใช้งาน

import com.google.common.base.CaseFormat;

public class GuavaTester {
    public static void main(String[] args) {
        String data = "test_data";

        // LOWER_UNDERSCORE -> LOWER_CAMEL
        System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "test_data"));
        // Output: testData

        // LOWER_HYPHEN -> LOWER_CAMEL
        System.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, "test-data"));
        // Output: testData

        // UPPER_UNDERSCORE -> UPPER_CAMEL
        System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "test_data"));
        // Output: TestData

        // Cross conversions
        System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "myVariableName"));
        // Output: my_variable_name

        System.out.println(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, "MyClassName"));
        // Output: my-class-name

        System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_UNDERSCORE, "database_url"));
        // Output: DATABASE_URL
    }
}

Output

testData
testData
TestData
my_variable_name
my-class-name
DATABASE_URL

กรณีใช้งานทั่วไป

  • Code generation: แปลง database column names (snake_case) → Java fields (camelCase)
  • Configuration mapping: YAML/JSON keys (kebab-case) → Java properties
  • API integration: Convert between naming conventions ของระบบต่างๆ
  • Constant generation: camelCase → UPPER_UNDERSCORE สำหรับ constants

การติดตั้ง Guava

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>33.0.0-jre</version>
</dependency>

ข้อจำกัดและข้อควรระวัง

  • รองรับ ASCII only - ไม่รองรับ Unicode identifiers penuh
  • ไม่จัดการ acronyms อัตโนมัติ (เช่น XMLHttpRequest อาจแปลงเป็น x_m_l_http_request)
  • สำหรับ complex cases ควรใช้ library เฉพาะทาง เช่น commons-text WordUtils
  • Performance: เหมาะสำหรับ compile-time/code-gen ไม่ใช่ hot path runtime

ตัวอย่าง Real-world: Database Column to Java Field

public class DbToJavaConverter {
    public static String columnToField(String columnName) {
        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, columnName);
    }

    public static String fieldToColumn(String fieldName) {
        return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, fieldName);
    }

    public static void main(String[] args) {
        String[] columns = {"user_id", "first_name", "last_name", "email_address", "created_at"};
        for (String col : columns) {
            System.out.println(col + " -> " + columnToField(col));
        }
    }
}

Output

user_id -> userId
first_name -> firstName
last_name -> lastName
email_address -> emailAddress
created_at -> createdAt