Chen Yangjian's Blog

Carpe diem - Seize the day

Guava Libraries

| Comments

Google 近日发布了个增强 Java 1.6 的库,叫做 Guava Libraries,涵盖语言基本、输入输出流、网络通信等方面。让我想到了 Apache 的 commons 库,以前用过 commons.io 和 commons.net,用来处理 Servlet 里头的文件上传和通过 FTP 完成文件传输。

Guava 与它的区别是,顾名思意,有的地方就不再是 Java 了。此外,只面向 Java 1.6,可应用于平台如 Google App Engine、Android 等。可喜的是,改善的都是我所喜闻乐见的。

Guava 还在很初期的阶段,官方的介绍【PDF】也很简洁,不过它指出来的 Java 语言中某些差强人意的地方挺有意思。例如 String 的 split 方法:

小测试:”,a,,b,”.split(“,”) 的返回值是…

“”, “a”, “”, “b”, “” null, “a”, null, “b”, null “a”, null, “b” “a”, “b” None of the above

答案是以上皆否,返回的是 "", "a", "", "b",只有末尾的逗号被忽略掉了。于是 Guava 里头提供了一个字符串切割的类(Spliter)。

Splitter.on(',').trimResults().omitEmptyStrings().split(" foo, ,bar, quux,")

如今把成员方法串起来很流行呀。

Comments