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()就能够使得这项工作很简单:
- public class Item {
- private String id;
- private String name;
- public Item(String id, String name) {
- this.id = id;
- this.id = name;
- }
- public String getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- @Override
- public int hashCode() {
- return Objects.hashCode(getId(), getName());
- }
- @Override
- public String toString() {
- return Objects.toStringHelper(this).add("id", getId()).add("name", getName()).toString();
- }
- @Override
- public boolean equals(Object o) {
- if (!(o instanceof Item)) {
- return false;
- }
- Item other = (Item) o;
- return Objects.equal(getId(), other.getId()) && Objects.equal(getName(), other.getName());
- }
- }
这个toString的结果是:
- Item{id=1, name=Book}
Throwables
对异常对象进行封装不总是合适的,因为这可能导致ClassNotFoundException(可能因为不同的classloader或者序列化导致此问题),Throwables能够解耦,并且允许远程的客户端看到异常堆栈的内容:
- try {
- // throws implementation specific exception
- } catch (InternalException e) {
- throw new ApiException("reason", Throwables.getStackTraceAsString(e));
- }
Iterables
对两个集合进行合并,然后对他们的元素进行处理,这个工作需要耗费很多的代码,但是有了Iterables,事情就简单多了:
- for (Item item : Iterables.concat(books, electronics)) {
- // do something useful
- }
Preconditions
很多情况下,一个类的构造函数或者方法的参数都需要对参数的值进行限制,非法的值应该被及时发现和处理,一般情况下,对这些非法的情况快速的返回异常比在后面再处理要好得多。
- public Item(String id, String name) {
- this.id = Preconditions.checkNotNull(id, "id must not be null");
- this.name = Preconditions.checkNotNull(name, "name must not be null");
- Preconditions.checkArgument(name.length() > 6, "name must be longer than 6 chars");
- }
Constraints
Constraints和Preconditions很类似,但是它是被用来限制集合的元素,这样能够使得代码很干净,因为限制性的代码和业务代码分开了。
- public class Voyage {
- private Country targetcountry;
- private int capacity;
- private List<Cargo> items = Constraints.constrainedList(new ArrayList<Cargo>(), new Constraint<Cargo>() {
- @Override
- public Cargo checkElement(Cargo cargo) {
- Preconditions.checkNotNull(cargo);
- Preconditions.checkArgument(targetcountry.allows(cargo));
- Preconditions.checkArgument(cargo.getUnits() gt; 0);
- return cargo;
- }
- });
- public void load(List<Cargo> cargos) {
- items.addAll(cargos);
- }
- }
0 0
- Google Guava:快速入门(转载)
- Google Guava:快速入门(转载)
- Google Guava快速入门
- Google Guava官方教程(中文版 转载)
- [转载]Google Guava官方教程(中文版)
- Google Guava ListenableFuture入门
- guava快速入门
- 【转载】Guava学习笔记:Google Guava 类库简介
- Google guava
- Google Guava
- google guava
- google guava
- Google Guava
- Google从入门到精通(转载)
- google gtest 快速入门
- Google ProtoBuf快速入门
- Google Guava学习(1)-Guava Optional类
- Google Guava学习(2)-Guava Preconditions类
- log4net面面观之工作原理
- Eclipse中使用SVN
- codeforce 374A 网格图最短路走法
- 天黑了
- Step-By-Step Guide To Create Physical Standby On Normal File System For ASM Primary using RMAN
- Google Guava:快速入门(转载)
- HTML title属性 换行
- 转自 老罗 Android应用程序资源管理器(Asset Manager)的创建过程分析
- Android-x86入门之--各类问题总结
- CentOS配置IP地址
- log4net面面观之Repository
- 如何实现android ScrollView ListView的回弹效果
- xcode5改变模拟器size
- ios本地通知和远程通知