jdk/src/share/classes/java/util/Optional.java
author mduigou
Tue, 06 Aug 2013 18:18:12 -0700
changeset 19220 d3d40ccb544e
parent 19074 84a8d23e8f32
child 20479 36121d698418
permissions -rw-r--r--
8022476: cleanup some raw types and unchecked warnings in java.util.stream Reviewed-by: darcy Contributed-by: mike.duigou@oracle.com, henry.jen@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     1
/*
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
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;
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 non-null 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
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    35
 * {@code get()} will return the value.
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(java.lang.Object) orElse()}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    39
 * (return a default value if value not present) and
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    40
 * {@link #ifPresent(java.util.function.Consumer) ifPresent()} (execute a block
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    41
 * of code if the value is present).
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    42
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    43
 * @since 1.8
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    44
 */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    45
public final class Optional<T> {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    46
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    47
     * Common instance for {@code empty()}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    48
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    49
    private static final Optional<?> EMPTY = new Optional<>();
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    50
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
     * If non-null, the value; if null, indicates no value is present
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 final T value;
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
    /**
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
    57
     * Constructs an empty instance.
16489
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
     * @implNote Generally only one empty instance, {@link Optional#EMPTY},
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    60
     * should exist per VM.
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
    private Optional() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    63
        this.value = null;
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
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
     * Returns an empty {@code Optional} instance.  No value is present for this
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    68
     * Optional.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    69
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    70
     * @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
    71
     * is empty by comparing with {@code ==} against instances returned by
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    72
     * {@code Option.empty()}. There is no guarantee that it is a singleton.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    73
     * Instead, use {@link #isPresent()}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    74
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    75
     * @param <T> Type of the non-existent value
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    76
     * @return an empty {@code Optional}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    77
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    78
    public static<T> Optional<T> empty() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    79
        @SuppressWarnings("unchecked")
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    80
        Optional<T> t = (Optional<T>) EMPTY;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    81
        return t;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    82
    }
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
    /**
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
    85
     * Constructs an instance with the value present.
16489
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
     * @param value the non-null value to be present
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
    private Optional(T value) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    90
        this.value = Objects.requireNonNull(value);
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    91
    }
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
    /**
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
    94
     * Returns an {@code Optional} with the specified present non-null value.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    95
     *
19074
84a8d23e8f32 8020539: Clean up doclint problems in java.util package, part 2
bpb
parents: 18819
diff changeset
    96
     * @param <T> the class of the value
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    97
     * @param value the value to be present, which must be non-null
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    98
     * @return an {@code Optional} with the value present
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
    public static <T> Optional<T> of(T value) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   101
        return new Optional<>(value);
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   102
    }
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
    /**
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   105
     * Returns an {@code Optional} describing the specified value, if non-null,
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   106
     * otherwise returns an empty {@code Optional}.
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   107
     *
19074
84a8d23e8f32 8020539: Clean up doclint problems in java.util package, part 2
bpb
parents: 18819
diff changeset
   108
     * @param <T> the class of the value
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   109
     * @param value the possibly-null value to describe
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   110
     * @return an {@code Optional} with a present value if the specified value
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   111
     * is non-null, otherwise an empty {@code Optional}
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   112
     */
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   113
    public static <T> Optional<T> ofNullable(T value) {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   114
        return value == null ? empty() : of(value);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   115
    }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   116
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   117
    /**
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   118
     * If a value is present in this {@code Optional}, returns the value,
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   119
     * otherwise throws {@code NoSuchElementException}.
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 the non-null value held by this {@code Optional}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   122
     * @throws NoSuchElementException if there is no value present
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
     * @see Optional#isPresent()
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   125
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   126
    public T get() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   127
        if (value == null) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   128
            throw new NoSuchElementException("No value present");
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   129
        }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   130
        return value;
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
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   134
     * Return {@code true} if there is a value present, otherwise {@code false}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   135
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   136
     * @return {@code true} if there is a value present, otherwise {@code false}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   137
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   138
    public boolean isPresent() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   139
        return value != null;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   140
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   141
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   142
    /**
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   143
     * If a value is present, invoke the specified consumer with the value,
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   144
     * otherwise do nothing.
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
     * @param consumer block to be executed if a value is present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   147
     * @throws NullPointerException if value is present and {@code consumer} is
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   148
     * null
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
    public void ifPresent(Consumer<? super T> consumer) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   151
        if (value != null)
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   152
            consumer.accept(value);
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   153
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   154
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   155
    /**
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   156
     * If a value is present, and the value matches the given predicate,
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   157
     * return an {@code Optional} describing the value, otherwise return an
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   158
     * empty {@code Optional}.
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   159
     *
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   160
     * @param predicate a predicate to apply to the value, if present
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   161
     * @return an {@code Optional} describing the value of this {@code Optional}
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   162
     * if a value is present and the value matches the given predicate,
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   163
     * otherwise an empty {@code Optional}
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   164
     * @throws NullPointerException if the predicate is null
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   165
     */
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   166
    public Optional<T> filter(Predicate<? super T> predicate) {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   167
        Objects.requireNonNull(predicate);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   168
        if (!isPresent())
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   169
            return this;
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   170
        else
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   171
            return predicate.test(value) ? this : empty();
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   172
    }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   173
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   174
    /**
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   175
     * If a value is present, apply the provided mapping function to it,
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   176
     * and if the result is non-null, return an {@code Optional} describing the
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   177
     * result.  Otherwise return an empty {@code Optional}.
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   178
     *
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   179
     * @apiNote This method supports post-processing on optional values, without
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   180
     * the need to explicitly check for a return status.  For example, the
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   181
     * following code traverses a stream of file names, selects one that has
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   182
     * not yet been processed, and then opens that file, returning an
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   183
     * {@code Optional<FileInputStream>}:
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   184
     *
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   185
     * <pre>{@code
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   186
     *     Optional<FileInputStream> fis =
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   187
     *         names.stream().filter(name -> !isProcessedYet(name))
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   188
     *                       .findFirst()
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   189
     *                       .map(name -> new FileInputStream(name));
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   190
     * }</pre>
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   191
     *
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   192
     * Here, {@code findFirst} returns an {@code Optional<String>}, and then
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   193
     * {@code map} returns an {@code Optional<FileInputStream>} for the desired
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   194
     * file if one exists.
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   195
     *
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   196
     * @param <U> The type of the result of the mapping function
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   197
     * @param mapper a mapping function to apply to the value, if present
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   198
     * @return an {@code Optional} describing the result of applying a mapping
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   199
     * function to the value of this {@code Optional}, if a value is present,
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   200
     * otherwise an empty {@code Optional}
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   201
     * @throws NullPointerException if the mapping function is null
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   202
     */
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   203
    public<U> Optional<U> map(Function<? super T, ? extends U> mapper) {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   204
        Objects.requireNonNull(mapper);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   205
        if (!isPresent())
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   206
            return empty();
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   207
        else {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   208
            return Optional.ofNullable(mapper.apply(value));
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   209
        }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   210
    }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   211
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   212
    /**
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   213
     * If a value is present, apply the provided {@code Optional}-bearing
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   214
     * mapping function to it, return that result, otherwise return an empty
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   215
     * {@code Optional}.  This method is similar to {@link #map(Function)},
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   216
     * but the provided mapper is one whose result is already an {@code Optional},
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   217
     * and if invoked, {@code flatMap} does not wrap it with an additional
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   218
     * {@code Optional}.
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   219
     *
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   220
     * @param <U> The type parameter to the {@code Optional} returned by
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   221
     * @param mapper a mapping function to apply to the value, if present
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   222
     *           the mapping function
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   223
     * @return the result of applying an {@code Optional}-bearing mapping
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   224
     * function to the value of this {@code Optional}, if a value is present,
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   225
     * otherwise an empty {@code Optional}
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   226
     * @throws NullPointerException if the mapping function is null or returns
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   227
     * a null result
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   228
     */
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   229
    public<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   230
        Objects.requireNonNull(mapper);
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   231
        if (!isPresent())
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   232
            return empty();
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   233
        else {
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   234
            return Objects.requireNonNull(mapper.apply(value));
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   235
        }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   236
    }
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   237
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   238
    /**
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   239
     * Return the value if present, otherwise return {@code other}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   240
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   241
     * @param other the value to be returned if there is no value present, may
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   242
     * be null
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   243
     * @return the value, if present, otherwise {@code other}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   244
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   245
    public T orElse(T other) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   246
        return value != null ? value : other;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   247
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   248
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
     * Return the value if present, otherwise invoke {@code other} and return
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   251
     * the result of that invocation.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   252
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   253
     * @param other a {@code Supplier} whose result is returned if no value
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   254
     * is present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   255
     * @return the value if present otherwise the result of {@code other.get()}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   256
     * @throws NullPointerException if value is not present and {@code other} is
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   257
     * null
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
    public T orElseGet(Supplier<? extends T> other) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   260
        return value != null ? value : other.get();
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   261
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   262
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   263
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   264
     * Return the contained value, if present, otherwise throw an exception
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   265
     * to be created by the provided supplier.
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
     * @apiNote A method reference to the exception constructor with an empty
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   268
     * argument list can be used as the supplier. For example,
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   269
     * {@code IllegalStateException::new}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   270
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   271
     * @param <X> Type of the exception to be thrown
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   272
     * @param exceptionSupplier The supplier which will return the exception to
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   273
     * be thrown
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   274
     * @return the present value
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   275
     * @throws X if there is no value present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   276
     * @throws NullPointerException if no value is present and
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   277
     * {@code exceptionSupplier} is null
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   278
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   279
    public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   280
        if (value != null) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   281
            return value;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   282
        } else {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   283
            throw exceptionSupplier.get();
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   284
        }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   285
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   286
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
     * Indicates whether some other object is "equal to" this Optional. The
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   289
     * other object is considered equal if:
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   290
     * <ul>
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   291
     * <li>it is also an {@code Optional} and;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   292
     * <li>both instances have no value present or;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   293
     * <li>the present values are "equal to" each other via {@code equals()}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   294
     * </ul>
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
     * @param obj an object to be tested for equality
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   297
     * @return {code true} if the other object is "equal to" this object
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   298
     * otherwise {@code false}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   299
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   300
    @Override
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   301
    public boolean equals(Object obj) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   302
        if (this == obj) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   303
            return true;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   304
        }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   305
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   306
        if (!(obj instanceof Optional)) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   307
            return false;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   308
        }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   309
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 19074
diff changeset
   310
        Optional<?> other = (Optional<?>) obj;
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   311
        return Objects.equals(value, other.value);
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   312
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   313
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   314
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   315
     * 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
   316
     * no value is present.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   317
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   318
     * @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
   319
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   320
    @Override
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   321
    public int hashCode() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   322
        return Objects.hashCode(value);
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   323
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   324
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   325
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   326
     * Returns a non-empty string representation of this Optional suitable for
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   327
     * debugging. The exact presentation format is unspecified and may vary
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   328
     * between implementations and versions.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   329
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   330
     * @implSpec If a value is present the result must include its string
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   331
     * representation in the result. Empty and present Optionals must be
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   332
     * unambiguously differentiable.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   333
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   334
     * @return the string representation of this instance
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   335
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   336
    @Override
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   337
    public String toString() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   338
        return value != null
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   339
            ? String.format("Optional[%s]", value)
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   340
            : "Optional.empty";
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   341
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   342
}