mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
Reviewed-by: alundblad, forax, chegar, jrose
This commit is contained in:
parent
3b9021981f
commit
dd21d2c4db
8 changed files with 225 additions and 42 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -28,6 +28,7 @@ import java.util.function.Consumer;
|
|||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* A container object which may or may not contain a non-null value.
|
||||
|
@ -155,8 +156,9 @@ public final class Optional<T> {
|
|||
* null
|
||||
*/
|
||||
public void ifPresent(Consumer<? super T> consumer) {
|
||||
if (value != null)
|
||||
if (value != null) {
|
||||
consumer.accept(value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -172,10 +174,11 @@ public final class Optional<T> {
|
|||
*/
|
||||
public Optional<T> filter(Predicate<? super T> predicate) {
|
||||
Objects.requireNonNull(predicate);
|
||||
if (!isPresent())
|
||||
if (!isPresent()) {
|
||||
return this;
|
||||
else
|
||||
} else {
|
||||
return predicate.test(value) ? this : empty();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -209,9 +212,9 @@ public final class Optional<T> {
|
|||
*/
|
||||
public<U> Optional<U> map(Function<? super T, ? extends U> mapper) {
|
||||
Objects.requireNonNull(mapper);
|
||||
if (!isPresent())
|
||||
if (!isPresent()) {
|
||||
return empty();
|
||||
else {
|
||||
} else {
|
||||
return Optional.ofNullable(mapper.apply(value));
|
||||
}
|
||||
}
|
||||
|
@ -235,13 +238,36 @@ public final class Optional<T> {
|
|||
*/
|
||||
public<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) {
|
||||
Objects.requireNonNull(mapper);
|
||||
if (!isPresent())
|
||||
if (!isPresent()) {
|
||||
return empty();
|
||||
else {
|
||||
} else {
|
||||
return Objects.requireNonNull(mapper.apply(value));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If a value is present return a sequential {@link Stream} containing only
|
||||
* that value, otherwise return an empty {@code Stream}.
|
||||
*
|
||||
* @apiNote This method can be used to transform a {@code Stream} of
|
||||
* optional elements to a {@code Stream} of present value elements:
|
||||
*
|
||||
* <pre>{@code
|
||||
* Stream<Optional<T>> os = ..
|
||||
* Stream<T> s = os.flatMap(Optional::stream)
|
||||
* }</pre>
|
||||
*
|
||||
* @return the optional value as a {@code Stream}
|
||||
* @since 1.9
|
||||
*/
|
||||
public Stream<T> stream() {
|
||||
if (!isPresent()) {
|
||||
return Stream.empty();
|
||||
} else {
|
||||
return Stream.of(value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value if present, otherwise return {@code other}.
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue