@GwtCompatible final class Absent<T> extends Optional<T>
Optional not containing a reference.| Modifier and Type | Field and Description |
|---|---|
(package private) static Absent<java.lang.Object> |
INSTANCE |
private static long |
serialVersionUID |
| Modifier | Constructor and Description |
|---|---|
private |
Absent() |
| Modifier and Type | Method and Description |
|---|---|
java.util.Set<T> |
asSet()
Returns an immutable singleton
Set whose only element is the contained instance
if it is present; an empty immutable Set otherwise. |
boolean |
equals(java.lang.Object object)
Returns
true if object is an Optional instance, and either
the contained references are equal to each other or both
are absent. |
T |
get()
Returns the contained instance, which must be present.
|
int |
hashCode()
Returns a hash code for this instance.
|
boolean |
isPresent()
Returns
true if this holder contains a (non-null) instance. |
Optional<T> |
or(Optional<? extends T> secondChoice)
Returns this
Optional if it has a value present; secondChoice
otherwise. |
T |
or(Supplier<? extends T> supplier)
Returns the contained instance if it is present;
supplier.get() otherwise. |
T |
or(T defaultValue)
Returns the contained instance if it is present;
defaultValue otherwise. |
T |
orNull()
Returns the contained instance if it is present;
null otherwise. |
private java.lang.Object |
readResolve() |
java.lang.String |
toString()
Returns a string representation for this instance.
|
<V> Optional<V> |
transform(Function<? super T,V> function)
If the instance is present, it is transformed with the given
Function; otherwise,
Optional.absent() is returned. |
(package private) static <T> Optional<T> |
withType() |
absent, fromNullable, of, presentInstancesstatic final Absent<java.lang.Object> INSTANCE
private static final long serialVersionUID
static <T> Optional<T> withType()
public boolean isPresent()
Optionaltrue if this holder contains a (non-null) instance.public T get()
OptionalOptional.or(Object) or Optional.orNull() instead.public T or(T defaultValue)
OptionaldefaultValue otherwise. If
no default value should be required because the instance is known to be present, use
Optional.get() instead. For a default value of null, use Optional.orNull().
Note about generics: The signature public T or(T defaultValue) is overly
restrictive. However, the ideal signature, public <S super T> S or(S), is not legal
Java. As a result, some sensible operations involving subtypes are compile errors:
Optional<Integer> optionalInt = getSomeOptionalInt();
Number value = optionalInt.or(0.5); // error
FluentIterable<? extends Number> numbers = getSomeNumbers();
Optional<? extends Number> first = numbers.first();
Number value = first.or(0.5); // error
As a workaround, it is always safe to cast an Optional<? extends T> to Optional<T>. Casting either of the above example Optional instances to Optional<Number> (where Number is the desired output type) solves the problem:
Optional<Number> optionalInt = (Optional) getSomeOptionalInt();
Number value = optionalInt.or(0.5); // fine
FluentIterable<? extends Number> numbers = getSomeNumbers();
Optional<Number> first = (Optional) numbers.first();
Number value = first.or(0.5); // finepublic Optional<T> or(Optional<? extends T> secondChoice)
OptionalOptional if it has a value present; secondChoice
otherwise.public T or(Supplier<? extends T> supplier)
Optionalsupplier.get() otherwise. If the
supplier returns null, a NullPointerException is thrown.@Nullable public T orNull()
Optionalnull otherwise. If the
instance is known to be present, use Optional.get() instead.public java.util.Set<T> asSet()
OptionalSet whose only element is the contained instance
if it is present; an empty immutable Set otherwise.public <V> Optional<V> transform(Function<? super T,V> function)
OptionalFunction; otherwise,
Optional.absent() is returned. If the function returns null, a
NullPointerException is thrown.public boolean equals(@Nullable
java.lang.Object object)
Optionaltrue if object is an Optional instance, and either
the contained references are equal to each other or both
are absent. Note that Optional instances of differing parameterized types can
be equal.public int hashCode()
Optionalpublic java.lang.String toString()
Optionalprivate java.lang.Object readResolve()