src/java.base/share/classes/java/util/Optional.java
author weijun
Wed, 06 Dec 2017 21:17:19 +0800
changeset 49774 5950773b665c
parent 48328 7acf5700d542
child 49823 af4b57a556be
permissions -rw-r--r--
8189977: Improve permission portability Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     1
/*
44779
2a39971d67ab 8167981: Optional: add notes explaining intended use
smarks
parents: 41963
diff changeset
     2
 * Copyright (c) 2012, 2017, 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.Consumer;
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
    28
import java.util.function.Function;
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
    29
import java.util.function.Predicate;
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    30
import java.util.function.Supplier;
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
    31
import java.util.stream.Stream;
16489
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
/**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
    34
 * A container object which may or may not contain a non-{@code null} value.
48328
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
    35
 * If a value is present, {@code isPresent()} returns {@code true}. If no
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
    36
 * value is present, the object is considered <i>empty</i> and
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
    37
 * {@code isPresent()} returns {@code false}.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    38
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    39
 * <p>Additional methods that depend on the presence or absence of a contained
44779
2a39971d67ab 8167981: Optional: add notes explaining intended use
smarks
parents: 41963
diff changeset
    40
 * value are provided, such as {@link #orElse(Object) orElse()}
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
    41
 * (returns a default value if no value is present) and
44779
2a39971d67ab 8167981: Optional: add notes explaining intended use
smarks
parents: 41963
diff changeset
    42
 * {@link #ifPresent(Consumer) ifPresent()} (performs an
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
    43
 * action if a value is present).
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    44
 *
21966
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents: 20479
diff changeset
    45
 * <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: 20479
diff changeset
    46
 * class; use of identity-sensitive operations (including reference equality
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents: 20479
diff changeset
    47
 * ({@code ==}), identity hash code, or synchronization) on instances of
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents: 20479
diff changeset
    48
 * {@code Optional} may have unpredictable results and should be avoided.
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents: 20479
diff changeset
    49
 *
44779
2a39971d67ab 8167981: Optional: add notes explaining intended use
smarks
parents: 41963
diff changeset
    50
 * @apiNote
2a39971d67ab 8167981: Optional: add notes explaining intended use
smarks
parents: 41963
diff changeset
    51
 * {@code Optional} is primarily intended for use as a method return type where
2a39971d67ab 8167981: Optional: add notes explaining intended use
smarks
parents: 41963
diff changeset
    52
 * there is a clear need to represent "no result," and where using {@code null}
2a39971d67ab 8167981: Optional: add notes explaining intended use
smarks
parents: 41963
diff changeset
    53
 * is likely to cause errors. A variable whose type is {@code Optional} should
2a39971d67ab 8167981: Optional: add notes explaining intended use
smarks
parents: 41963
diff changeset
    54
 * never itself be {@code null}; it should always point to an {@code Optional}
2a39971d67ab 8167981: Optional: add notes explaining intended use
smarks
parents: 41963
diff changeset
    55
 * instance.
2a39971d67ab 8167981: Optional: add notes explaining intended use
smarks
parents: 41963
diff changeset
    56
 *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
    57
 * @param <T> the type of value
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    58
 * @since 1.8
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    59
 */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    60
public final class Optional<T> {
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
     * Common instance for {@code empty()}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    63
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    64
    private static final Optional<?> EMPTY = new Optional<>();
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    65
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    66
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    67
     * If non-null, the value; if null, indicates no value is present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    68
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    69
    private final T value;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    70
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    71
    /**
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
    72
     * Constructs an empty instance.
16489
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
     * @implNote Generally only one empty instance, {@link Optional#EMPTY},
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    75
     * should exist per VM.
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
    private Optional() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    78
        this.value = null;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    79
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    80
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
     * Returns an empty {@code Optional} instance.  No value is present for this
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
    83
     * {@code Optional}.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    84
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
    85
     * @apiNote
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
    86
     * Though it may be tempting to do so, avoid testing if an object is empty
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
    87
     * by comparing with {@code ==} against instances returned by
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
    88
     * {@code Optional.empty()}.  There is no guarantee that it is a singleton.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    89
     * Instead, use {@link #isPresent()}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    90
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
    91
     * @param <T> The type of the non-existent value
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    92
     * @return an empty {@code Optional}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    93
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    94
    public static<T> Optional<T> empty() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    95
        @SuppressWarnings("unchecked")
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    96
        Optional<T> t = (Optional<T>) EMPTY;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    97
        return t;
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
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   100
    /**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   101
     * Constructs an instance with the described value.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   102
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   103
     * @param value the non-{@code null} value to describe
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   104
     * @throws NullPointerException if value is {@code null}
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   105
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   106
    private Optional(T value) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   107
        this.value = Objects.requireNonNull(value);
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
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   110
    /**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   111
     * Returns an {@code Optional} describing the given non-{@code null}
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   112
     * value.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   113
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   114
     * @param value the value to describe, which must be non-{@code null}
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   115
     * @param <T> the type of the value
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   116
     * @return an {@code Optional} with the value present
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   117
     * @throws NullPointerException if value is {@code null}
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   118
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   119
    public static <T> Optional<T> of(T value) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   120
        return new Optional<>(value);
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   121
    }
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
    /**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   124
     * Returns an {@code Optional} describing the given value, if
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   125
     * non-{@code null}, otherwise returns an empty {@code Optional}.
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   126
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   127
     * @param value the possibly-{@code null} value to describe
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   128
     * @param <T> the type of the value
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   129
     * @return an {@code Optional} with a present value if the specified value
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   130
     *         is non-{@code null}, otherwise an empty {@code Optional}
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   131
     */
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   132
    public static <T> Optional<T> ofNullable(T value) {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   133
        return value == null ? empty() : of(value);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   134
    }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   135
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   136
    /**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   137
     * If a value is present, returns the value, otherwise throws
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   138
     * {@code NoSuchElementException}.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   139
     *
44779
2a39971d67ab 8167981: Optional: add notes explaining intended use
smarks
parents: 41963
diff changeset
   140
     * @apiNote
48328
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   141
     * The preferred alternative to this method is {@link #orElseThrow()}.
44779
2a39971d67ab 8167981: Optional: add notes explaining intended use
smarks
parents: 41963
diff changeset
   142
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   143
     * @return the non-{@code null} value described by this {@code Optional}
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   144
     * @throws NoSuchElementException if no value is present
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   145
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   146
    public T get() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   147
        if (value == null) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   148
            throw new NoSuchElementException("No value present");
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   149
        }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   150
        return value;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   151
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   152
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   153
    /**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   154
     * If a value is present, returns {@code true}, otherwise {@code false}.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   155
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   156
     * @return {@code true} if a value is present, otherwise {@code false}
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   157
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   158
    public boolean isPresent() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   159
        return value != null;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   160
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   161
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   162
    /**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   163
     * If a value is present, performs the given action with the value,
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   164
     * otherwise does nothing.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   165
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   166
     * @param action the action to be performed, if a value is present
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   167
     * @throws NullPointerException if value is present and the given action is
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   168
     *         {@code null}
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   169
     */
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   170
    public void ifPresent(Consumer<? super T> action) {
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   171
        if (value != null) {
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   172
            action.accept(value);
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   173
        }
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   174
    }
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   175
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   176
    /**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   177
     * If a value is present, performs the given action with the value,
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   178
     * otherwise performs the given empty-based action.
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   179
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   180
     * @param action the action to be performed, if a value is present
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   181
     * @param emptyAction the empty-based action to be performed, if no value is
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   182
     *        present
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   183
     * @throws NullPointerException if a value is present and the given action
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   184
     *         is {@code null}, or no value is present and the given empty-based
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   185
     *         action is {@code null}.
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   186
     * @since 9
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   187
     */
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   188
    public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   189
        if (value != null) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   190
            action.accept(value);
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   191
        } else {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   192
            emptyAction.run();
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   193
        }
16489
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
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   196
    /**
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   197
     * If a value is present, and the value matches the given predicate,
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   198
     * returns an {@code Optional} describing the value, otherwise returns an
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   199
     * empty {@code Optional}.
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   200
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   201
     * @param predicate the predicate to apply to a value, if present
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   202
     * @return an {@code Optional} describing the value of this
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   203
     *         {@code Optional}, if a value is present and the value matches the
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   204
     *         given predicate, otherwise an empty {@code Optional}
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   205
     * @throws NullPointerException if the predicate is {@code null}
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   206
     */
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   207
    public Optional<T> filter(Predicate<? super T> predicate) {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   208
        Objects.requireNonNull(predicate);
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   209
        if (!isPresent()) {
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   210
            return this;
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   211
        } else {
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   212
            return predicate.test(value) ? this : empty();
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   213
        }
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   214
    }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   215
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   216
    /**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   217
     * If a value is present, returns an {@code Optional} describing (as if by
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   218
     * {@link #ofNullable}) the result of applying the given mapping function to
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   219
     * the value, otherwise returns an empty {@code Optional}.
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   220
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   221
     * <p>If the mapping function returns a {@code null} result then this method
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   222
     * returns an empty {@code Optional}.
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   223
     *
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   224
     * @apiNote
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   225
     * This method supports post-processing on {@code Optional} values, without
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   226
     * the need to explicitly check for a return status.  For example, the
41963
1bcc32e6e324 8164934: Optional.map() javadoc code example
psandoz
parents: 41598
diff changeset
   227
     * following code traverses a stream of URIs, selects one that has not
1bcc32e6e324 8164934: Optional.map() javadoc code example
psandoz
parents: 41598
diff changeset
   228
     * yet been processed, and creates a path from that URI, returning
1bcc32e6e324 8164934: Optional.map() javadoc code example
psandoz
parents: 41598
diff changeset
   229
     * an {@code Optional<Path>}:
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   230
     *
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   231
     * <pre>{@code
41963
1bcc32e6e324 8164934: Optional.map() javadoc code example
psandoz
parents: 41598
diff changeset
   232
     *     Optional<Path> p =
1bcc32e6e324 8164934: Optional.map() javadoc code example
psandoz
parents: 41598
diff changeset
   233
     *         uris.stream().filter(uri -> !isProcessedYet(uri))
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   234
     *                       .findFirst()
41963
1bcc32e6e324 8164934: Optional.map() javadoc code example
psandoz
parents: 41598
diff changeset
   235
     *                       .map(Paths::get);
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   236
     * }</pre>
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   237
     *
41963
1bcc32e6e324 8164934: Optional.map() javadoc code example
psandoz
parents: 41598
diff changeset
   238
     * Here, {@code findFirst} returns an {@code Optional<URI>}, and then
1bcc32e6e324 8164934: Optional.map() javadoc code example
psandoz
parents: 41598
diff changeset
   239
     * {@code map} returns an {@code Optional<Path>} for the desired
1bcc32e6e324 8164934: Optional.map() javadoc code example
psandoz
parents: 41598
diff changeset
   240
     * URI if one exists.
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   241
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   242
     * @param mapper the mapping function to apply to a value, if present
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   243
     * @param <U> The type of the value returned from the mapping function
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   244
     * @return an {@code Optional} describing the result of applying a mapping
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   245
     *         function to the value of this {@code Optional}, if a value is
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   246
     *         present, otherwise an empty {@code Optional}
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   247
     * @throws NullPointerException if the mapping function is {@code null}
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   248
     */
41598
755ef825ca6a 8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents: 33238
diff changeset
   249
    public <U> Optional<U> map(Function<? super T, ? extends U> mapper) {
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   250
        Objects.requireNonNull(mapper);
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   251
        if (!isPresent()) {
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   252
            return empty();
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   253
        } else {
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   254
            return Optional.ofNullable(mapper.apply(value));
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   255
        }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   256
    }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   257
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   258
    /**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   259
     * If a value is present, returns the result of applying the given
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   260
     * {@code Optional}-bearing mapping function to the value, otherwise returns
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   261
     * an empty {@code Optional}.
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   262
     *
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   263
     * <p>This method is similar to {@link #map(Function)}, but the mapping
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   264
     * function is one whose result is already an {@code Optional}, and if
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   265
     * invoked, {@code flatMap} does not wrap it within an additional
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   266
     * {@code Optional}.
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   267
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   268
     * @param <U> The type of value of the {@code Optional} returned by the
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   269
     *            mapping function
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   270
     * @param mapper the mapping function to apply to a value, if present
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   271
     * @return the result of applying an {@code Optional}-bearing mapping
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   272
     *         function to the value of this {@code Optional}, if a value is
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   273
     *         present, otherwise an empty {@code Optional}
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   274
     * @throws NullPointerException if the mapping function is {@code null} or
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   275
     *         returns a {@code null} result
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   276
     */
41598
755ef825ca6a 8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents: 33238
diff changeset
   277
    public <U> Optional<U> flatMap(Function<? super T, ? extends Optional<? extends U>> mapper) {
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   278
        Objects.requireNonNull(mapper);
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   279
        if (!isPresent()) {
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   280
            return empty();
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   281
        } else {
41598
755ef825ca6a 8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents: 33238
diff changeset
   282
            @SuppressWarnings("unchecked")
755ef825ca6a 8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents: 33238
diff changeset
   283
            Optional<U> r = (Optional<U>) mapper.apply(value);
755ef825ca6a 8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents: 33238
diff changeset
   284
            return Objects.requireNonNull(r);
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   285
        }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   286
    }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   287
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   288
    /**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   289
     * If a value is present, returns an {@code Optional} describing the value,
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   290
     * otherwise returns an {@code Optional} produced by the supplying function.
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   291
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   292
     * @param supplier the supplying function that produces an {@code Optional}
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   293
     *        to be returned
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   294
     * @return returns an {@code Optional} describing the value of this
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   295
     *         {@code Optional}, if a value is present, otherwise an
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   296
     *         {@code Optional} produced by the supplying function.
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   297
     * @throws NullPointerException if the supplying function is {@code null} or
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   298
     *         produces a {@code null} result
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   299
     * @since 9
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   300
     */
41598
755ef825ca6a 8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents: 33238
diff changeset
   301
    public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier) {
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   302
        Objects.requireNonNull(supplier);
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   303
        if (isPresent()) {
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   304
            return this;
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   305
        } else {
41598
755ef825ca6a 8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents: 33238
diff changeset
   306
            @SuppressWarnings("unchecked")
755ef825ca6a 8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents: 33238
diff changeset
   307
            Optional<T> r = (Optional<T>) supplier.get();
755ef825ca6a 8152617: add missing wildcards to Optional or() and flatMap()
smarks
parents: 33238
diff changeset
   308
            return Objects.requireNonNull(r);
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   309
        }
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   310
    }
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   311
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   312
    /**
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   313
     * If a value is present, returns a sequential {@link Stream} containing
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   314
     * only that value, otherwise returns an empty {@code Stream}.
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   315
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   316
     * @apiNote
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   317
     * This method can be used to transform a {@code Stream} of optional
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   318
     * elements to a {@code Stream} of present value elements:
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   319
     * <pre>{@code
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   320
     *     Stream<Optional<T>> os = ..
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   321
     *     Stream<T> s = os.flatMap(Optional::stream)
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   322
     * }</pre>
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   323
     *
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   324
     * @return the optional value as a {@code Stream}
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   325
     * @since 9
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   326
     */
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   327
    public Stream<T> stream() {
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   328
        if (!isPresent()) {
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   329
            return Stream.empty();
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   330
        } else {
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   331
            return Stream.of(value);
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   332
        }
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   333
    }
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   334
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 25859
diff changeset
   335
    /**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   336
     * If a value is present, returns the value, otherwise returns
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   337
     * {@code other}.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   338
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   339
     * @param other the value to be returned, if no value is present.
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   340
     *        May be {@code null}.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   341
     * @return the value, if present, otherwise {@code other}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   342
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   343
    public T orElse(T other) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   344
        return value != null ? value : other;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   345
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   346
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   347
    /**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   348
     * If a value is present, returns the value, otherwise returns the result
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   349
     * produced by the supplying function.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   350
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   351
     * @param supplier the supplying function that produces a value to be returned
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   352
     * @return the value, if present, otherwise the result produced by the
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   353
     *         supplying function
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   354
     * @throws NullPointerException if no value is present and the supplying
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   355
     *         function is {@code null}
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   356
     */
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   357
    public T orElseGet(Supplier<? extends T> supplier) {
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   358
        return value != null ? value : supplier.get();
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   359
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   360
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   361
    /**
48328
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   362
     * If a value is present, returns the value, otherwise throws
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   363
     * {@code NoSuchElementException}.
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   364
     *
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   365
     * @return the non-{@code null} value described by this {@code Optional}
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   366
     * @throws NoSuchElementException if no value is present
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   367
     * @since 10
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   368
     */
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   369
    public T orElseThrow() {
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   370
        if (value == null) {
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   371
            throw new NoSuchElementException("No value present");
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   372
        }
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   373
        return value;
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   374
    }
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   375
7acf5700d542 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
smarks
parents: 47216
diff changeset
   376
    /**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   377
     * If a value is present, returns the value, otherwise throws an exception
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   378
     * produced by the exception supplying function.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   379
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   380
     * @apiNote
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   381
     * A method reference to the exception constructor with an empty argument
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   382
     * list can be used as the supplier. For example,
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   383
     * {@code IllegalStateException::new}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   384
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   385
     * @param <X> Type of the exception to be thrown
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   386
     * @param exceptionSupplier the supplying function that produces an
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   387
     *        exception to be thrown
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   388
     * @return the value, if present
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   389
     * @throws X if no value is present
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   390
     * @throws NullPointerException if no value is present and the exception
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   391
     *          supplying function is {@code null}
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   392
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   393
    public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   394
        if (value != null) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   395
            return value;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   396
        } else {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   397
            throw exceptionSupplier.get();
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   398
        }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   399
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   400
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   401
    /**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   402
     * Indicates whether some other object is "equal to" this {@code Optional}.
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   403
     * The other object is considered equal if:
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   404
     * <ul>
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   405
     * <li>it is also an {@code Optional} and;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   406
     * <li>both instances have no value present or;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   407
     * <li>the present values are "equal to" each other via {@code equals()}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   408
     * </ul>
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   409
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   410
     * @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
   411
     * @return {@code true} if the other object is "equal to" this object
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   412
     *         otherwise {@code false}
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   413
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   414
    @Override
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   415
    public boolean equals(Object obj) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   416
        if (this == obj) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   417
            return true;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   418
        }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   419
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   420
        if (!(obj instanceof Optional)) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   421
            return false;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   422
        }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   423
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 19074
diff changeset
   424
        Optional<?> other = (Optional<?>) obj;
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   425
        return Objects.equals(value, other.value);
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   426
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   427
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   428
    /**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   429
     * Returns the hash code of the value, if present, otherwise {@code 0}
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   430
     * (zero) if no value is present.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   431
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   432
     * @return hash code value of the present value or {@code 0} if no value is
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   433
     *         present
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   434
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   435
    @Override
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   436
    public int hashCode() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   437
        return Objects.hashCode(value);
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   438
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   439
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   440
    /**
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   441
     * Returns a non-empty string representation of this {@code Optional}
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   442
     * suitable for debugging.  The exact presentation format is unspecified and
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   443
     * may vary between implementations and versions.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   444
     *
33238
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   445
     * @implSpec
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   446
     * If a value is present the result must include its string representation
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   447
     * in the result.  Empty and present {@code Optional}s must be unambiguously
80720cbea36f 8080418: Add Optional.or()
psandoz
parents: 29599
diff changeset
   448
     * differentiable.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   449
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   450
     * @return the string representation of this instance
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   451
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   452
    @Override
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   453
    public String toString() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   454
        return value != null
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   455
            ? String.format("Optional[%s]", value)
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   456
            : "Optional.empty";
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   457
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   458
}