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 ...
随机推荐
- Successor hdu 4366 线段树
题意: 现在n个人,其中编号0的是老板,之后n-1个员工,每个员工只有一个上司,有一个忠诚值和能力值.每次要解雇一个人的时候,从他的下属中选取能力值大于他的且忠诚值最高的一个,若不存在则输出-1.共m ...
- 【Vue实战之路】二、路由使用基础,六步搞定Vue-router
vue-router的出现是为了解决路由与视图(实际项目中的单文件组件)的对应关系.若单单为了实现交互时对相应组件的渲染,则通过vue的基础操作完全可以实现,那么为什么要是用vue-router呢,个 ...
- StringBuffer StringBuilder append
StringBuilder is not thread safe. So, it performs better in situations where thread safety is not re ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem D. Distance 迪杰斯特拉
Problem D. Distance 题目连接: http://codeforces.com/gym/100714 Description In a large city a cellular ne ...
- hdu 5726 GCD 暴力倍增rmq
GCD/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence ...
- centos7 打造基于python语言Selenium2自动化开发环境
1. 准备 安装模块 # yum groupinstall "Development tools" # yum install zlib-devel bzip2-devel ope ...
- word标题编号与上一级不一致的解决方法
前段时间时间就遇到了这个问题,情形如下: 1.标题内容一 1.1二级标题 1.2二级标题 2.第二个一级标题 1.3第二个一级下的二级标题 1.4.就这样乱了 当时搜索一通后很容易就解决了……于是忘记 ...
- BrowserLog——使用Chrome控制台作为Log查看器
Chrome控制台是十分强大的,即使将它作为一个log查看器也是非常强大的,BrowserLog就是一个.net下的把Chrome作为log输出的程序包. 原理非常简单,server端将log数据通过 ...
- maria-developers 开发者邮件
https://lists.launchpad.net/maria-developers/
- HDU 4568 SPFA + TSP
这道题是长沙邀请赛的题,当时是道签到题. 这种题还是很常见的,讲一下思路. 首先是预处理出每个宝藏之间的距离,还有到边的距离,直接对每个宝藏进行一次SPFA就可以了. 然后就是经典的求TSP的过程. ...