8164934: Optional.map() javadoc code example

Reviewed-by: forax, chegar
This commit is contained in:
Paul Sandoz 2016-11-09 10:36:32 -08:00
parent d0b96b7e6a
commit 9555cf3b50

View file

@ -214,20 +214,20 @@ public final class Optional<T> {
* @apiNote
* This method supports post-processing on {@code Optional} values, without
* the need to explicitly check for a return status. For example, the
* following code traverses a stream of file names, selects one that has not
* yet been processed, and then opens that file, returning an
* {@code Optional<FileInputStream>}:
* following code traverses a stream of URIs, selects one that has not
* yet been processed, and creates a path from that URI, returning
* an {@code Optional<Path>}:
*
* <pre>{@code
* Optional<FileInputStream> fis =
* names.stream().filter(name -> !isProcessedYet(name))
* Optional<Path> p =
* uris.stream().filter(uri -> !isProcessedYet(uri))
* .findFirst()
* .map(name -> new FileInputStream(name));
* .map(Paths::get);
* }</pre>
*
* Here, {@code findFirst} returns an {@code Optional<String>}, and then
* {@code map} returns an {@code Optional<FileInputStream>} for the desired
* file if one exists.
* Here, {@code findFirst} returns an {@code Optional<URI>}, and then
* {@code map} returns an {@code Optional<Path>} for the desired
* URI if one exists.
*
* @param mapper the mapping function to apply to a value, if present
* @param <U> The type of the value returned from the mapping function