jdk/src/share/classes/java/util/OptionalInt.java
author dl
Fri, 03 Jan 2014 06:22:41 +0000
changeset 22096 ca113030b2b3
parent 21966 db858b7c6313
permissions -rw-r--r--
8031133: AbstractMap should specify its default implementation using @implSpec Reviewed-by: chegar, alanb
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.IntConsumer;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    28
import java.util.function.IntSupplier;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    29
import java.util.function.Supplier;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    30
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
 * A container object which may or may not contain a {@code int} value.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    33
 * If a value is present, {@code isPresent()} will return {@code true} and
21966
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents: 18819
diff changeset
    34
 * {@code getAsInt()} will return the value.
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    35
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    36
 * <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
    37
 * value are provided, such as {@link #orElse(int) orElse()}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    38
 * (return a default value if value not present) and
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    39
 * {@link #ifPresent(java.util.function.IntConsumer) ifPresent()} (execute a block
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    40
 * of code if the value is present).
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    41
 *
21966
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents: 18819
diff changeset
    42
 * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents: 18819
diff changeset
    43
 * class; use of identity-sensitive operations (including reference equality
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents: 18819
diff changeset
    44
 * ({@code ==}), identity hash code, or synchronization) on instances of
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents: 18819
diff changeset
    45
 * {@code OptionalInt} may have unpredictable results and should be avoided.
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents: 18819
diff changeset
    46
 *
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    47
 * @since 1.8
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
public final class OptionalInt {
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
     * Common instance for {@code empty()}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    52
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    53
    private static final OptionalInt EMPTY = new OptionalInt();
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    54
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
     * If true then the value is present, otherwise indicates no value is present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    57
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    58
    private final boolean isPresent;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    59
    private final int value;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    60
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
     * Construct an empty instance.
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
     * @implNote Generally only one empty instance, {@link OptionalInt#EMPTY},
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    65
     * should exist per VM.
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
    private OptionalInt() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    68
        this.isPresent = false;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    69
        this.value = 0;
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
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    72
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    73
     * Returns an empty {@code OptionalInt} instance.  No value is present for this
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    74
     * OptionalInt.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    75
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    76
     * @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
    77
     * is empty by comparing with {@code ==} against instances returned by
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    78
     * {@code Option.empty()}. There is no guarantee that it is a singleton.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    79
     * Instead, use {@link #isPresent()}.
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
     *  @return an empty {@code OptionalInt}
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
    public static OptionalInt empty() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    84
        return EMPTY;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    85
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    86
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    87
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    88
     * Construct an instance with the value present.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    89
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    90
     * @param value the int value to be present
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
    private OptionalInt(int value) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    93
        this.isPresent = true;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    94
        this.value = value;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    95
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    96
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    97
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    98
     * Return an {@code OptionalInt} with the specified 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
     * @param value the value to be present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   101
     * @return an {@code OptionalInt} with the value present
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
    public static OptionalInt of(int value) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   104
        return new OptionalInt(value);
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
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   107
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   108
     * If a value is present in this {@code OptionalInt}, returns the value,
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   109
     * otherwise throws {@code NoSuchElementException}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   110
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   111
     * @return the value held by this {@code OptionalInt}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   112
     * @throws NoSuchElementException if there is no value present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   113
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   114
     * @see OptionalInt#isPresent()
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   115
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   116
    public int getAsInt() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   117
        if (!isPresent) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   118
            throw new NoSuchElementException("No value present");
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   119
        }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   120
        return 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
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   124
     * Return {@code true} if there is a value present, otherwise {@code false}.
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
     * @return {@code true} if there is a value present, otherwise {@code false}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   127
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   128
    public boolean isPresent() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   129
        return isPresent;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   130
    }
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
     * Have the specified consumer accept the value if a value is present,
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   134
     * otherwise do nothing.
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
     * @param consumer block to be executed if a value is present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   137
     * @throws NullPointerException if value is present and {@code consumer} is
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   138
     * null
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   139
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   140
    public void ifPresent(IntConsumer consumer) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   141
        if (isPresent)
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   142
            consumer.accept(value);
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   143
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   144
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
     * Return the value if present, otherwise return {@code other}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   147
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   148
     * @param other the value to be returned if there is no value present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   149
     * @return the value, if present, otherwise {@code other}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   150
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   151
    public int orElse(int other) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   152
        return isPresent ? value : other;
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
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   156
     * Return the value if present, otherwise invoke {@code other} and return
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   157
     * the result of that invocation.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   158
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   159
     * @param other a {@code IntSupplier} whose result is returned if no value
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   160
     * is present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   161
     * @return the value if present otherwise the result of {@code other.getAsInt()}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   162
     * @throws NullPointerException if value is not present and {@code other} is
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   163
     * null
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   164
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   165
    public int orElseGet(IntSupplier other) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   166
        return isPresent ? value : other.getAsInt();
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   167
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   168
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   169
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   170
     * Return the contained value, if present, otherwise throw an exception
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   171
     * to be created by the provided supplier.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   172
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   173
     * @apiNote A method reference to the exception constructor with an empty
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   174
     * argument list can be used as the supplier. For example,
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   175
     * {@code IllegalStateException::new}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   176
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   177
     * @param <X> Type of the exception to be thrown
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   178
     * @param exceptionSupplier The supplier which will return the exception to
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   179
     * be thrown
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   180
     * @return the present value
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   181
     * @throws X if there is no value present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   182
     * @throws NullPointerException if no value is present and
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   183
     * {@code exceptionSupplier} is null
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   184
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   185
    public<X extends Throwable> int orElseThrow(Supplier<X> exceptionSupplier) throws X {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   186
        if (isPresent) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   187
            return value;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   188
        } else {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   189
            throw exceptionSupplier.get();
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   190
        }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   191
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   192
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   193
    /**
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   194
     * Indicates whether some other object is "equal to" this OptionalInt. The
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   195
     * other object is considered equal if:
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   196
     * <ul>
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   197
     * <li>it is also an {@code OptionalInt} and;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   198
     * <li>both instances have no value present or;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   199
     * <li>the present values are "equal to" each other via {@code ==}.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   200
     * </ul>
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   201
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   202
     * @param obj an object to be tested for equality
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   203
     * @return {code true} if the other object is "equal to" this object
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   204
     * otherwise {@code false}
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   205
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   206
    @Override
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   207
    public boolean equals(Object obj) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   208
        if (this == obj) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   209
            return true;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   210
        }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   211
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   212
        if (!(obj instanceof OptionalInt)) {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   213
            return false;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   214
        }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   215
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   216
        OptionalInt other = (OptionalInt) obj;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   217
        return (isPresent && other.isPresent)
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   218
                ? value == other.value
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   219
                : isPresent == other.isPresent;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   220
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   221
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   222
    /**
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   223
     * 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
   224
     * no value is present.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   225
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   226
     * @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
   227
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   228
    @Override
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   229
    public int hashCode() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   230
        return isPresent ? Integer.hashCode(value) : 0;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   231
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   232
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   233
    /**
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   234
     * {@inheritDoc}
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
     * Returns a non-empty string representation of this object suitable for
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   237
     * debugging. The exact presentation format is unspecified and may vary
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   238
     * between implementations and versions.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   239
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   240
     * @implSpec If a value is present the result must include its string
18819
c4335fc31aeb 8015317: Optional.filter, map, and flatMap
mduigou
parents: 16489
diff changeset
   241
     * representation in the result. Empty and present instances must be
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   242
     * unambiguously differentiable.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   243
     *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   244
     * @return the string representation of this instance
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   245
     */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   246
    @Override
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   247
    public String toString() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   248
        return isPresent
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   249
                ? String.format("OptionalInt[%s]", value)
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   250
                : "OptionalInt.empty";
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   251
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   252
}