使用Guava的CaseFormat进行数据规范化

发布时间 2023-05-23 11:56:47作者: 畔山陆仁贾

需求背景

在数据规范化的过程中,经常面临多种数据源,字段存在不同的命名风格,那有没有一种方法方便我们对字段名称进行规范化。

用什么做

恰好,Guava提供了这种能力。Guava的CaseFormat类可以做到统一命名风格。话不多说,直接看源码。

在CaseFormat中定义了五个枚举值:

img

五个枚举值分别对应五种场景的命名格式:

枚举常量 说明 输入 输出
LOWER_CAMEL 小写驼峰式命名 aa_bb_cc aaBbCc
UPPER_CAMEL 大写驼峰式命名 aa_bb_cc AaBbCc
LOWER_UNDERSCORE 小写下划线命名 AaBbCc aa_bb_cc
UPPER_UNDERSCORE 大写下划线命名 AaBbCc AA_BB_CC
LOWER_HYPHEN 小写字符连接命名 AaBbCc aa-bb-cc

话不多说,上代码

import com.google.common.base.CaseFormat;

public class CaseFormatTest1 {

public static void main(String[] args) {
    System.out.println("------------------");
    String inputStr1 = "aa_bb_cc";
    System.out.println(String.format("inputStr1:%s,LOWER_CAMEL:%s", inputStr1, CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, inputStr1)));
    System.out.println(String.format("inputStr1:%s,UPPER_CAMEL:%s", inputStr1, CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, inputStr1)));
    System.out.println(String.format("inputStr1:%s,LOWER_UNDERSCORE:%s", inputStr1, CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_UNDERSCORE, inputStr1)));
    System.out.println(String.format("inputStr1:%s,UPPER_UNDERSCORE:%s", inputStr1, CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_UNDERSCORE, inputStr1)));
    System.out.println(String.format("inputStr1:%s,LOWER_HYPHEN:%s", inputStr1, CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN, inputStr1)));
    System.out.println("------------------");
    String inputStr2 = "AaBbCc";
    System.out.println(String.format("inputStr2:%s,LOWER_CAMEL:%s", inputStr2, CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_CAMEL, inputStr2)));
    System.out.println(String.format("inputStr2:%s,UPPER_CAMEL:%s", inputStr2, CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, inputStr2)));
    System.out.println(String.format("inputStr2:%s,LOWER_UNDERSCORE:%s", inputStr2, CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, inputStr2)));
    System.out.println(String.format("inputStr2:%s,UPPER_UNDERSCORE:%s", inputStr2, CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, inputStr2)));
    System.out.println(String.format("inputStr2:%s,LOWER_HYPHEN:%s", inputStr2, CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, inputStr2)));
    System.out.println("------------------");
    String inputStr3 = "aaBbCc";
    System.out.println(String.format("inputStr3:%s,LOWER_CAMEL:%s", inputStr3, CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, inputStr3)));
    System.out.println(String.format("inputStr3:%s,UPPER_CAMEL:%s", inputStr3, CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, inputStr3)));
    System.out.println(String.format("inputStr3:%s,LOWER_UNDERSCORE:%s", inputStr3, CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_UNDERSCORE, inputStr3)));
    System.out.println(String.format("inputStr3:%s,UPPER_UNDERSCORE:%s", inputStr3, CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_UNDERSCORE, inputStr3)));
    System.out.println(String.format("inputStr3:%s,LOWER_HYPHEN:%s", inputStr3, CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN, inputStr3)));
}
}

输出:
------------------
inputStr1:aa_bb_cc,LOWER_CAMEL:aaBbCc
inputStr1:aa_bb_cc,UPPER_CAMEL:AaBbCc
inputStr1:aa_bb_cc,LOWER_UNDERSCORE:aa_bb_cc
inputStr1:aa_bb_cc,UPPER_UNDERSCORE:AA_BB_CC
inputStr1:aa_bb_cc,LOWER_HYPHEN:aa-bb-cc
------------------
inputStr2:AaBbCc,LOWER_CAMEL:AaBbCc
inputStr2:AaBbCc,UPPER_CAMEL:AaBbCc
inputStr2:AaBbCc,LOWER_UNDERSCORE:aa_bb_cc
inputStr2:AaBbCc,UPPER_UNDERSCORE:AA_BB_CC
inputStr2:AaBbCc,LOWER_HYPHEN:aa-bb-cc
------------------
inputStr3:aaBbCc,LOWER_CAMEL:aabbcc
inputStr3:aaBbCc,UPPER_CAMEL:Aabbcc
inputStr3:aaBbCc,LOWER_UNDERSCORE:aaBbCc
inputStr3:aaBbCc,UPPER_UNDERSCORE:AABBCC
inputStr3:aaBbCc,LOWER_HYPHEN:aaBbCc

总结

对于强迫症患者,看着统一风格的变量、字段,着实有一种满足感。使用CaseFormat,可极大满足这种癖好。良好的命名规范,其实也是一种数据质量的体现。