Guava CaseFormat
概述
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的更多相关文章
- Guava之CaseFormat
com.google.common.base.CaseFormat是一种实用工具类,以提供不同的ASCII字符格式之间的转换. 其对应的枚举常量 从以上枚举中可以看出,java程序员最常用的转换类型为 ...
- Google的java工具类Guava
前言 google开发java项目肯定也不想重复造轮子,所以肯定也有工具类,就是它了:Guava 我将举例几个实际的例子,发挥这个工具类好用的功能.更多的方法和功能,还有内部的实现可以直接参考http ...
- Guava:好用的java类库 学习小记
基础功能 google guava中定义的String操作 在google guava中为字符串操作提供了很大的便利,有老牌的判断字符串是否为空字符串或者为null,用指定字符填充字符串,以及拆分合并 ...
- [Google Guava] 6-字符串处理:分割,连接,填充
原文链接 译文链接 译者:沈义扬,校对:丁一 连接器[Joiner] 用分隔符把字符串序列连接起来也可能会遇上不必要的麻烦.如果字符串序列中含有null,那连接操作会更难.Fluent风格的Joine ...
- 有关google的guava工具包详细说明
Guava 中文是石榴的意思,该项目是 Google 的一个开源项目,包含许多 Google 核心的 Java 常用库. 目前主要包含: com.google.common.annotations c ...
- Java开发的得力助手---Guava
导语 guava是google出品的java类库,被google广泛用于内部项目,该类库经过google大牛们的千锤百炼,以优雅的设计在java世界流行.版本迭代至今,很多思想甚至被JDK标准库借鉴, ...
- Spring cache简单使用guava cache
Spring cache简单使用 前言 spring有一套和各种缓存的集成方式.类似于sl4j,你可以选择log框架实现,也一样可以实现缓存实现,比如ehcache,guava cache. [TOC ...
- Guava库介绍之实用工具类
作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...
- Google Java编程库Guava介绍
本系列想介绍下Java下开源的优秀编程库--Guava[ˈgwɑːvə].它包含了Google在Java项目中使用一些核心库,包含集合(Collections),缓存(Caching),并发编程库(C ...
随机推荐
- SpringMVC框架03——数据绑定
1.绑定基本数据类型 在Controller类中添加业务方法: /** * 绑定基本数据类型 */ @RequestMapping("/baseType") @ResponseBo ...
- Java NIO -2
NIO http://www.cnblogs.com/puyangsky/p/5840873.html -- 操作系统与 Java 基于流的 I/O模型有些不匹配.操作系统要移动的是大块数据(缓冲区) ...
- hdu 4451 37届金华赛区 J题
题意:给出衣服裤子鞋子的数目,有一些衣服和裤子,裤子和鞋子不能搭配,求最终的搭配方案总数 wa点很多,我写wa了很多次,代码能力需要进一步提升 #include<cstdio> #incl ...
- hdu 4438 第37届ACM/ICPC 天津赛区现场赛H题
题意:Alice和Bob两个人去打猎,有两种(只)猎物老虎和狼: 杀死老虎得分x,狼得分y: 如果两个人都选择同样的猎物,则Alice得分的概率是p,则Bob得分的概率是(1-p): 但是Alice事 ...
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem H. Password Service dp
Problem H. Password Service 题目连接: http://www.codeforces.com/gym/100253 Description Startups are here ...
- 【转】InitializingBean的作用
原文链接:http://blog.csdn.net/maclaren001/article/details/37039749 最近工作需要得到sping中的每个事物需要执行的sql,称机会简单研究了一 ...
- spring cloud 学习(5) - config server
分布式环境下的统一配置框架,已经有不少了,比如百度的disconf,阿里的diamand.今天来看下spring cloud对应的解决方案: 如上图,从架构上就可以看出与disconf之类的有很大不同 ...
- 反编译APK文件的三种方法(转)
因为学习Android编程的需要,有时我们需要对网络上发布的应用项目进行学习,可是Android项目一般是通过APK文件进行发布的,我们看不到源代码,嘿嘿,办法总会有的,而且不止一个... ps:对于 ...
- SGU 275. To xor or not to xor (高斯消元法)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=275 题意:给你n个数,可以选择任意个数异或,但是要使得最后的异或值最大. 我们把每 ...
- BootstrapClassloader ExtClassloader AppClassloader
http://www.importnew.com/26269.html import java.net.URL; class test9 { public static void main(Str ...