Guava 15新特性介绍

来源:互联网 发布:php strip tags源码 编辑:程序博客网 时间:2024/05/22 03:30

Guava 15新特性介绍

发表于2年前(2013-10-06 08:02)   阅读(417) | 评论(2) 10人收藏此文章, 我要收藏
0

【活动】OSC账号一键绑定监控宝,给你送水果

原文: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了,比如: 
StandardSystemProperty.JAVA_VERSION.value(); 
// [result] 1.7.0_25 


StandardSystemProperty.JAVA_VERSION.key(); 
// [result] java.version 
3 EvictingQueue 
  这个是一个非阻塞的队列,当队列长度满了后,自动移除头元素,比如: 
  EvictingQueue<String> q = EvictingQueue.create(3); 
q.add("one"); 
q.add("two"); 
q.add("three"); 
q.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("/var/tmp")); 
for (File f : iterable) { 
    System.out.println(f.getAbsolutePath()); 

 其中f.getAbsolutePath()用的是jdk 7中的方法 
0 0
原创粉丝点击