您的位置:首页 > 其它

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.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.
下面是一个例子, 展示其最基本的使用. 使用Optional可以避免使用null, 而又可以具有null不存在的含义.

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: