/**
* Common instance for {@code empty()}.
*/
private static final Optional<?> EMPTY = new Optional<>();
/**
* If non-null, the value; if null, indicates no value is present
*/
private final T value;
/**
* Constructs an empty instance.
*
* @implNote Generally only one empty instance, {@link Optional#EMPTY},
* should exist per VM.
*/
private Optional() {
this.value = null;
}
/**
* Constructs an instance with the value present.
*
* @param value the non-null value to be present
* @throws NullPointerException if value is null
*/
private Optional(T value) {
this.value = Objects.requireNonNull(value);
}
分别是创建一个空实例和构造一个具有当前值的实例。
2.3.2 创建方法 1)源码
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY;
return t;
}
public static <T> Optional<T> of(T value) {
return new Optional<>(value);
}
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
public boolean isPresent() {
return value != null;
}
public void ifPresent(Consumer<? super T> consumer) {
if (value != null)
consumer.accept(value);
}
public T orElse(T other) {
return value != null ? value : other;
}
public T orElseGet(Supplier<? extends T> other) {
return value != null ? value : other.get();
}
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if (value != null) {
return value;
} else {
throw exceptionSupplier.get();
}
}
public static void main(String[] args) {
Integer value1 = null;
Integer value2 = 1;
Optional<Integer> optional1 = Optional.ofNullable(value1);
Optional<Integer> optional2 = Optional.of(value2);
try {
if(optional1.isPresent()){
System.out.println("optional1的isPresent结果不为空");
}else{
System.out.println("optional1的isPresent结果为空");
}
}catch (Exception e){
System.out.println("optional1的isPresent判空失败,原因:"+e.getMessage());
}
try {
if(optional2.isPresent()){
System.out.println("optional2的isPresent结果不为空");
}else{
System.out.println("optional2的isPresent结果为空");
}
}catch (Exception e){
System.out.println("optional2的isPresent判空失败,原因:"+e.getMessage());
}
optional1.ifPresent(t->{
int i =t+1;
System.out.println("optional1处理后的值是"+i);
});
optional2.ifPresent(t->{
int i =t+1;
System.out.println("optional2处理后的值是"+i);});
Integer value3 = 2;
Integer result = optional1.orElse(value3);
System.out.println("optional1执行orElse处理后的值是"+result);
result = optional2.orElse(value3);
System.out.println("optional2执行orElse处理后的值是"+result);
result = optional1.orElseGet(()-> new Integer(-1));
System.out.println("optional1执行orElseGet处理后的值是"+result);
result = optional2.orElseGet(()-> new Integer(-1));
System.out.println("optional2执行orElseGet处理后的值是"+result);
try {
result = optional1.orElseThrow (()-> new RuntimeException("值是空的"));
System.out.println("optional1执行orElseThrow处理后的值是"+result);
}catch (Exception e){
System.out.println("optional1的orElseThrow抛出异常:"+e.getMessage());
}
try {
result = optional2.orElseThrow (()-> new RuntimeException("值是空的"));
System.out.println("optional2执行orElseThrow处理后的值是"+result);
}catch (Exception e){
System.out.println("optional2的orElseThrow抛出异常:"+e.getMessage());
4)运行结果
2.3.5 过滤方法 1)源码
public Optional<T> filter(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate);
if (!isPresent())
return this;
else
return predicate.test(value) ? this : empty();
}