概述

CaseFormat用来转换各种不同的编程语言间的变量名命名格式, 主要用到的方法只有一个 CaseFormat.to(CaseFormat from, String s)

        CaseFormat fromFormat = CaseFormat.LOWER_CAMEL;
CaseFormat toFormat = CaseFormat.UPPER_CAMEL;
String s = "lowerCamel";
System.out.println(fromFormat.to(toFormat, s));

输出

lowerCamel
LowerCamel

代码分析

package com.google.common.base;

import com.google.common.annotations.GwtCompatible;

/**
* Utility class for converting between various ASCII case formats.
* 转换ASCII字符串各种格式的工具类
*
* @author Mike Bostock
* @since 1.0
*/
@GwtCompatible
public enum CaseFormat {
/**
* Hyphenated variable naming convention, e.g., "lower-hyphen".
* 带连接符的变量名转换
*/
LOWER_HYPHEN(CharMatcher.is('-'), "-"), /**
* C++ variable naming convention, e.g., "lower_underscore".
* C++ 变量名转换
*/
LOWER_UNDERSCORE(CharMatcher.is('_'), "_"), /**
* Java variable naming convention, e.g., "lowerCamel".
* Java 变量名转换
*/
LOWER_CAMEL(CharMatcher.inRange('A', 'Z'), ""), /**
* Java and C++ class naming convention, e.g., "UpperCamel".
* Java 和 C++ 类名转换
*/
UPPER_CAMEL(CharMatcher.inRange('A', 'Z'), ""), /**
* Java and C++ constant naming convention, e.g., "UPPER_UNDERSCORE".
* Java 和 C++ 常量命名转换
*/
UPPER_UNDERSCORE(CharMatcher.is('_'), "_"); private final CharMatcher wordBoundary;
private final String wordSeparator; CaseFormat(CharMatcher wordBoundary, String wordSeparator) {
this.wordBoundary = wordBoundary;
this.wordSeparator = wordSeparator;
} /**
* Converts the specified {@code String s} from this format to the specified {@code format}. A
* "best effort" approach is taken; if {@code s} does not conform to the assumed format, then the
* behavior of this method is undefined but we make a reasonable effort at converting anyway.
*
* 使用这个format将指定String s转为指定format.采取的是"尽力而为"的方法;假如s不符合设定的格式
* 那么这个方法的行为将会是不确定的,但我们会尽量做出合理的转换
*
* 实际上我们使用的只有这一个方法
*/
public String to(CaseFormat format, String s) {
if (format == null) {
throw new NullPointerException();
}
if (s == null) {
throw new NullPointerException();
} if (format == this) {
return s;
} /* optimize cases where no camel conversion is required */
/* 没有驼峰转换的时候优化转换 */
switch (this) {
case LOWER_HYPHEN:
switch (format) {
case LOWER_UNDERSCORE:
return s.replace('-', '_');
case UPPER_UNDERSCORE:
return Ascii.toUpperCase(s.replace('-', '_'));
}
break;
case LOWER_UNDERSCORE:
switch (format) {
case LOWER_HYPHEN:
return s.replace('_', '-');
case UPPER_UNDERSCORE:
return Ascii.toUpperCase(s);
}
break;
case UPPER_UNDERSCORE:
switch (format) {
case LOWER_HYPHEN:
return Ascii.toLowerCase(s.replace('_', '-'));
case LOWER_UNDERSCORE:
return Ascii.toLowerCase(s);
}
break;
} // otherwise, deal with camel conversion
// 处理驼峰转其他的转换
StringBuilder out = null;
int i = 0;
int j = -1;
// 将字符串按分隔符切分单词,转换每个单词
while ((j = wordBoundary.indexIn(s, ++j)) != -1) {
if (i == 0) {
// include some extra space for separators
// 为分隔符留出额外的空间
out = new StringBuilder(s.length() + 4 * wordSeparator.length());
// 第一个单词使用normalizeFirstWord处理
out.append(format.normalizeFirstWord(s.substring(i, j)));
} else {
// 后续单词用normalizeWord处理
out.append(format.normalizeWord(s.substring(i, j)));
}
out.append(format.wordSeparator);
// 当前坐标后移
i = j + wordSeparator.length();
}
if (i == 0) {
return format.normalizeFirstWord(s);
}
// 处理最后一个分隔符右边的字符串
out.append(format.normalizeWord(s.substring(i)));
return out.toString();
} /**
* 将第一个单词普通化
* LOWER_CAMEL -> 全小写
* 其他 -> normalizeWord
*/
private String normalizeFirstWord(String word) {
switch (this) {
case LOWER_CAMEL:
return Ascii.toLowerCase(word);
default:
return normalizeWord(word);
}
} /**
* 将单词普通化
* LOWER_HYPHEN, LOWER_UNDERSCORE -> 全小写
* LOWER_CAMEL, UPPER_CAMEL -> 第一个字母大写其他字母小写
* UPPER_UNDERSCORE -> 全大写
*/
private String normalizeWord(String word) {
switch (this) {
case LOWER_HYPHEN:
return Ascii.toLowerCase(word);
case LOWER_UNDERSCORE:
return Ascii.toLowerCase(word);
case LOWER_CAMEL:
return firstCharOnlyToUpper(word);
case UPPER_CAMEL:
return firstCharOnlyToUpper(word);
case UPPER_UNDERSCORE:
return Ascii.toUpperCase(word);
}
throw new RuntimeException("unknown case: " + this);
} /**
* 将单词第一个字母变大写,其他变小写
*/
private static String firstCharOnlyToUpper(String word) {
int length = word.length();
if (length == 0) {
return word;
}
return new StringBuilder(length)
.append(Ascii.toUpperCase(word.charAt(0)))
.append(Ascii.toLowerCase(word.substring(1)))
.toString();
}
}

Guava CaseFormat的更多相关文章

  1. Guava之CaseFormat

    com.google.common.base.CaseFormat是一种实用工具类,以提供不同的ASCII字符格式之间的转换. 其对应的枚举常量 从以上枚举中可以看出,java程序员最常用的转换类型为 ...

  2. Google的java工具类Guava

    前言 google开发java项目肯定也不想重复造轮子,所以肯定也有工具类,就是它了:Guava 我将举例几个实际的例子,发挥这个工具类好用的功能.更多的方法和功能,还有内部的实现可以直接参考http ...

  3. Guava:好用的java类库 学习小记

    基础功能 google guava中定义的String操作 在google guava中为字符串操作提供了很大的便利,有老牌的判断字符串是否为空字符串或者为null,用指定字符填充字符串,以及拆分合并 ...

  4. [Google Guava] 6-字符串处理:分割,连接,填充

    原文链接 译文链接 译者:沈义扬,校对:丁一 连接器[Joiner] 用分隔符把字符串序列连接起来也可能会遇上不必要的麻烦.如果字符串序列中含有null,那连接操作会更难.Fluent风格的Joine ...

  5. 有关google的guava工具包详细说明

    Guava 中文是石榴的意思,该项目是 Google 的一个开源项目,包含许多 Google 核心的 Java 常用库. 目前主要包含: com.google.common.annotations c ...

  6. Java开发的得力助手---Guava

    导语 guava是google出品的java类库,被google广泛用于内部项目,该类库经过google大牛们的千锤百炼,以优雅的设计在java世界流行.版本迭代至今,很多思想甚至被JDK标准库借鉴, ...

  7. Spring cache简单使用guava cache

    Spring cache简单使用 前言 spring有一套和各种缓存的集成方式.类似于sl4j,你可以选择log框架实现,也一样可以实现缓存实现,比如ehcache,guava cache. [TOC ...

  8. Guava库介绍之实用工具类

    作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...

  9. Google Java编程库Guava介绍

    本系列想介绍下Java下开源的优秀编程库--Guava[ˈgwɑːvə].它包含了Google在Java项目中使用一些核心库,包含集合(Collections),缓存(Caching),并发编程库(C ...

随机推荐

  1. python基础下的数据结构与算法之链表

    一.链表的定义 用链接关系显式表示元素之间顺序关系的线性表称为链接表或链表. 二.单链表的python实现 class Node(object): """定义节点&quo ...

  2. 通俗讲解transform3D变换时css各属性的作用与搭配

    当没有浏览器兼容性限制时,就大胆地使用transiton的3D效果吧,前端也要做不一样的烟火! *常用的3D效果 rotateX/rotateY/rotateZ/rotate3dtranslateX/ ...

  3. BZOJ.1396.识别子串(后缀自动机/后缀数组 线段树)

    题目链接 SAM:能成为识别子串的只有那些|right|=1的节点代表的串. 设这个节点对应原串的右端点为r[i],则如果|right[i]|=1,即\(s[\ [r_i-len_i+1,r_i-le ...

  4. 【BZOJ-3672】购票 树分治 + 斜率优化DP

    3672: [Noi2014]购票 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1177  Solved: 562[Submit][Status][ ...

  5. rabbitmq的发布确认和事务 - 2207872494的个人空间

    rabbitmq的发布确认和事务 - 2207872494的个人空间   https://my.oschina.net/lzhaoqiang/blog/670749

  6. ubuntu下msmtp+mutt的安装和配置

    1.mutt+msmtp的安装 默认情况下smokeping发送邮件使用sendmail,但是sendmail配置起来真心不是一般的麻烦,而且也没有必要,完全大材小用了,所以我就想用mutt+msmt ...

  7. systemtap跟踪C

    1.[root@localhost ~]# rpm -qi  glibcName        : glibc                        Relocations: (not rel ...

  8. mixpanel实验教程(1)

    一.关于 mixpanel 这个我不想多说,不明确请看官方手冊:https://mixpanel.com/help/reference/ 二.注冊 mixpanel.com 是一个商业机构.它的用户分 ...

  9. AdjustWindowRect 与 SetWindowPos

    这两个函数经常一起使用,所以放到一起讲: 1 AdjustWindowRect 函数功能:该函数依据所需客户矩形的大小,计算需要的窗口矩形的大小.计算出的窗口矩形随后可以传递给CreateWindow ...

  10. lufylegend基础知识1

    这是官方的介绍: lufylegend是一个HTML5开源引擎,它实现了利用仿ActionScript3.0的语法进行HTML5的开发, 包含了LSprite,LBitmapData,LBitmap, ...