< prev index next >

src/java.base/share/classes/java/util/OptionalLong.java

Print this page
rev 11381 : 8071670: java.util.Optional: please add a way to specify if-else behavior
Reviewed-by: dfuchs


  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.util;
  26 
  27 import java.util.function.LongConsumer;
  28 import java.util.function.LongSupplier;
  29 import java.util.function.Supplier;
  30 import java.util.stream.LongStream;
  31 
  32 /**
  33  * A container object which may or may not contain a {@code long} value.
  34  * If a value is present, {@code isPresent()} will return {@code true} and
  35  * {@code getAsLong()} will return the value.
  36  *
  37  * <p>Additional methods that depend on the presence or absence of a contained
  38  * value are provided, such as {@link #orElse(long) orElse()}
  39  * (return a default value if value not present) and
  40  * {@link #ifPresent(java.util.function.LongConsumer) ifPresent()} (execute a block
  41  * of code if the value is present).
  42  *
  43  * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
  44  * class; use of identity-sensitive operations (including reference equality
  45  * ({@code ==}), identity hash code, or synchronization) on instances of
  46  * {@code OptionalLong} may have unpredictable results and should be avoided.
  47  *
  48  * @since 1.8
  49  */
  50 public final class OptionalLong {
  51     /**
  52      * Common instance for {@code empty()}.
  53      */
  54     private static final OptionalLong EMPTY = new OptionalLong();
  55 
  56     /**
  57      * If true then the value is present, otherwise indicates no value is present
  58      */
  59     private final boolean isPresent;
  60     private final long value;
  61 


 114      *
 115      * @see OptionalLong#isPresent()
 116      */
 117     public long getAsLong() {
 118         if (!isPresent) {
 119             throw new NoSuchElementException("No value present");
 120         }
 121         return value;
 122     }
 123 
 124     /**
 125      * Return {@code true} if there is a value present, otherwise {@code false}.
 126      *
 127      * @return {@code true} if there is a value present, otherwise {@code false}
 128      */
 129     public boolean isPresent() {
 130         return isPresent;
 131     }
 132 
 133     /**
 134      * Have the specified consumer accept the value if a value is present,
 135      * otherwise do nothing.
 136      *
 137      * @param consumer block to be executed if a value is present
 138      * @throws NullPointerException if value is present and {@code consumer} is
 139      * null
 140      */
 141     public void ifPresent(LongConsumer consumer) {
 142         if (isPresent) {
 143             consumer.accept(value);



















 144         }
 145     }
 146 
 147     /**
 148      * If a value is present return a sequential {@link LongStream} containing
 149      * only that value, otherwise return an empty {@code LongStream}.
 150      *
 151      * @apiNote This method can be used to transform a {@code Stream} of
 152      * optional longs to a {@code LongStream} of present longs:
 153      *
 154      * <pre>{@code
 155      *     Stream<OptionalLong> os = ..
 156      *     LongStream s = os.flatMapToLong(OptionalLong::stream)
 157      * }</pre>
 158      *
 159      * @return the optional value as a {@code LongStream}
 160      * @since 1.9
 161      */
 162     public LongStream stream() {
 163         if (isPresent) {




  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.util;
  26 
  27 import java.util.function.LongConsumer;
  28 import java.util.function.LongSupplier;
  29 import java.util.function.Supplier;
  30 import java.util.stream.LongStream;
  31 
  32 /**
  33  * A container object which may or may not contain a {@code long} value.
  34  * If a value is present, {@code isPresent()} will return {@code true} and
  35  * {@code getAsLong()} will return the value.
  36  *
  37  * <p>Additional methods that depend on the presence or absence of a contained
  38  * value are provided, such as {@link #orElse(long) orElse()}
  39  * (return a default value if value not present) and
  40  * {@link #ifPresent(java.util.function.LongConsumer) ifPresent()} (perform an
  41  * action if the value is present).
  42  *
  43  * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
  44  * class; use of identity-sensitive operations (including reference equality
  45  * ({@code ==}), identity hash code, or synchronization) on instances of
  46  * {@code OptionalLong} may have unpredictable results and should be avoided.
  47  *
  48  * @since 1.8
  49  */
  50 public final class OptionalLong {
  51     /**
  52      * Common instance for {@code empty()}.
  53      */
  54     private static final OptionalLong EMPTY = new OptionalLong();
  55 
  56     /**
  57      * If true then the value is present, otherwise indicates no value is present
  58      */
  59     private final boolean isPresent;
  60     private final long value;
  61 


 114      *
 115      * @see OptionalLong#isPresent()
 116      */
 117     public long getAsLong() {
 118         if (!isPresent) {
 119             throw new NoSuchElementException("No value present");
 120         }
 121         return value;
 122     }
 123 
 124     /**
 125      * Return {@code true} if there is a value present, otherwise {@code false}.
 126      *
 127      * @return {@code true} if there is a value present, otherwise {@code false}
 128      */
 129     public boolean isPresent() {
 130         return isPresent;
 131     }
 132 
 133     /**
 134      * If a value is present, perform the given action with the value,
 135      * otherwise do nothing.
 136      *
 137      * @param action the action to be performed if a value is present
 138      * @throws NullPointerException if a value is present and {@code action} is
 139      * null
 140      */
 141     public void ifPresent(LongConsumer action) {
 142         if (isPresent) {
 143             action.accept(value);
 144         }
 145     }
 146 
 147     /**
 148      * If a value is present, perform the given action with the value,
 149      * otherwise perform the given empty-based action.
 150      *
 151      * @param action the action to be performed if a value is present
 152      * @param emptyAction the empty-based action to be performed if a value is
 153      * not present
 154      * @throws NullPointerException if a value is present and {@code action} is
 155      * null, or a value is not present and {@code emptyAction} is null.
 156      * @since 1.9
 157      */
 158     public void ifPresentOrElse(LongConsumer action, Runnable emptyAction) {
 159         if (isPresent) {
 160             action.accept(value);
 161         } else {
 162             emptyAction.run();
 163         }
 164     }
 165 
 166     /**
 167      * If a value is present return a sequential {@link LongStream} containing
 168      * only that value, otherwise return an empty {@code LongStream}.
 169      *
 170      * @apiNote This method can be used to transform a {@code Stream} of
 171      * optional longs to a {@code LongStream} of present longs:
 172      *
 173      * <pre>{@code
 174      *     Stream<OptionalLong> os = ..
 175      *     LongStream s = os.flatMapToLong(OptionalLong::stream)
 176      * }</pre>
 177      *
 178      * @return the optional value as a {@code LongStream}
 179      * @since 1.9
 180      */
 181     public LongStream stream() {
 182         if (isPresent) {


< prev index next >