jdk/src/java.base/share/classes/java/util/OptionalDouble.java
author mchung
Fri, 22 May 2015 16:43:39 -0700
changeset 30789 9eca83469588
parent 29599 e2c48b046f91
child 33238 80720cbea36f
permissions -rw-r--r--
8074431: Remove native2ascii tool Reviewed-by: erikj, alanb, okutsu, mfang, naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     1
/*
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
     2
 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     4
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    10
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    15
 * accompanied this code).
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    16
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    20
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    23
 * questions.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    24
 */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    25
package java.util;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    26
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    27
import java.util.function.DoubleConsumer;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    28
import java.util.function.DoubleSupplier;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    29
import java.util.function.Supplier;
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
    30
import java.util.stream.DoubleStream;
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    31
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    32
/**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    33
 * A container object which may or may not contain a {@code double} value.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    34
 * If a value is present, {@code isPresent()} will return {@code true} and
21966
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents: 18819
diff changeset
    35
 * {@code getAsDouble()} will return the value.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    36
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    37
 * <p>Additional methods that depend on the presence or absence of a contained
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    38
 * value are provided, such as {@link #orElse(double) orElse()}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    39
 * (return a default value if value not present) and
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    40
 * {@link #ifPresent(java.util.function.DoubleConsumer) ifPresent()} (perform an
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    41
 * action if the value is present).
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    42
 *
21966
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents: 18819
diff changeset
    43
 * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents: 18819
diff changeset
    44
 * class; use of identity-sensitive operations (including reference equality
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents: 18819
diff changeset
    45
 * ({@code ==}), identity hash code, or synchronization) on instances of
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents: 18819
diff changeset
    46
 * {@code OptionalDouble} may have unpredictable results and should be avoided.
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents: 18819
diff changeset
    47
 *
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    48
 * @since 1.8
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    49
 */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    50
public final class OptionalDouble {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    51
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    52
     * Common instance for {@code empty()}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    53
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    54
    private static final OptionalDouble EMPTY = new OptionalDouble();
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    55
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    56
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    57
     * If true then the value is present, otherwise indicates no value is present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    58
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    59
    private final boolean isPresent;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    60
    private final double value;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    61
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    62
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    63
     * Construct an empty instance.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    64
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    65
     * @implNote generally only one empty instance, {@link OptionalDouble#EMPTY},
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    66
     * should exist per VM.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    67
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    68
    private OptionalDouble() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    69
        this.isPresent = false;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    70
        this.value = Double.NaN;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    71
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    72
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    73
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    74
     * Returns an empty {@code OptionalDouble} instance.  No value is present for this
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    75
     * OptionalDouble.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    76
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    77
     * @apiNote Though it may be tempting to do so, avoid testing if an object
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    78
     * is empty by comparing with {@code ==} against instances returned by
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    79
     * {@code Option.empty()}. There is no guarantee that it is a singleton.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    80
     * Instead, use {@link #isPresent()}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    81
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    82
     *  @return an empty {@code OptionalDouble}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    83
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    84
    public static OptionalDouble empty() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    85
        return EMPTY;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    86
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    87
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    88
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    89
     * Construct an instance with the value present.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    90
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    91
     * @param value the double value to be present.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    92
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    93
    private OptionalDouble(double value) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    94
        this.isPresent = true;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    95
        this.value = value;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    96
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    97
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    98
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    99
     * Return an {@code OptionalDouble} with the specified value present.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   100
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   101
     * @param value the value to be present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   102
     * @return an {@code OptionalDouble} with the value present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   103
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   104
    public static OptionalDouble of(double value) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   105
        return new OptionalDouble(value);
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   106
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   107
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   108
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   109
     * If a value is present in this {@code OptionalDouble}, returns the value,
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   110
     * otherwise throws {@code NoSuchElementException}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   111
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   112
     * @return the value held by this {@code OptionalDouble}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   113
     * @throws NoSuchElementException if there is no value present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   114
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   115
     * @see OptionalDouble#isPresent()
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   116
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   117
    public double getAsDouble() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   118
        if (!isPresent) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   119
            throw new NoSuchElementException("No value present");
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   120
        }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   121
        return value;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   122
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   123
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   124
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   125
     * Return {@code true} if there is a value present, otherwise {@code false}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   126
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   127
     * @return {@code true} if there is a value present, otherwise {@code false}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   128
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   129
    public boolean isPresent() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   130
        return isPresent;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   131
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   132
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   133
    /**
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   134
     * If a value is present, perform the given action with the value,
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   135
     * otherwise do nothing.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   136
     *
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   137
     * @param action the action to be performed if a value is present
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   138
     * @throws NullPointerException if a value is present and {@code action} is
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   139
     * null
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   140
     */
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   141
    public void ifPresent(DoubleConsumer action) {
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   142
        if (isPresent) {
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   143
            action.accept(value);
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   144
        }
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   145
    }
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   146
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   147
    /**
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   148
     * If a value is present, perform the given action with the value,
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   149
     * otherwise perform the given empty-based action.
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   150
     *
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   151
     * @param action the action to be performed if a value is present
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   152
     * @param emptyAction the empty-based action to be performed if a value is
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   153
     * not present
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   154
     * @throws NullPointerException if a value is present and {@code action} is
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   155
     * null, or a value is not present and {@code emptyAction} is null.
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   156
     * @since 1.9
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   157
     */
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   158
    public void ifPresentOrElse(DoubleConsumer action, Runnable emptyAction) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   159
        if (isPresent) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   160
            action.accept(value);
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   161
        } else {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   162
            emptyAction.run();
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   163
        }
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   164
    }
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   165
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   166
    /**
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   167
     * If a value is present return a sequential {@link DoubleStream} containing
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   168
     * only that value, otherwise return an empty {@code DoubleStream}.
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   169
     *
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   170
     * @apiNote This method can be used to transform a {@code Stream} of
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   171
     * optional doubles to a {@code DoubleStream} of present doubles:
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   172
     *
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   173
     * <pre>{@code
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   174
     *     Stream<OptionalDouble> os = ..
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   175
     *     DoubleStream s = os.flatMapToDouble(OptionalDouble::stream)
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   176
     * }</pre>
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   177
     *
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   178
     * @return the optional value as a {@code DoubleStream}
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   179
     * @since 1.9
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   180
     */
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   181
    public DoubleStream stream() {
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   182
        if (isPresent) {
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   183
            return DoubleStream.of(value);
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   184
        } else {
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   185
            return DoubleStream.empty();
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 28753
diff changeset
   186
        }
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   187
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   188
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   189
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   190
     * Return the value if present, otherwise return {@code other}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   191
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   192
     * @param other the value to be returned if there is no value present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   193
     * @return the value, if present, otherwise {@code other}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   194
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   195
    public double orElse(double other) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   196
        return isPresent ? value : other;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   197
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   198
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   199
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   200
     * Return the value if present, otherwise invoke {@code other} and return
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   201
     * the result of that invocation.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   202
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   203
     * @param other a {@code DoubleSupplier} whose result is returned if no value
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   204
     * is present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   205
     * @return the value if present otherwise the result of {@code other.getAsDouble()}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   206
     * @throws NullPointerException if value is not present and {@code other} is
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   207
     * null
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   208
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   209
    public double orElseGet(DoubleSupplier other) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   210
        return isPresent ? value : other.getAsDouble();
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   211
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   212
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   213
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   214
     * Return the contained value, if present, otherwise throw an exception
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   215
     * to be created by the provided supplier.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   216
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   217
     * @apiNote A method reference to the exception constructor with an empty
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   218
     * argument list can be used as the supplier. For example,
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   219
     * {@code IllegalStateException::new}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   220
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   221
     * @param <X> Type of the exception to be thrown
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   222
     * @param exceptionSupplier The supplier which will return the exception to
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   223
     * be thrown
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   224
     * @return the present value
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   225
     * @throws X if there is no value present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   226
     * @throws NullPointerException if no value is present and
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   227
     * {@code exceptionSupplier} is null
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   228
     */
28753
764648da0e46 8059324: orElseThrow has different signatures for OptionalPrimitive and Optional<T>
psandoz
parents: 25859
diff changeset
   229
    public<X extends Throwable> double orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   230
        if (isPresent) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   231
            return value;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   232
        } else {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   233
            throw exceptionSupplier.get();
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   234
        }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   235
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   236
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   237
    /**
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   238
     * Indicates whether some other object is "equal to" this OptionalDouble. The
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   239
     * other object is considered equal if:
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   240
     * <ul>
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   241
     * <li>it is also an {@code OptionalDouble} and;
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   242
     * <li>both instances have no value present or;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   243
     * <li>the present values are "equal to" each other via {@code Double.compare() == 0}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   244
     * </ul>
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   245
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   246
     * @param obj an object to be tested for equality
29599
e2c48b046f91 8075560: Typo in Javadoc for java.util.Optional.equals()
psandoz
parents: 28963
diff changeset
   247
     * @return {@code true} if the other object is "equal to" this object
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   248
     * otherwise {@code false}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   249
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   250
    @Override
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   251
    public boolean equals(Object obj) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   252
        if (this == obj) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   253
            return true;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   254
        }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   255
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   256
        if (!(obj instanceof OptionalDouble)) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   257
            return false;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   258
        }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   259
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   260
        OptionalDouble other = (OptionalDouble) obj;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   261
        return (isPresent && other.isPresent)
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   262
               ? Double.compare(value, other.value) == 0
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   263
               : isPresent == other.isPresent;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   264
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   265
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   266
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   267
     * Returns the hash code value of the present value, if any, or 0 (zero) if
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   268
     * no value is present.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   269
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   270
     * @return hash code value of the present value or 0 if no value is present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   271
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   272
    @Override
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   273
    public int hashCode() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   274
        return isPresent ? Double.hashCode(value) : 0;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   275
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   276
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   277
    /**
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   278
     * {@inheritDoc}
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   279
     *
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   280
     * Returns a non-empty string representation of this object suitable for
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   281
     * debugging. The exact presentation format is unspecified and may vary
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   282
     * between implementations and versions.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   283
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   284
     * @implSpec If a value is present the result must include its string
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   285
     * representation in the result. Empty and present instances must be
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   286
     * unambiguously differentiable.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   287
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   288
     * @return the string representation of this instance
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   289
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   290
    @Override
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   291
    public String toString() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   292
        return isPresent
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   293
                ? String.format("OptionalDouble[%s]", value)
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   294
                : "OptionalDouble.empty";
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   295
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   296
}