Guava学习笔记(1)--安全地使用null(Using and avoiding null)
2012-06-20 20:26
661 查看
Guava是一个基于java1.6的类库集合, 包含了几个google的核心库, collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O等库, 这些工具都使用在google的生产环境.
使用Guava很简单, 只需要包含他的库文件既可.
官方网址: http://code.google.com/p/guava-libraries/
null是一个特殊的值, 却经常是有着实在含义的, null可以表达失败, 可以表示成功, 可以表示任何我们定义的含义.
比如Map.get(key) 返回null, 这个是两个含义, 可以是key本身就对应null值, 也可能是key不存在. 简单的null不能表达这个确定的含义.
但是, 简单的null很难确切表达这样的含义. 而且, null本身也会引起一些程序的bug, 所以我们应该用null的替代物而不是null本身, 这样我们可以更具体的知道null所代表的含义.
Guava为我们提供了一个Optional的抽象类, 下面有两个主要的类Present和Absent. 一个表示值存在, 一个表示不存在. 这样的表示我们就获得了更加具有含义的两种值.
构造方法
Optional使用下列三个静态方法来构造, Present和Absent都是不可直接实例化的, 只可以使用下列的方法来构建实例, 应该使用了工厂模式.
访问方法
下面是一个例子, 展示其最基本的使用. 使用Optional可以避免使用null, 而又可以具有null不存在的含义.
这个里面有一些类似函数式编程的风格, 不使用null, 而又具备null的含义.
参考资料:
官方wiki: http://code.google.com/p/guava-libraries/wiki/GuavaExplained
Using and avoiding null: http://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained
使用Guava很简单, 只需要包含他的库文件既可.
官方网址: http://code.google.com/p/guava-libraries/
null是一个特殊的值, 却经常是有着实在含义的, null可以表达失败, 可以表示成功, 可以表示任何我们定义的含义.
比如Map.get(key) 返回null, 这个是两个含义, 可以是key本身就对应null值, 也可能是key不存在. 简单的null不能表达这个确定的含义.
但是, 简单的null很难确切表达这样的含义. 而且, null本身也会引起一些程序的bug, 所以我们应该用null的替代物而不是null本身, 这样我们可以更具体的知道null所代表的含义.
Guava为我们提供了一个Optional的抽象类, 下面有两个主要的类Present和Absent. 一个表示值存在, 一个表示不存在. 这样的表示我们就获得了更加具有含义的两种值.
构造方法
Optional使用下列三个静态方法来构造, Present和Absent都是不可直接实例化的, 只可以使用下列的方法来构建实例, 应该使用了工厂模式.
Optional.of(T) | 返回一个包含给定数值的Present, 如果是null值, 就直接失败. |
Optional.absent() | 返回具有某种类型的Absent.INSTANCE |
Optional.fromNullable(T) | 返回一个包含给定数值的Present, 如果是null值, 返回具有某种类型的Absent.INSTANCE |
boolean isPresent() | 如果非null, 返回true, 如果是null, 返回false |
T get() | 如果非null, 返回其中T的值; 否则, 抛出IllegalStateException. |
T or(T) | 参数是默认值, 如果非null, 就返回Optional的值, 否则就返回默认值 |
T orNull() | 如果非null, 返回Optional的值, 否则, 返回null. 和fromNullable方法操作相反. |
Set<T> asSet() | 如果非null, 返回一个包含Optional值的不可变Set, 否则返回空的Set. |
package optional; import com.google.common.base.Objects; import com.google.common.base.Optional; public class OptionalDemo { public static void main(String[] args) { Optional<Integer> possible = Optional.<Integer>of(5); if (possible.isPresent()) // true System.out.println(possible.get()); // 5 System.out.println(possible.asSet()); // [5] Optional<Integer> absentValue = Optional.<Integer>absent(); if (absentValue.isPresent()) // false System.out.println(absentValue.get()); System.out.println(absentValue.or(-1)); // -1 System.out.println(absentValue.orNull()); // null System.out.println(absentValue.asSet()); // [] // absent is not null System.out.println(Objects.firstNonNull(possible, absentValue)); // Optional.of(5) System.out.println(Objects.firstNonNull(absentValue, possible)); // Optional.absent() System.out.println(Objects.firstNonNull(absentValue, absentValue)); // Optional.absent() Optional<Integer> nullValue = Optional.<Integer>fromNullable(null); if (nullValue.isPresent()) // false System.out.println(nullValue.get()); System.out.println(nullValue.or(-1)); // -1 System.out.println(nullValue.orNull()); // null System.out.println(nullValue.asSet()); // [] System.out.println(Objects.firstNonNull(possible, nullValue)); // Optional.of(5) System.out.println(Objects.firstNonNull(nullValue, possible)); // Optional.absent() System.out.println(Objects.firstNonNull(nullValue, nullValue)); // Optional.absent() System.out.println(Objects.firstNonNull(null, 1)); // 1 System.out.println(Objects.firstNonNull(1, null)); // 1 System.out.println(Objects.firstNonNull(null, null)); // cause a java.lang.NullPointerException } }
这个里面有一些类似函数式编程的风格, 不使用null, 而又具备null的含义.
参考资料:
官方wiki: http://code.google.com/p/guava-libraries/wiki/GuavaExplained
Using and avoiding null: http://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained
相关文章推荐
- [Guava官方文档翻译] 2.使用和避免使用null (Using And Avoiding Null Explained)
- Guava学习笔记:Optional优雅的使用null
- Guava学习笔记:Optional优雅的使用null
- Guava学习笔记:Optional优雅的使用null
- Guava学习笔记:Optional优雅的使用null
- Guava学习笔记(1):Optional优雅的使用null
- Guava学习笔记:Optional优雅的使用null
- WPF and Silverlight 学习笔记(二十八):基本图形的使用(3)图形的操作
- Guava Preconditions类使用【Guava学习笔记】
- guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用
- 汇编学习笔记-一段安全的空间及段前缀的使用.
- guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用
- [转]WPF and Silverlight 学习笔记(二十五):使用CollectionView实现对绑定数据的排序、筛选、分组
- guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用
- 【NDN安全】Interest Flooding Attack and Countermeasures in Named Data Networking 学习笔记
- Android 学习笔记之AndBase框架学习(四) 使用封装好的函数实现单,多线程任务
- Git使用笔记(四)Using Repo and Git
- guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁
- 【Google Guava 学习笔记 二】关于Optional,避免null对你的摧残
- 菜鸟学习笔记:java中使用Integer.getInteger(String)有java.lang.NullPointerException