T - type of value held insidepublic final class Optional<T>
extends java.lang.Object
implements java.io.Serializable
Like java.util.Optional in Java 8, scala.Option in Scala, and
com.google.common.base.Optional in Google Guava, this class represents a
value of a given type that may or may not exist. It is used in methods that wish
to optionally return a value, in preference to returning null.
In fact, the class here is a reimplementation of the essential API of both
java.util.Optional and com.google.common.base.Optional. From
java.util.Optional, it implements:
From com.google.common.base.Optional it implements:
java.util.Optional itself is not used at this time because the
project does not require Java 8. Using com.google.common.base.Optional
has in the past caused serious library version conflicts with Guava that can't
be resolved by shading. Hence this work-alike clone.
| Modifier and Type | Method and Description |
|---|---|
static <T> Optional<T> |
absent() |
static <T> Optional<T> |
empty() |
boolean |
equals(java.lang.Object obj) |
static <T> Optional<T> |
fromNullable(T value) |
T |
get() |
int |
hashCode() |
boolean |
isPresent() |
static <T> Optional<T> |
of(T value) |
static <T> Optional<T> |
ofNullable(T value) |
T |
or(T other) |
T |
orElse(T other) |
T |
orNull() |
java.lang.String |
toString() |
public static <T> Optional<T> empty()
Optionalpublic static <T> Optional<T> of(T value)
value - non-null value to wrapOptional wrapping this valuejava.lang.NullPointerException - if value is nullpublic static <T> Optional<T> ofNullable(T value)
value - value to wrap, which may be nullOptional wrapping this value, which may be emptypublic T get()
Optionaljava.lang.NullPointerException - if this is empty (contains no value)public T orElse(T other)
other - value to return if this is emptyOptional's value if present, or else the given valuepublic boolean isPresent()
Optional contains a value (non-empty)public static <T> Optional<T> absent()
Optionalpublic static <T> Optional<T> fromNullable(T value)
value - value to wrap, which may be nullOptional wrapping this value, which may be emptypublic T or(T other)
other - value to return if this is emptyOptional's value if present, or else the given valuepublic T orNull()
Optional's value if present, or else nullpublic boolean equals(java.lang.Object obj)
equals in class java.lang.Objectpublic int hashCode()
hashCode in class java.lang.Objectpublic java.lang.String toString()
toString in class java.lang.Object