概述

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. django模型查询操作

    一旦创建好了数据模型,Django就会自动为我们提供一个数据库抽象API,允许创建.检索.更新和删除对象操作 下面的示例都是通过下面参考模型来对模型字段进行操作说明: from django.db i ...

  2. MIT-6.828-JOS-lab4:Preemptive Multitasking

    Lab 4: Preemptive Multitasking tags: mit-6.828, os 概述 本文是lab4的实验报告,主要围绕进程相关概念进行介绍.主要将四个知识点: 开启多处理器.现 ...

  3. Java程序员进击书籍推荐

    计算机基础 计算机科学导论 计算机操作系统 操作系统原理及应用(Linux) Java 基础和进阶 疯狂Java讲义 Java 核心基础卷1/2 Java编程思想 Java 8实战 jls11 Eff ...

  4. [转]c++优先队列(priority_queue)用法详解

    既然是队列那么先要包含头文件#include <queue>, 他和queue不同的就在于我们可以自定义其中数据的优先级, 让优先级高的排在队列前面,优先出队 优先队列具有队列的所有特性, ...

  5. 【BZOJ 1430】 1430: 小猴打架 (Prufer数列)

    1430: 小猴打架 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 625  Solved: 452 Description 一开始森林里面有N只互不相 ...

  6. 13、Redis的发布订阅模式

     写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------------------- ...

  7. 使用 IntraWeb (8) - 系统模板

    我们可以自定义系统错误模板, 编辑 IWError.html 放到模板文件夹后, 它将替换默认的模板. {在主页面, 这是要模拟一个系统错误} procedure TIWForm1.IWButton1 ...

  8. java 虚拟机启动参数 (转)

    在Java.J2EE大型应用中,JVM非标准参数的配置直接关系到整个系统的性能. JVM非标准参数指的是JVM底层的一些配置参数,这些参数在一般开发中默认即可,不需要任何配置.但是在生产环境中,为了提 ...

  9. mmc生产任务分配问题续

    mmc生产任务分配问题续,本题目比上个题目难, 要注意的是,生产,销售,库存的关系, 生产+上月库存-销售=本月库存, 期初,生产=库存,销售没有.

  10. Delphi加载驱动

    program Project2; uses Windows, Native, JwaWinType, Unit_Driver; function Is2KXp(): Boolean; var OSV ...