jdk/src/java.base/share/classes/java/util/Optional.java
changeset 28754 8fe59917548d
parent 25859 3317bb8137f4
child 28963 8498cdb7c54b
equal deleted inserted replaced
28753:764648da0e46 28754:8fe59917548d
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    26 
    26 
    27 import java.util.function.Consumer;
    27 import java.util.function.Consumer;
    28 import java.util.function.Function;
    28 import java.util.function.Function;
    29 import java.util.function.Predicate;
    29 import java.util.function.Predicate;
    30 import java.util.function.Supplier;
    30 import java.util.function.Supplier;
       
    31 import java.util.stream.Stream;
    31 
    32 
    32 /**
    33 /**
    33  * A container object which may or may not contain a non-null value.
    34  * A container object which may or may not contain a non-null value.
    34  * If a value is present, {@code isPresent()} will return {@code true} and
    35  * If a value is present, {@code isPresent()} will return {@code true} and
    35  * {@code get()} will return the value.
    36  * {@code get()} will return the value.
   153      * @param consumer block to be executed if a value is present
   154      * @param consumer block to be executed if a value is present
   154      * @throws NullPointerException if value is present and {@code consumer} is
   155      * @throws NullPointerException if value is present and {@code consumer} is
   155      * null
   156      * null
   156      */
   157      */
   157     public void ifPresent(Consumer<? super T> consumer) {
   158     public void ifPresent(Consumer<? super T> consumer) {
   158         if (value != null)
   159         if (value != null) {
   159             consumer.accept(value);
   160             consumer.accept(value);
       
   161         }
   160     }
   162     }
   161 
   163 
   162     /**
   164     /**
   163      * If a value is present, and the value matches the given predicate,
   165      * If a value is present, and the value matches the given predicate,
   164      * return an {@code Optional} describing the value, otherwise return an
   166      * return an {@code Optional} describing the value, otherwise return an
   170      * otherwise an empty {@code Optional}
   172      * otherwise an empty {@code Optional}
   171      * @throws NullPointerException if the predicate is null
   173      * @throws NullPointerException if the predicate is null
   172      */
   174      */
   173     public Optional<T> filter(Predicate<? super T> predicate) {
   175     public Optional<T> filter(Predicate<? super T> predicate) {
   174         Objects.requireNonNull(predicate);
   176         Objects.requireNonNull(predicate);
   175         if (!isPresent())
   177         if (!isPresent()) {
   176             return this;
   178             return this;
   177         else
   179         } else {
   178             return predicate.test(value) ? this : empty();
   180             return predicate.test(value) ? this : empty();
       
   181         }
   179     }
   182     }
   180 
   183 
   181     /**
   184     /**
   182      * If a value is present, apply the provided mapping function to it,
   185      * If a value is present, apply the provided mapping function to it,
   183      * and if the result is non-null, return an {@code Optional} describing the
   186      * and if the result is non-null, return an {@code Optional} describing the
   207      * otherwise an empty {@code Optional}
   210      * otherwise an empty {@code Optional}
   208      * @throws NullPointerException if the mapping function is null
   211      * @throws NullPointerException if the mapping function is null
   209      */
   212      */
   210     public<U> Optional<U> map(Function<? super T, ? extends U> mapper) {
   213     public<U> Optional<U> map(Function<? super T, ? extends U> mapper) {
   211         Objects.requireNonNull(mapper);
   214         Objects.requireNonNull(mapper);
   212         if (!isPresent())
   215         if (!isPresent()) {
   213             return empty();
   216             return empty();
   214         else {
   217         } else {
   215             return Optional.ofNullable(mapper.apply(value));
   218             return Optional.ofNullable(mapper.apply(value));
   216         }
   219         }
   217     }
   220     }
   218 
   221 
   219     /**
   222     /**
   233      * @throws NullPointerException if the mapping function is null or returns
   236      * @throws NullPointerException if the mapping function is null or returns
   234      * a null result
   237      * a null result
   235      */
   238      */
   236     public<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) {
   239     public<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) {
   237         Objects.requireNonNull(mapper);
   240         Objects.requireNonNull(mapper);
   238         if (!isPresent())
   241         if (!isPresent()) {
   239             return empty();
   242             return empty();
   240         else {
   243         } else {
   241             return Objects.requireNonNull(mapper.apply(value));
   244             return Objects.requireNonNull(mapper.apply(value));
       
   245         }
       
   246     }
       
   247 
       
   248     /**
       
   249      * If a value is present return a sequential {@link Stream} containing only
       
   250      * that value, otherwise return an empty {@code Stream}.
       
   251      *
       
   252      * @apiNote This method can be used to transform a {@code Stream} of
       
   253      * optional elements to a {@code Stream} of present value elements:
       
   254      *
       
   255      * <pre>{@code
       
   256      *     Stream<Optional<T>> os = ..
       
   257      *     Stream<T> s = os.flatMap(Optional::stream)
       
   258      * }</pre>
       
   259      *
       
   260      * @return the optional value as a {@code Stream}
       
   261      * @since 1.9
       
   262      */
       
   263     public Stream<T> stream() {
       
   264         if (!isPresent()) {
       
   265             return Stream.empty();
       
   266         } else {
       
   267             return Stream.of(value);
   242         }
   268         }
   243     }
   269     }
   244 
   270 
   245     /**
   271     /**
   246      * Return the value if present, otherwise return {@code other}.
   272      * Return the value if present, otherwise return {@code other}.