Top Description Fields Constructors Methods
com.google.common.base

pack-priv final Class Absent<T>

extends Optional<T>
Class Inheritance
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull

Implementation of an Optional not containing a reference.

Field Summary

Modifier and TypeField and Description
pack-priv static final Absent<Object>
private static final long

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Set<T>
asSet()

Implements abstract com.google.common.base.Optional.asSet.

Returns an immutable singleton Set whose only element is the contained instance if it is present; an empty immutable Set otherwise.
public boolean
equals(Object
the reference object with which to compare.
object
)

Implements abstract com.google.common.base.Optional.equals.

Returns true if object is an Optional instance, and either the contained references are equal to each other or both are absent.
public T
get()

Implements abstract com.google.common.base.Optional.get.

Returns the contained instance, which must be present.
public int
hashCode()

Implements abstract com.google.common.base.Optional.hashCode.

Returns a hash code for this instance.
public boolean
isPresent()

Implements abstract com.google.common.base.Optional.isPresent.

Returns true if this holder contains a (non-null) instance.
public T
or(T defaultValue)

Implements abstract com.google.common.base.Optional.or.

Returns the contained instance if it is present; defaultValue otherwise.
public Optional<T>
or(Optional<? extends T> secondChoice)

Implements abstract com.google.common.base.Optional.or.

Returns this Optional if it has a value present; secondChoice otherwise.
public T
or(Supplier<? extends T> supplier)

Implements abstract com.google.common.base.Optional.or.

Returns the contained instance if it is present; supplier.get() otherwise.
public T
orNull()

Implements abstract com.google.common.base.Optional.orNull.

Returns the contained instance if it is present; null otherwise.
private Object
public String
toString()

Implements abstract com.google.common.base.Optional.toString.

Returns a string representation for this instance.
public <V> Optional<V>
transform(Function<? super T, V> function)

Implements abstract com.google.common.base.Optional.transform.

If the instance is present, it is transformed with the given Function; otherwise, Optional#absent is returned.
pack-priv static <T> Optional<T>
Inherited from com.google.common.base.Optional:
absentfromJavaUtilfromNullableofpresentInstancestoJavaUtiltoJavaUtil

Field Detail

INSTANCEback to summary
pack-priv static final Absent<Object> INSTANCE
serialVersionUIDback to summary
private static final long serialVersionUID

Hides com.google.common.base.Optional.serialVersionUID.

Constructor Detail

Absentback to summary
private Absent()

Method Detail

asSetback to summary
public Set<T> asSet()

Implements abstract com.google.common.base.Optional.asSet.

Doc from com.google.common.base.Optional.asSet.

Returns an immutable singleton Set whose only element is the contained instance if it is present; an empty immutable Set otherwise.

Comparison to java.util.Optional: this method has no equivalent in Java 8's Optional class. However, this common usage:

for (Foo foo : possibleFoo.asSet()) {
  doSomethingWith(foo);
}
... can be replaced with:
possibleFoo.ifPresent(foo -> doSomethingWith(foo));

Java 9 users: some use cases can be written with calls to optional.stream().

Annotations
@Override
equalsback to summary
public boolean equals(Object object)

Implements abstract com.google.common.base.Optional.equals.

Doc from com.google.common.base.Optional.equals.

Returns true 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.

Comparison to java.util.Optional: no differences.

Parameters
object:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getback to summary
public T get()

Implements abstract com.google.common.base.Optional.get.

Doc from com.google.common.base.Optional.get.

Returns the contained instance, which must be present. If the instance might be absent, use or(Object) or orNull instead.

Comparison to java.util.Optional: when the value is absent, this method throws IllegalStateException, whereas the Java 8 counterpart throws NoSuchElementException.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Implements abstract com.google.common.base.Optional.hashCode.

Doc from com.google.common.base.Optional.hashCode.

Returns a hash code for this instance.

Comparison to java.util.Optional: this class leaves the specific choice of hash code unspecified, unlike the Java 8 equivalent.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object.

Annotations
@Override
isPresentback to summary
public boolean isPresent()

Implements abstract com.google.common.base.Optional.isPresent.

Doc from com.google.common.base.Optional.isPresent.

Returns true if this holder contains a (non-null) instance.

Comparison to java.util.Optional: no differences.

Annotations
@Override
orback to summary
public T or(T defaultValue)

Implements abstract com.google.common.base.Optional.or.

Doc from com.google.common.base.Optional.or.

Returns the contained instance if it is present; defaultValue otherwise. If no default value should be required because the instance is known to be present, use get() instead. For a default value of null, use 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); // fine

Comparison to java.util.Optional: this method is similar to Java 8's Optional.orElse, but will not accept null as a defaultValue (orNull must be used instead). As a result, the value returned by this method is guaranteed non-null, which is not the case for the java.util equivalent.

Annotations
@Override
orback to summary
public Optional<T> or(Optional<? extends T> secondChoice)

Implements abstract com.google.common.base.Optional.or.

Doc from com.google.common.base.Optional.or.

Returns this Optional if it has a value present; secondChoice otherwise.

Comparison to java.util.Optional: this method has no equivalent in Java 8's Optional class; write thisOptional.isPresent() ? thisOptional : secondChoice instead.

Annotations
@SuppressWarnings:unchecked
@Override
orback to summary
public T or(Supplier<? extends T> supplier)

Implements abstract com.google.common.base.Optional.or.

Doc from com.google.common.base.Optional.or.

Returns the contained instance if it is present; supplier.get() otherwise.

Comparison to java.util.Optional: this method is similar to Java 8's Optional.orElseGet, except when supplier returns null. In this case this method throws an exception, whereas the Java 8 method returns the null to the caller.

Annotations
@Override
orNullback to summary
public T orNull()

Implements abstract com.google.common.base.Optional.orNull.

Doc from com.google.common.base.Optional.orNull.

Returns the contained instance if it is present; null otherwise. If the instance is known to be present, use get() instead.

Comparison to java.util.Optional: this method is equivalent to Java 8's Optional.orElse(null).

Annotations
@Override
@CheckForNull
readResolveback to summary
private Object readResolve()
toStringback to summary
public String toString()

Implements abstract com.google.common.base.Optional.toString.

Doc from com.google.common.base.Optional.toString.

Returns a string representation for this instance.

Comparison to java.util.Optional: this class leaves the specific string representation unspecified, unlike the Java 8 equivalent.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object.

Annotations
@Override
transformback to summary
public <V> Optional<V> transform(Function<? super T, V> function)

Implements abstract com.google.common.base.Optional.transform.

Doc from com.google.common.base.Optional.transform.

If the instance is present, it is transformed with the given Function; otherwise, Optional#absent is returned.

Comparison to java.util.Optional: this method is similar to Java 8's Optional.map, except when function returns null. In this case this method throws an exception, whereas the Java 8 method returns Optional.absent().

Annotations
@Override
withTypeback to summary
pack-priv static <T> Optional<T> withType()
Annotations
@SuppressWarnings:unchecked