Guava 15新特性介绍

时间:2023-03-08 22:12:30
Guava 15新特性介绍

原文:http://www.javacodegeeks.com/2013/10/guava-15-new-features.html
Guava 是众所周知的google出品的开源工具包,十分好用,本月退出了version 15的版本,其中主要的几个新特性有

1 Escapers字符转义器:

HtmlEscapers
XmlEscapers
UrlEscapers

还可以自定义escaper,比如

    // escaping HTML
HtmlEscapers.htmlEscaper().escape("echo foo > file &");
// [result] echo foo > file & // escaping XML attributes and content
XmlEscapers.xmlAttributeEscaper().escape("foo \"bar\"");
// [result] echo "bar" XmlEscapers.xmlContentEscaper().escape("foo \"bar\"");
// [result] foo "bar" // Custom Escaper
// escape single quote with another single quote
// and escape ampersand with backslash Escaper myEscaper = Escapers.builder() .addEscape('\'', "''") .addEscape('&', "\\&").build();

2 StandardSystemProperty:

这个是用来方便调用如java.version, java.home 等环境变量的,使用的是enum了,比如:

    String java_version_value = StandardSystemProperty.JAVA_VERSION.value();
System.out.println(java_version_value);//1.6.0_16 String java_version_key = StandardSystemProperty.JAVA_VERSION.key();
System.out.println(java_version_key);//java.version

3 EvictingQueue:

这个是一个非阻塞的队列,当队列长度满了后,自动移除头元素,比如:

    EvictingQueue<String> queue = EvictingQueue.create(3);
queue.add("one");
queue.add("two");
queue.add("three");
queue.add("four");
// the head of the queue is evicted after adding the fourth element
// queue contains: [two, three, four]

4  fileTreeTraverser 文件遍历递归利器:

这个方法可以快速遍历某个文件目录下的所有文件,比如:

    FluentIterable<File> iterable = Files.fileTreeTraverser().breadthFirstTraversal(new File("d:\\ddd.sql"));
for (File file : iterable) {
System.out.println(file.getAbsolutePath());
}