8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
Reviewed-by: mduigou, darcy, alanb, jjb
Contributed-by: Brian Goetz <brian.goetz@oracle.com>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/share/classes/java/util/Optional.java Tue Mar 19 16:05:34 2013 -0700
@@ -0,0 +1,243 @@
+/*
+ * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.util;
+
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+
+/**
+ * A container object which may or may not contain a non-null value.
+ * If a value is present, {@code isPresent()} will return {@code true} and
+ * {@code get()} will return the value.
+ *
+ * <p>Additional methods that depend on the presence or absence of a contained
+ * value are provided, such as {@link #orElse(java.lang.Object) orElse()}
+ * (return a default value if value not present) and
+ * {@link #ifPresent(java.util.function.Consumer) ifPresent()} (execute a block
+ * of code if the value is present).
+ *
+ * @since 1.8
+ */
+public final class Optional<T> {
+ /**
+ * Common instance for {@code empty()}.
+ */
+ private static final Optional<?> EMPTY = new Optional<>();
+
+ /**
+ * If non-null, the value; if null, indicates no value is present
+ */
+ private final T value;
+
+ /**
+ * Construct an empty instance.
+ *
+ * @implNote Generally only one empty instance, {@link Optional#EMPTY},
+ * should exist per VM.
+ */
+ private Optional() {
+ this.value = null;
+ }
+
+ /**
+ * Returns an empty {@code Optional} instance. No value is present for this
+ * Optional.
+ *
+ * @apiNote Though it may be tempting to do so, avoid testing if an object
+ * is empty by comparing with {@code ==} against instances returned by
+ * {@code Option.empty()}. There is no guarantee that it is a singleton.
+ * Instead, use {@link #isPresent()}.
+ *
+ * @param <T> Type of the non-existent value
+ * @return an empty {@code Optional}
+ */
+ public static<T> Optional<T> empty() {
+ @SuppressWarnings("unchecked")
+ Optional<T> t = (Optional<T>) EMPTY;
+ return t;
+ }
+
+ /**
+ * Construct an instance with the value present.
+ *
+ * @param value the non-null value to be present
+ */
+ private Optional(T value) {
+ this.value = Objects.requireNonNull(value);
+ }
+
+ /**
+ * Return an {@code Optional} with the specified present value.
+ *
+ * @param value the value to be present, which must be non-null
+ * @return an {@code Optional} with the value present
+ */
+ public static <T> Optional<T> of(T value) {
+ return new Optional<>(value);
+ }
+
+ /**
+ * If a value is present in this {@code Optional}, returns the value,
+ * otherwise throws {@code NoSuchElementException}.
+ *
+ * @return the non-null value held by this {@code Optional}
+ * @throws NoSuchElementException if there is no value present
+ *
+ * @see Optional#isPresent()
+ */
+ public T get() {
+ if (value == null) {
+ throw new NoSuchElementException("No value present");
+ }
+ return value;
+ }
+
+ /**
+ * Return {@code true} if there is a value present, otherwise {@code false}.
+ *
+ * @return {@code true} if there is a value present, otherwise {@code false}
+ */
+ public boolean isPresent() {
+ return value != null;
+ }
+
+ /**
+ * Have the specified consumer accept the value if a value is present,
+ * otherwise do nothing.
+ *
+ * @param consumer block to be executed if a value is present
+ * @throws NullPointerException if value is present and {@code consumer} is
+ * null
+ */
+ public void ifPresent(Consumer<? super T> consumer) {
+ if (value != null)
+ consumer.accept(value);
+ }
+
+ /**
+ * Return the value if present, otherwise return {@code other}.
+ *
+ * @param other the value to be returned if there is no value present, may
+ * be null
+ * @return the value, if present, otherwise {@code other}
+ */
+ public T orElse(T other) {
+ return value != null ? value : other;
+ }
+
+ /**
+ * Return the value if present, otherwise invoke {@code other} and return
+ * the result of that invocation.
+ *
+ * @param other a {@code Supplier} whose result is returned if no value
+ * is present
+ * @return the value if present otherwise the result of {@code other.get()}
+ * @throws NullPointerException if value is not present and {@code other} is
+ * null
+ */
+ public T orElseGet(Supplier<? extends T> other) {
+ return value != null ? value : other.get();
+ }
+
+ /**
+ * Return the contained value, if present, otherwise throw an exception
+ * to be created by the provided supplier.
+ *
+ * @apiNote A method reference to the exception constructor with an empty
+ * argument list can be used as the supplier. For example,
+ * {@code IllegalStateException::new}
+ *
+ * @param <X> Type of the exception to be thrown
+ * @param exceptionSupplier The supplier which will return the exception to
+ * be thrown
+ * @return the present value
+ * @throws X if there is no value present
+ * @throws NullPointerException if no value is present and
+ * {@code exceptionSupplier} is null
+ */
+ public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
+ if (value != null) {
+ return value;
+ } else {
+ throw exceptionSupplier.get();
+ }
+ }
+
+ /**
+ * Indicates whether some other object is "equal to" this Optional. The
+ * other object is considered equal if:
+ * <ul>
+ * <li>it is also an {@code Optional} and;
+ * <li>both instances have no value present or;
+ * <li>the present values are "equal to" each other via {@code equals()}.
+ * </ul>
+ *
+ * @param obj an object to be tested for equality
+ * @return {code true} if the other object is "equal to" this object
+ * otherwise {@code false}
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+
+ if (!(obj instanceof Optional)) {
+ return false;
+ }
+
+ Optional other = (Optional) obj;
+ return Objects.equals(value, other.value);
+ }
+
+ /**
+ * Returns the hash code value of the present value, if any, or 0 (zero) if
+ * no value is present.
+ *
+ * @return hash code value of the present value or 0 if no value is present
+ */
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(value);
+ }
+
+ /**
+ * Returns a non-empty string representation of this Optional suitable for
+ * debugging. The exact presentation format is unspecified and may vary
+ * between implementations and versions.
+ *
+ * @implSpec If a value is present the result must include its string
+ * representation in the result. Empty and present Optionals must be
+ * unambiguously differentiable.
+ *
+ * @return the string representation of this instance
+ */
+ @Override
+ public String toString() {
+ return value != null
+ ? String.format("Optional[%s]", value)
+ : "Optional.empty";
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/share/classes/java/util/OptionalDouble.java Tue Mar 19 16:05:34 2013 -0700
@@ -0,0 +1,245 @@
+/*
+ * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.util;
+
+import java.util.function.DoubleConsumer;
+import java.util.function.DoubleSupplier;
+import java.util.function.Supplier;
+
+/**
+ * A container object which may or may not contain a {@code double} value.
+ * If a value is present, {@code isPresent()} will return {@code true} and
+ * {@code get()} will return the value.
+ *
+ * <p>Additional methods that depend on the presence or absence of a contained
+ * value are provided, such as {@link #orElse(double) orElse()}
+ * (return a default value if value not present) and
+ * {@link #ifPresent(java.util.function.DoubleConsumer) ifPresent()} (execute a block
+ * of code if the value is present).
+ *
+ * @since 1.8
+ */
+public final class OptionalDouble {
+ /**
+ * Common instance for {@code empty()}.
+ */
+ private static final OptionalDouble EMPTY = new OptionalDouble();
+
+ /**
+ * If true then the value is present, otherwise indicates no value is present
+ */
+ private final boolean isPresent;
+ private final double value;
+
+ /**
+ * Construct an empty instance.
+ *
+ * @implNote generally only one empty instance, {@link OptionalDouble#EMPTY},
+ * should exist per VM.
+ */
+ private OptionalDouble() {
+ this.isPresent = false;
+ this.value = Double.NaN;
+ }
+
+ /**
+ * Returns an empty {@code OptionalDouble} instance. No value is present for this
+ * OptionalDouble.
+ *
+ * @apiNote Though it may be tempting to do so, avoid testing if an object
+ * is empty by comparing with {@code ==} against instances returned by
+ * {@code Option.empty()}. There is no guarantee that it is a singleton.
+ * Instead, use {@link #isPresent()}.
+ *
+ * @return an empty {@code OptionalDouble}.
+ */
+ public static OptionalDouble empty() {
+ return EMPTY;
+ }
+
+ /**
+ * Construct an instance with the value present.
+ *
+ * @param value the double value to be present.
+ */
+ private OptionalDouble(double value) {
+ this.isPresent = true;
+ this.value = value;
+ }
+
+ /**
+ * Return an {@code OptionalDouble} with the specified value present.
+ *
+ * @param value the value to be present
+ * @return an {@code OptionalDouble} with the value present
+ */
+ public static OptionalDouble of(double value) {
+ return new OptionalDouble(value);
+ }
+
+ /**
+ * If a value is present in this {@code OptionalDouble}, returns the value,
+ * otherwise throws {@code NoSuchElementException}.
+ *
+ * @return the value held by this {@code OptionalDouble}
+ * @throws NoSuchElementException if there is no value present
+ *
+ * @see OptionalDouble#isPresent()
+ */
+ public double getAsDouble() {
+ if (!isPresent) {
+ throw new NoSuchElementException("No value present");
+ }
+ return value;
+ }
+
+ /**
+ * Return {@code true} if there is a value present, otherwise {@code false}.
+ *
+ * @return {@code true} if there is a value present, otherwise {@code false}
+ */
+ public boolean isPresent() {
+ return isPresent;
+ }
+
+ /**
+ * Have the specified consumer accept the value if a value is present,
+ * otherwise do nothing.
+ *
+ * @param consumer block to be executed if a value is present
+ * @throws NullPointerException if value is present and {@code consumer} is
+ * null
+ */
+ public void ifPresent(DoubleConsumer consumer) {
+ if (isPresent)
+ consumer.accept(value);
+ }
+
+ /**
+ * Return the value if present, otherwise return {@code other}.
+ *
+ * @param other the value to be returned if there is no value present
+ * @return the value, if present, otherwise {@code other}
+ */
+ public double orElse(double other) {
+ return isPresent ? value : other;
+ }
+
+ /**
+ * Return the value if present, otherwise invoke {@code other} and return
+ * the result of that invocation.
+ *
+ * @param other a {@code DoubleSupplier} whose result is returned if no value
+ * is present
+ * @return the value if present otherwise the result of {@code other.getAsDouble()}
+ * @throws NullPointerException if value is not present and {@code other} is
+ * null
+ */
+ public double orElseGet(DoubleSupplier other) {
+ return isPresent ? value : other.getAsDouble();
+ }
+
+ /**
+ * Return the contained value, if present, otherwise throw an exception
+ * to be created by the provided supplier.
+ *
+ * @apiNote A method reference to the exception constructor with an empty
+ * argument list can be used as the supplier. For example,
+ * {@code IllegalStateException::new}
+ *
+ * @param <X> Type of the exception to be thrown
+ * @param exceptionSupplier The supplier which will return the exception to
+ * be thrown
+ * @return the present value
+ * @throws X if there is no value present
+ * @throws NullPointerException if no value is present and
+ * {@code exceptionSupplier} is null
+ */
+ public<X extends Throwable> double orElseThrow(Supplier<X> exceptionSupplier) throws X {
+ if (isPresent) {
+ return value;
+ } else {
+ throw exceptionSupplier.get();
+ }
+ }
+
+ /**
+ * Indicates whether some other object is "equal to" this Optional. The
+ * other object is considered equal if:
+ * <ul>
+ * <li>it is also an {@code OptionalInt} and;
+ * <li>both instances have no value present or;
+ * <li>the present values are "equal to" each other via {@code Double.compare() == 0}.
+ * </ul>
+ *
+ * @param obj an object to be tested for equality
+ * @return {code true} if the other object is "equal to" this object
+ * otherwise {@code false}
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+
+ if (!(obj instanceof OptionalDouble)) {
+ return false;
+ }
+
+ OptionalDouble other = (OptionalDouble) obj;
+ return (isPresent && other.isPresent)
+ ? Double.compare(value, other.value) == 0
+ : isPresent == other.isPresent;
+ }
+
+ /**
+ * Returns the hash code value of the present value, if any, or 0 (zero) if
+ * no value is present.
+ *
+ * @return hash code value of the present value or 0 if no value is present
+ */
+ @Override
+ public int hashCode() {
+ return isPresent ? Double.hashCode(value) : 0;
+ }
+
+ /**
+ * Returns a non-empty string representation of this OptionalDouble suitable for
+ * debugging. The exact presentation format is unspecified and may vary
+ * between implementations and versions.
+ *
+ * @implSpec If a value is present the result must include its string
+ * representation in the result. Empty and present OptionalDoubless must be
+ * unambiguously differentiable.
+ *
+ * @return the string representation of this instance
+ */
+ @Override
+ public String toString() {
+ return isPresent
+ ? String.format("OptionalDouble[%s]", value)
+ : "OptionalDouble.empty";
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/share/classes/java/util/OptionalInt.java Tue Mar 19 16:05:34 2013 -0700
@@ -0,0 +1,245 @@
+/*
+ * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.util;
+
+import java.util.function.IntConsumer;
+import java.util.function.IntSupplier;
+import java.util.function.Supplier;
+
+/**
+ * A container object which may or may not contain a {@code int} value.
+ * If a value is present, {@code isPresent()} will return {@code true} and
+ * {@code get()} will return the value.
+ *
+ * <p>Additional methods that depend on the presence or absence of a contained
+ * value are provided, such as {@link #orElse(int) orElse()}
+ * (return a default value if value not present) and
+ * {@link #ifPresent(java.util.function.IntConsumer) ifPresent()} (execute a block
+ * of code if the value is present).
+ *
+ * @since 1.8
+ */
+public final class OptionalInt {
+ /**
+ * Common instance for {@code empty()}.
+ */
+ private static final OptionalInt EMPTY = new OptionalInt();
+
+ /**
+ * If true then the value is present, otherwise indicates no value is present
+ */
+ private final boolean isPresent;
+ private final int value;
+
+ /**
+ * Construct an empty instance.
+ *
+ * @implNote Generally only one empty instance, {@link OptionalInt#EMPTY},
+ * should exist per VM.
+ */
+ private OptionalInt() {
+ this.isPresent = false;
+ this.value = 0;
+ }
+
+ /**
+ * Returns an empty {@code OptionalInt} instance. No value is present for this
+ * OptionalInt.
+ *
+ * @apiNote Though it may be tempting to do so, avoid testing if an object
+ * is empty by comparing with {@code ==} against instances returned by
+ * {@code Option.empty()}. There is no guarantee that it is a singleton.
+ * Instead, use {@link #isPresent()}.
+ *
+ * @return an empty {@code OptionalInt}
+ */
+ public static OptionalInt empty() {
+ return EMPTY;
+ }
+
+ /**
+ * Construct an instance with the value present.
+ *
+ * @param value the int value to be present
+ */
+ private OptionalInt(int value) {
+ this.isPresent = true;
+ this.value = value;
+ }
+
+ /**
+ * Return an {@code OptionalInt} with the specified value present.
+ *
+ * @param value the value to be present
+ * @return an {@code OptionalInt} with the value present
+ */
+ public static OptionalInt of(int value) {
+ return new OptionalInt(value);
+ }
+
+ /**
+ * If a value is present in this {@code OptionalInt}, returns the value,
+ * otherwise throws {@code NoSuchElementException}.
+ *
+ * @return the value held by this {@code OptionalInt}
+ * @throws NoSuchElementException if there is no value present
+ *
+ * @see OptionalInt#isPresent()
+ */
+ public int getAsInt() {
+ if (!isPresent) {
+ throw new NoSuchElementException("No value present");
+ }
+ return value;
+ }
+
+ /**
+ * Return {@code true} if there is a value present, otherwise {@code false}.
+ *
+ * @return {@code true} if there is a value present, otherwise {@code false}
+ */
+ public boolean isPresent() {
+ return isPresent;
+ }
+
+ /**
+ * Have the specified consumer accept the value if a value is present,
+ * otherwise do nothing.
+ *
+ * @param consumer block to be executed if a value is present
+ * @throws NullPointerException if value is present and {@code consumer} is
+ * null
+ */
+ public void ifPresent(IntConsumer consumer) {
+ if (isPresent)
+ consumer.accept(value);
+ }
+
+ /**
+ * Return the value if present, otherwise return {@code other}.
+ *
+ * @param other the value to be returned if there is no value present
+ * @return the value, if present, otherwise {@code other}
+ */
+ public int orElse(int other) {
+ return isPresent ? value : other;
+ }
+
+ /**
+ * Return the value if present, otherwise invoke {@code other} and return
+ * the result of that invocation.
+ *
+ * @param other a {@code IntSupplier} whose result is returned if no value
+ * is present
+ * @return the value if present otherwise the result of {@code other.getAsInt()}
+ * @throws NullPointerException if value is not present and {@code other} is
+ * null
+ */
+ public int orElseGet(IntSupplier other) {
+ return isPresent ? value : other.getAsInt();
+ }
+
+ /**
+ * Return the contained value, if present, otherwise throw an exception
+ * to be created by the provided supplier.
+ *
+ * @apiNote A method reference to the exception constructor with an empty
+ * argument list can be used as the supplier. For example,
+ * {@code IllegalStateException::new}
+ *
+ * @param <X> Type of the exception to be thrown
+ * @param exceptionSupplier The supplier which will return the exception to
+ * be thrown
+ * @return the present value
+ * @throws X if there is no value present
+ * @throws NullPointerException if no value is present and
+ * {@code exceptionSupplier} is null
+ */
+ public<X extends Throwable> int orElseThrow(Supplier<X> exceptionSupplier) throws X {
+ if (isPresent) {
+ return value;
+ } else {
+ throw exceptionSupplier.get();
+ }
+ }
+
+ /**
+ * Indicates whether some other object is "equal to" this Optional. The
+ * other object is considered equal if:
+ * <ul>
+ * <li>it is also an {@code OptionalInt} and;
+ * <li>both instances have no value present or;
+ * <li>the present values are "equal to" each other via {@code ==}.
+ * </ul>
+ *
+ * @param obj an object to be tested for equality
+ * @return {code true} if the other object is "equal to" this object
+ * otherwise {@code false}
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+
+ if (!(obj instanceof OptionalInt)) {
+ return false;
+ }
+
+ OptionalInt other = (OptionalInt) obj;
+ return (isPresent && other.isPresent)
+ ? value == other.value
+ : isPresent == other.isPresent;
+ }
+
+ /**
+ * Returns the hash code value of the present value, if any, or 0 (zero) if
+ * no value is present.
+ *
+ * @return hash code value of the present value or 0 if no value is present
+ */
+ @Override
+ public int hashCode() {
+ return isPresent ? Integer.hashCode(value) : 0;
+ }
+
+ /**
+ * Returns a non-empty string representation of this OptionalInt suitable for
+ * debugging. The exact presentation format is unspecified and may vary
+ * between implementations and versions.
+ *
+ * @implSpec If a value is present the result must include its string
+ * representation in the result. Empty and present OptionalInts must be
+ * unambiguously differentiable.
+ *
+ * @return the string representation of this instance
+ */
+ @Override
+ public String toString() {
+ return isPresent
+ ? String.format("OptionalInt[%s]", value)
+ : "OptionalInt.empty";
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/share/classes/java/util/OptionalLong.java Tue Mar 19 16:05:34 2013 -0700
@@ -0,0 +1,245 @@
+/*
+ * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.util;
+
+import java.util.function.LongConsumer;
+import java.util.function.LongSupplier;
+import java.util.function.Supplier;
+
+/**
+ * A container object which may or may not contain a {@code long} value.
+ * If a value is present, {@code isPresent()} will return {@code true} and
+ * {@code get()} will return the value.
+ *
+ * <p>Additional methods that depend on the presence or absence of a contained
+ * value are provided, such as {@link #orElse(long) orElse()}
+ * (return a default value if value not present) and
+ * {@link #ifPresent(java.util.function.LongConsumer) ifPresent()} (execute a block
+ * of code if the value is present).
+ *
+ * @since 1.8
+ */
+public final class OptionalLong {
+ /**
+ * Common instance for {@code empty()}.
+ */
+ private static final OptionalLong EMPTY = new OptionalLong();
+
+ /**
+ * If true then the value is present, otherwise indicates no value is present
+ */
+ private final boolean isPresent;
+ private final long value;
+
+ /**
+ * Construct an empty instance.
+ *
+ * @implNote generally only one empty instance, {@link OptionalLong#EMPTY},
+ * should exist per VM.
+ */
+ private OptionalLong() {
+ this.isPresent = false;
+ this.value = 0;
+ }
+
+ /**
+ * Returns an empty {@code OptionalLong} instance. No value is present for this
+ * OptionalLong.
+ *
+ * @apiNote Though it may be tempting to do so, avoid testing if an object
+ * is empty by comparing with {@code ==} against instances returned by
+ * {@code Option.empty()}. There is no guarantee that it is a singleton.
+ * Instead, use {@link #isPresent()}.
+ *
+ * @return an empty {@code OptionalLong}.
+ */
+ public static OptionalLong empty() {
+ return EMPTY;
+ }
+
+ /**
+ * Construct an instance with the value present.
+ *
+ * @param value the long value to be present
+ */
+ private OptionalLong(long value) {
+ this.isPresent = true;
+ this.value = value;
+ }
+
+ /**
+ * Return an {@code OptionalLong} with the specified value present.
+ *
+ * @param value the value to be present
+ * @return an {@code OptionalLong} with the value present
+ */
+ public static OptionalLong of(long value) {
+ return new OptionalLong(value);
+ }
+
+ /**
+ * If a value is present in this {@code OptionalLong}, returns the value,
+ * otherwise throws {@code NoSuchElementException}.
+ *
+ * @return the value held by this {@code OptionalLong}
+ * @throws NoSuchElementException if there is no value present
+ *
+ * @see OptionalLong#isPresent()
+ */
+ public long getAsLong() {
+ if (!isPresent) {
+ throw new NoSuchElementException("No value present");
+ }
+ return value;
+ }
+
+ /**
+ * Return {@code true} if there is a value present, otherwise {@code false}.
+ *
+ * @return {@code true} if there is a value present, otherwise {@code false}
+ */
+ public boolean isPresent() {
+ return isPresent;
+ }
+
+ /**
+ * Have the specified consumer accept the value if a value is present,
+ * otherwise do nothing.
+ *
+ * @param consumer block to be executed if a value is present
+ * @throws NullPointerException if value is present and {@code consumer} is
+ * null
+ */
+ public void ifPresent(LongConsumer consumer) {
+ if (isPresent)
+ consumer.accept(value);
+ }
+
+ /**
+ * Return the value if present, otherwise return {@code other}.
+ *
+ * @param other the value to be returned if there is no value present
+ * @return the value, if present, otherwise {@code other}
+ */
+ public long orElse(long other) {
+ return isPresent ? value : other;
+ }
+
+ /**
+ * Return the value if present, otherwise invoke {@code other} and return
+ * the result of that invocation.
+ *
+ * @param other a {@code LongSupplier} whose result is returned if no value
+ * is present
+ * @return the value if present otherwise the result of {@code other.getAsLong()}
+ * @throws NullPointerException if value is not present and {@code other} is
+ * null
+ */
+ public long orElseGet(LongSupplier other) {
+ return isPresent ? value : other.getAsLong();
+ }
+
+ /**
+ * Return the contained value, if present, otherwise throw an exception
+ * to be created by the provided supplier.
+ *
+ * @apiNote A method reference to the exception constructor with an empty
+ * argument list can be used as the supplier. For example,
+ * {@code IllegalStateException::new}
+ *
+ * @param <X> Type of the exception to be thrown
+ * @param exceptionSupplier The supplier which will return the exception to
+ * be thrown
+ * @return the present value
+ * @throws X if there is no value present
+ * @throws NullPointerException if no value is present and
+ * {@code exceptionSupplier} is null
+ */
+ public<X extends Throwable> long orElseThrow(Supplier<X> exceptionSupplier) throws X {
+ if (isPresent) {
+ return value;
+ } else {
+ throw exceptionSupplier.get();
+ }
+ }
+
+ /**
+ * Indicates whether some other object is "equal to" this Optional. The
+ * other object is considered equal if:
+ * <ul>
+ * <li>it is also an {@code OptionalInt} and;
+ * <li>both instances have no value present or;
+ * <li>the present values are "equal to" each other via {@code ==}.
+ * </ul>
+ *
+ * @param obj an object to be tested for equality
+ * @return {code true} if the other object is "equal to" this object
+ * otherwise {@code false}
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+
+ if (!(obj instanceof OptionalLong)) {
+ return false;
+ }
+
+ OptionalLong other = (OptionalLong) obj;
+ return (isPresent && other.isPresent)
+ ? value == other.value
+ : isPresent == other.isPresent;
+ }
+
+ /**
+ * Returns the hash code value of the present value, if any, or 0 (zero) if
+ * no value is present.
+ *
+ * @return hash code value of the present value or 0 if no value is present
+ */
+ @Override
+ public int hashCode() {
+ return isPresent ? Long.hashCode(value) : 0;
+ }
+
+ /**
+ * Returns a non-empty string representation of this OptionalLong suitable for
+ * debugging. The exact presentation format is unspecified and may vary
+ * between implementations and versions.
+ *
+ * @implSpec If a value is present the result must include its string
+ * representation in the result. Empty and present OptionalLongs must be
+ * unambiguously differentiable.
+ *
+ * @return the string representation of this instance
+ */
+ @Override
+ public String toString() {
+ return isPresent
+ ? String.format("OptionalLong[%s]", value)
+ : "OptionalLong.empty";
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Optional/Basic.java Tue Mar 19 16:05:34 2013 -0700
@@ -0,0 +1,122 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @summary Basic functional test of Optional
+ * @author Mike Duigou
+ * @run testng Basic
+ */
+
+import java.util.NoSuchElementException;
+import java.util.Optional;
+
+import static org.testng.Assert.*;
+import org.testng.annotations.Test;
+
+
+public class Basic {
+
+ @Test(groups = "unit")
+ public void testEmpty() {
+ Optional<Boolean> empty = Optional.empty();
+ Optional<String> presentEmptyString = Optional.of("");
+ Optional<Boolean> present = Optional.of(Boolean.TRUE);
+
+ // empty
+ assertTrue(empty.equals(empty));
+ assertTrue(empty.equals(Optional.empty()));
+ assertTrue(!empty.equals(present));
+ assertTrue(0 == empty.hashCode());
+ assertTrue(!empty.toString().isEmpty());
+ assertTrue(!empty.toString().equals(presentEmptyString.toString()));
+ assertTrue(!empty.isPresent());
+ empty.ifPresent(v -> { fail(); });
+ assertSame(null, empty.orElse(null));
+ RuntimeException orElse = new RuntimeException() { };
+ assertSame(Boolean.FALSE, empty.orElse(Boolean.FALSE));
+ assertSame(null, empty.orElseGet(()-> null));
+ assertSame(Boolean.FALSE, empty.orElseGet(()-> Boolean.FALSE));
+ }
+
+ @Test(expectedExceptions=NoSuchElementException.class)
+ public void testEmptyGet() {
+ Optional<Boolean> empty = Optional.empty();
+
+ Boolean got = empty.get();
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseGetNull() {
+ Optional<Boolean> empty = Optional.empty();
+
+ Boolean got = empty.orElseGet(null);
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseThrowNull() throws Throwable {
+ Optional<Boolean> empty = Optional.empty();
+
+ Boolean got = empty.orElseThrow(null);
+ }
+
+ @Test(expectedExceptions=ObscureException.class)
+ public void testEmptyOrElseThrow() throws Exception {
+ Optional<Boolean> empty = Optional.empty();
+
+ Boolean got = empty.orElseThrow(ObscureException::new);
+ }
+
+ @Test(groups = "unit")
+ public void testPresent() {
+ Optional<Boolean> empty = Optional.empty();
+ Optional<String> presentEmptyString = Optional.of("");
+ Optional<Boolean> present = Optional.of(Boolean.TRUE);
+
+ // present
+ assertTrue(present.equals(present));
+ assertTrue(present.equals(Optional.of(Boolean.TRUE)));
+ assertTrue(!present.equals(empty));
+ assertTrue(Boolean.TRUE.hashCode() == present.hashCode());
+ assertTrue(!present.toString().isEmpty());
+ assertTrue(!present.toString().equals(presentEmptyString.toString()));
+ assertTrue(-1 != present.toString().indexOf(Boolean.TRUE.toString()));
+ assertSame(Boolean.TRUE, present.get());
+ try {
+ present.ifPresent(v -> { throw new ObscureException(); });
+ fail();
+ } catch(ObscureException expected) {
+
+ }
+ assertSame(Boolean.TRUE, present.orElse(null));
+ assertSame(Boolean.TRUE, present.orElse(Boolean.FALSE));
+ assertSame(Boolean.TRUE, present.orElseGet(null));
+ assertSame(Boolean.TRUE, present.orElseGet(()-> null));
+ assertSame(Boolean.TRUE, present.orElseGet(()-> Boolean.FALSE));
+ assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow( null));
+ assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow(ObscureException::new));
+ }
+
+ private static class ObscureException extends RuntimeException {
+
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Optional/BasicDouble.java Tue Mar 19 16:05:34 2013 -0700
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2.0 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2.0 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2.0 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @summary Basic functional test of OptionalDouble
+ * @author Mike Duigou
+ * @run testng BasicDouble
+ */
+
+import java.util.NoSuchElementException;
+import java.util.OptionalDouble;
+
+import static org.testng.Assert.*;
+import org.testng.annotations.Test;
+
+
+public class BasicDouble {
+
+ @Test(groups = "unit")
+ public void testEmpty() {
+ OptionalDouble empty = OptionalDouble.empty();
+ OptionalDouble present = OptionalDouble.of(1.0);
+
+ // empty
+ assertTrue(empty.equals(empty));
+ assertTrue(empty.equals(OptionalDouble.empty()));
+ assertTrue(!empty.equals(present));
+ assertTrue(0 == empty.hashCode());
+ assertTrue(!empty.toString().isEmpty());
+ assertTrue(!empty.isPresent());
+ empty.ifPresent(v -> { fail(); });
+ assertEquals(2.0, empty.orElse(2.0));
+ assertEquals(2.0, empty.orElseGet(()-> 2.0));
+ }
+
+ @Test(expectedExceptions=NoSuchElementException.class)
+ public void testEmptyGet() {
+ OptionalDouble empty = OptionalDouble.empty();
+
+ double got = empty.getAsDouble();
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseGetNull() {
+ OptionalDouble empty = OptionalDouble.empty();
+
+ double got = empty.orElseGet(null);
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseThrowNull() throws Throwable {
+ OptionalDouble empty = OptionalDouble.empty();
+
+ double got = empty.orElseThrow(null);
+ }
+
+ @Test(expectedExceptions=ObscureException.class)
+ public void testEmptyOrElseThrow() throws Exception {
+ OptionalDouble empty = OptionalDouble.empty();
+
+ double got = empty.orElseThrow(ObscureException::new);
+ }
+
+ @Test(groups = "unit")
+ public void testPresent() {
+ OptionalDouble empty = OptionalDouble.empty();
+ OptionalDouble present = OptionalDouble.of(1.0);
+
+ // present
+ assertTrue(present.equals(present));
+ assertFalse(present.equals(OptionalDouble.of(0.0)));
+ assertTrue(present.equals(OptionalDouble.of(1.0)));
+ assertTrue(!present.equals(empty));
+ assertTrue(Double.hashCode(1.0) == present.hashCode());
+ assertFalse(present.toString().isEmpty());
+ assertTrue(-1 != present.toString().indexOf(Double.toString(present.getAsDouble()).toString()));
+ assertEquals(1.0, present.getAsDouble());
+ try {
+ present.ifPresent(v -> { throw new ObscureException(); });
+ fail();
+ } catch(ObscureException expected) {
+
+ }
+ assertEquals(1.0, present.orElse(2.0));
+ assertEquals(1.0, present.orElseGet(null));
+ assertEquals(1.0, present.orElseGet(()-> 2.0));
+ assertEquals(1.0, present.orElseGet(()-> 3.0));
+ assertEquals(1.0, present.<RuntimeException>orElseThrow(null));
+ assertEquals(1.0, present.<RuntimeException>orElseThrow(ObscureException::new));
+ }
+
+ private static class ObscureException extends RuntimeException {
+
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Optional/BasicInt.java Tue Mar 19 16:05:34 2013 -0700
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @summary Basic functional test of OptionalInt
+ * @author Mike Duigou
+ * @run testng BasicInt
+ */
+
+import java.util.NoSuchElementException;
+import java.util.OptionalInt;
+
+import static org.testng.Assert.*;
+import org.testng.annotations.Test;
+
+
+public class BasicInt {
+
+ @Test(groups = "unit")
+ public void testEmpty() {
+ OptionalInt empty = OptionalInt.empty();
+ OptionalInt present = OptionalInt.of(1);
+
+ // empty
+ assertTrue(empty.equals(empty));
+ assertTrue(empty.equals(OptionalInt.empty()));
+ assertTrue(!empty.equals(present));
+ assertTrue(0 == empty.hashCode());
+ assertTrue(!empty.toString().isEmpty());
+ assertTrue(!empty.isPresent());
+ empty.ifPresent(v -> { fail(); });
+ assertEquals(2, empty.orElse(2));
+ assertEquals(2, empty.orElseGet(()-> 2));
+ }
+
+ @Test(expectedExceptions=NoSuchElementException.class)
+ public void testEmptyGet() {
+ OptionalInt empty = OptionalInt.empty();
+
+ int got = empty.getAsInt();
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseGetNull() {
+ OptionalInt empty = OptionalInt.empty();
+
+ int got = empty.orElseGet(null);
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseThrowNull() throws Throwable {
+ OptionalInt empty = OptionalInt.empty();
+
+ int got = empty.orElseThrow(null);
+ }
+
+ @Test(expectedExceptions=ObscureException.class)
+ public void testEmptyOrElseThrow() throws Exception {
+ OptionalInt empty = OptionalInt.empty();
+
+ int got = empty.orElseThrow(ObscureException::new);
+ }
+
+ @Test(groups = "unit")
+ public void testPresent() {
+ OptionalInt empty = OptionalInt.empty();
+ OptionalInt present = OptionalInt.of(1);
+
+ // present
+ assertTrue(present.equals(present));
+ assertFalse(present.equals(OptionalInt.of(0)));
+ assertTrue(present.equals(OptionalInt.of(1)));
+ assertFalse(present.equals(empty));
+ assertTrue(Integer.hashCode(1) == present.hashCode());
+ assertFalse(present.toString().isEmpty());
+ assertTrue(-1 != present.toString().indexOf(Integer.toString(present.getAsInt()).toString()));
+ assertEquals(1, present.getAsInt());
+ try {
+ present.ifPresent(v -> { throw new ObscureException(); });
+ fail();
+ } catch(ObscureException expected) {
+
+ }
+ assertEquals(1, present.orElse(2));
+ assertEquals(1, present.orElseGet(null));
+ assertEquals(1, present.orElseGet(()-> 2));
+ assertEquals(1, present.orElseGet(()-> 3));
+ assertEquals(1, present.<RuntimeException>orElseThrow(null));
+ assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new));
+ }
+
+ private static class ObscureException extends RuntimeException {
+
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Optional/BasicLong.java Tue Mar 19 16:05:34 2013 -0700
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @summary Basic functional test of OptionalLong
+ * @author Mike Duigou
+ * @run testng BasicLong
+ */
+
+import java.util.NoSuchElementException;
+import java.util.OptionalLong;
+
+import static org.testng.Assert.*;
+import org.testng.annotations.Test;
+
+
+public class BasicLong {
+
+ @Test(groups = "unit")
+ public void testEmpty() {
+ OptionalLong empty = OptionalLong.empty();
+ OptionalLong present = OptionalLong.of(1);
+
+ // empty
+ assertTrue(empty.equals(empty));
+ assertTrue(empty.equals(OptionalLong.empty()));
+ assertTrue(!empty.equals(present));
+ assertTrue(0 == empty.hashCode());
+ assertTrue(!empty.toString().isEmpty());
+ assertTrue(!empty.isPresent());
+ empty.ifPresent(v -> { fail(); });
+ assertEquals(2, empty.orElse(2));
+ assertEquals(2, empty.orElseGet(()-> 2));
+ }
+
+ @Test(expectedExceptions=NoSuchElementException.class)
+ public void testEmptyGet() {
+ OptionalLong empty = OptionalLong.empty();
+
+ long got = empty.getAsLong();
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseGetNull() {
+ OptionalLong empty = OptionalLong.empty();
+
+ long got = empty.orElseGet(null);
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseThrowNull() throws Throwable {
+ OptionalLong empty = OptionalLong.empty();
+
+ long got = empty.orElseThrow(null);
+ }
+
+ @Test(expectedExceptions=ObscureException.class)
+ public void testEmptyOrElseThrow() throws Exception {
+ OptionalLong empty = OptionalLong.empty();
+
+ long got = empty.orElseThrow(ObscureException::new);
+ }
+
+ @Test(groups = "unit")
+ public void testPresent() {
+ OptionalLong empty = OptionalLong.empty();
+ OptionalLong present = OptionalLong.of(1L);
+
+ // present
+ assertTrue(present.equals(present));
+ assertFalse(present.equals(OptionalLong.of(0L)));
+ assertTrue(present.equals(OptionalLong.of(1L)));
+ assertFalse(present.equals(empty));
+ assertTrue(Long.hashCode(1) == present.hashCode());
+ assertFalse(present.toString().isEmpty());
+ assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString()));
+ assertEquals(1L, present.getAsLong());
+ try {
+ present.ifPresent(v -> { throw new ObscureException(); });
+ fail();
+ } catch(ObscureException expected) {
+
+ }
+ assertEquals(1, present.orElse(2));
+ assertEquals(1, present.orElseGet(null));
+ assertEquals(1, present.orElseGet(()-> 2));
+ assertEquals(1, present.orElseGet(()-> 3));
+ assertEquals(1, present.<RuntimeException>orElseThrow(null));
+ assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new));
+ }
+
+ private static class ObscureException extends RuntimeException {
+
+ }
+}