Optional 是一个不可变对象,用来包含一个非null对象。Optional使用absent来表达null值。该类提供了不少实用的方法来处理值是否可用,从而避免对null值进行检查。git
如下是com.google.common.base.Optional<T>
类的声明:github
@GwtCompatible(serializable=true) public abstract class Optional<T> extends Object implements Serializable
static <T> Optional<T> absent()编辑器
建立一个引用缺失的Optional实例。
abstract Set<T> asSet()this
返回一个不可变的单例的set集合,若是引用存在,则返回一个只包含一个元素的set集合,不然,返回一个空的不可变set集合。
abstract boolean equals(Object object)google
该equals对象属于Optional类的方法,比较的规则以下: 1.若给予的对象不是Optional及其子类,直接返回false 2.给予的对象是Optional及其子类,则比较改optional中所包含的引用的值。
static <T> Optional<T> fromNullable(T nullableReference)翻译
建立一个指定引用的Optional,若引用为null则表示引用缺失,并返回一个absent()①。
abstract T get()code
返回包含的实例,该实例必须存在,若是不存在将会抛出java.lang.IllegalStateException异常。
abstract int hashCode()orm
返回该实例的哈希码。
abstract boolean isPresent()htm
若是Optional包含非null引用实例则返回true。
static <T> Optional<T> of(T reference)
建立一个指定引用的Optional实例,若引用为null则快速失败。
abstract Optional<T> or(Optional<? extends T> secondChoice)
Returns this Optional if it has a value present; secondChoice otherwise.
abstract T or(Supplier<? extends T> supplier)
Returns the contained instance if it is present; supplier.get() otherwise.
abstract T or(T defaultValue)
若是包含的实例存在,则返回,若是不存在则返回给予的默认值。
abstract T orNull()
若是包含的实例存在则返回该实例,若是不存在则返回null。
static <T> Iterable<T> presentInstances(Iterable<? extends Optional<? extends T>>optionals)
Returns the value of each present instance from the supplied optionals,in order, skipping over occurrences of absent().
abstract String toString()
返回实例的字符串表示,默认实现只有两种表示方法若Optional中包含的引用缺失则返回optional.absent()不然返回optional.of(引用的值)
abstract <V> Optional<V> transform(Function<? super T,V> function)
If the instance is present, it is transformed with the given Function; otherwise, absent() is returned.
该类所继承的方法来自类Object:
java.lang.Object
使用任何编辑器建立一下程序:
GuavaTester.java
import com.google.common.base.Optional; public class GuavaTester { public static void main(String args[]) { GuavaTester guavaTester = new GuavaTester(); Integer value1 = null; Integer value2 = new Integer(10); //Optional.fromNullable - allows passed parameter to be null. Optional<Integer> a = Optional.fromNullable(value1); //Optional.of - throws NullPointerException if passed parameter is null Optional<Integer> b = Optional.of(value2); System.out.println(guavaTester.sum(a,b)); } public Integer sum(Optional<Integer> a, Optional<Integer> b) { //Optional.isPresent - checks the value is present or not System.out.println("First parameter is present: " + a.isPresent()); System.out.println("Second parameter is present: " + b.isPresent()); //Optional.or - returns the value if present otherwise returns //the default value passed. Integer value1 = a.or(new Integer(0)); //Optional.get - gets the value, value should be present Integer value2 = b.get(); return value1 + value2; } }
在控制台使用 javac
命令编译,编译结果以下:
C:\Guava>javac GuavaTester.java Now run the GuavaTester to see the result. C:\Guava>java GuavaTester See the result.
First parameter is present: false Second parameter is present: true 10
注①:请看一下来自Guava的部分代码
public static Optional fromNullable(Object nullableReference) { return ((Optional) (nullableReference != null ? new Present(nullableReference) : absent())); }
抽象类Optional只有两个实现类Present和absent,这两个类分别表示存在以及缺失状态。调用fromNullable方法
而且指定的引用为null的时候,会调用absent方法,来生成Optional对象,实际上与Optional.absent()一致。
说明:有三个方法没有做解释,主要是担忧相关知识不理解,容易作出错误的翻译,望请谅解!