< prev index next >

src/java.base/share/classes/java/util/Optional.java

Print this page

        

*** 59,76 **** */ public final class Optional<T> { /** * Common instance for {@code empty()}. */ ! private static final Optional<?> EMPTY = new Optional<>(null); /** * If non-null, the value; if null, indicates no value is present */ private final T value; /** * Returns an empty {@code Optional} instance. No value is present for this * {@code Optional}. * * @apiNote * Though it may be tempting to do so, avoid testing if an object is empty --- 59,86 ---- */ 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; /** + * Constructs 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 * {@code Optional}. * * @apiNote * Though it may be tempting to do so, avoid testing if an object is empty
*** 88,103 **** } /** * Constructs an instance with the described value. * ! * @param value the value to describe; it's the caller's responsibility to ! * ensure the value is non-{@code null} unless creating the singleton ! * instance returned by {@code empty()}. */ private Optional(T value) { ! this.value = value; } /** * Returns an {@code Optional} describing the given non-{@code null} * value. --- 98,112 ---- } /** * Constructs an instance with the described value. * ! * @param value the non-{@code null} value to describe ! * @throws NullPointerException if value is {@code null} */ private Optional(T value) { ! this.value = Objects.requireNonNull(value); } /** * Returns an {@code Optional} describing the given non-{@code null} * value.
*** 106,116 **** * @param <T> the type of the value * @return an {@code Optional} with the value present * @throws NullPointerException if value is {@code null} */ public static <T> Optional<T> of(T value) { ! return new Optional<>(Objects.requireNonNull(value)); } /** * Returns an {@code Optional} describing the given value, if * non-{@code null}, otherwise returns an empty {@code Optional}. --- 115,125 ---- * @param <T> the type of the value * @return an {@code Optional} with the value present * @throws NullPointerException if value is {@code null} */ public static <T> Optional<T> of(T value) { ! return new Optional<>(value); } /** * Returns an {@code Optional} describing the given value, if * non-{@code null}, otherwise returns an empty {@code Optional}.
*** 118,131 **** * @param value the possibly-{@code null} value to describe * @param <T> the type of the value * @return an {@code Optional} with a present value if the specified value * is non-{@code null}, otherwise an empty {@code Optional} */ - @SuppressWarnings("unchecked") public static <T> Optional<T> ofNullable(T value) { ! return value == null ? (Optional<T>) EMPTY ! : new Optional<>(value); } /** * If a value is present, returns the value, otherwise throws * {@code NoSuchElementException}. --- 127,138 ---- * @param value the possibly-{@code null} value to describe * @param <T> the type of the value * @return an {@code Optional} with a present value if the specified value * is non-{@code null}, otherwise an empty {@code Optional} */ public static <T> Optional<T> ofNullable(T value) { ! return value == null ? empty() : of(value); } /** * If a value is present, returns the value, otherwise throws * {@code NoSuchElementException}.
< prev index next >