Google Guava:快速入门(转载)

来源:互联网 发布:网上买鲜花 知乎 编辑:程序博客网 时间:2024/05/19 13:42

转载自http://vipcowrie.iteye.com/blog/1524006

Google Guava快速入门 


众所周知,Apache commons提供了一系列强大的功能库,对JDK提供了很好的补充,但是这里不介绍commons,这里介绍给大家的是Google Guava,一个被多数人遗忘的英雄。他是一个独立的库,为大家提供了日常开发经常使用的函数,包括集合处理、字符串处理、并发、IO、原始类型处理和异常处理等。 

Google Guava太强大,里面函数太多,这里我只能选取一些比较典型的介绍给大家,让大家有一个初步的认识。 

Objects 
Objects是的实现类的hashcode()/equals()方法很简单,那些希望在类里面实现toString()以方便调试和日志查看的情况,自己写toString()实在太麻烦了,Objects.toStringHelper()就能够使得这项工作很简单: 

Java代码  收藏代码
  1. public class Item {  
  2.   private String id;  
  3.   private String name;  
  4.    
  5.   public Item(String id, String name) {  
  6.     this.id = id;  
  7.     this.id = name;  
  8.   }  
  9.    
  10.   public String getId() {  
  11.     return id;  
  12.   }  
  13.    
  14.   public String getName() {  
  15.     return name;  
  16.   }  
  17.    
  18.   @Override  
  19.   public int hashCode() {  
  20.     return Objects.hashCode(getId(), getName());  
  21.   }  
  22.    
  23.   @Override  
  24.   public String toString() {  
  25.     return Objects.toStringHelper(this).add("id", getId()).add("name", getName()).toString();  
  26.   }  
  27.    
  28.   @Override  
  29.   public boolean equals(Object o) {  
  30.     if (!(o instanceof Item)) {  
  31.       return false;  
  32.     }  
  33.     Item other = (Item) o;  
  34.     return Objects.equal(getId(), other.getId()) && Objects.equal(getName(), other.getName());  
  35.   }  
  36. }  

这个toString的结果是: 
Java代码  收藏代码
  1. Item{id=1, name=Book}  


Throwables 

对异常对象进行封装不总是合适的,因为这可能导致ClassNotFoundException(可能因为不同的classloader或者序列化导致此问题),Throwables能够解耦,并且允许远程的客户端看到异常堆栈的内容: 

Java代码  收藏代码
  1. try {  
  2.   // throws implementation specific exception  
  3. catch (InternalException e) {  
  4.   throw new ApiException("reason", Throwables.getStackTraceAsString(e));  
  5. }  


Iterables 
对两个集合进行合并,然后对他们的元素进行处理,这个工作需要耗费很多的代码,但是有了Iterables,事情就简单多了: 
Java代码  收藏代码
  1. for (Item item : Iterables.concat(books, electronics)) {  
  2.   // do something useful  
  3. }  


Preconditions 
很多情况下,一个类的构造函数或者方法的参数都需要对参数的值进行限制,非法的值应该被及时发现和处理,一般情况下,对这些非法的情况快速的返回异常比在后面再处理要好得多。 

Java代码  收藏代码
  1. public Item(String id, String name) {  
  2.   this.id = Preconditions.checkNotNull(id, "id must not be null");  
  3.   this.name = Preconditions.checkNotNull(name, "name must not be null");  
  4.   Preconditions.checkArgument(name.length() > 6"name must be longer than 6 chars");  
  5. }  


Constraints 
Constraints和Preconditions很类似,但是它是被用来限制集合的元素,这样能够使得代码很干净,因为限制性的代码和业务代码分开了。 

Java代码  收藏代码
  1. public class Voyage {  
  2.   private Country targetcountry;  
  3.   private int capacity;  
  4.   private List<Cargo> items = Constraints.constrainedList(new ArrayList<Cargo>(), new Constraint<Cargo>() {  
  5.     @Override  
  6.     public Cargo checkElement(Cargo cargo) {  
  7.       Preconditions.checkNotNull(cargo);  
  8.       Preconditions.checkArgument(targetcountry.allows(cargo));  
  9.       Preconditions.checkArgument(cargo.getUnits() gt; 0);  
  10.       return cargo;  
  11.     }  
  12.   });  
  13.    
  14.   public void load(List<Cargo> cargos) {  
  15.     items.addAll(cargos);  
  16.   }  
  17. }  

0 0