Class Present<T>
- java.lang.Object
-
- com.google.common.base.Optional<T>
-
- com.google.common.base.Present<T>
-
- All Implemented Interfaces:
java.io.Serializable
@GwtCompatible final class Present<T> extends Optional<T>
Implementation of anOptional
containing a reference.
-
-
Field Summary
Fields Modifier and Type Field Description private T
reference
private static long
serialVersionUID
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Set<T>
asSet()
Returns an immutable singletonSet
whose only element is the contained instance if it is present; an empty immutableSet
otherwise.boolean
equals(java.lang.Object object)
Returnstrue
ifobject
is anOptional
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()
Returnstrue
if this holder contains a (non-null) instance.Optional<T>
or(Optional<? extends T> secondChoice)
Returns thisOptional
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.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 givenFunction
; otherwise,Optional.absent()
is returned.-
Methods inherited from class com.google.common.base.Optional
absent, fromNullable, of, presentInstances
-
-
-
-
Field Detail
-
reference
private final T reference
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Present
Present(T reference)
-
-
Method Detail
-
isPresent
public boolean isPresent()
Description copied from class:Optional
Returnstrue
if this holder contains a (non-null) instance.Comparison to
java.util.Optional
: no differences.
-
get
public T get()
Description copied from class:Optional
Returns the contained instance, which must be present. If the instance might be absent, useOptional.or(Object)
orOptional.orNull()
instead.Comparison to
java.util.Optional
: when the value is absent, this method throwsIllegalStateException
, whereas the Java 8 counterpart throwsNoSuchElementException
.
-
or
public T or(T defaultValue)
Description copied from class:Optional
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, useOptional.get()
instead. For a default value ofnull
, useOptional.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>
toOptional<T>
. Casting either of the above exampleOptional
instances toOptional<Number>
(whereNumber
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'sOptional.orElse
, but will not acceptnull
as adefaultValue
(Optional.orNull()
must be used instead). As a result, the value returned by this method is guaranteed non-null, which is not the case for thejava.util
equivalent.
-
or
public Optional<T> or(Optional<? extends T> secondChoice)
Description copied from class:Optional
Returns thisOptional
if it has a value present;secondChoice
otherwise.Comparison to
java.util.Optional
: this method has no equivalent in Java 8'sOptional
class; writethisOptional.isPresent() ? thisOptional : secondChoice
instead.
-
or
public T or(Supplier<? extends T> supplier)
Description copied from class:Optional
Returns the contained instance if it is present;supplier.get()
otherwise.Comparison to
java.util.Optional
: this method is similar to Java 8'sOptional.orElseGet
, except whensupplier
returnsnull
. In this case this method throws an exception, whereas the Java 8 method returns thenull
to the caller.
-
orNull
public T orNull()
Description copied from class:Optional
Returns the contained instance if it is present;null
otherwise. If the instance is known to be present, useOptional.get()
instead.Comparison to
java.util.Optional
: this method is equivalent to Java 8'sOptional.orElse(null)
.
-
asSet
public java.util.Set<T> asSet()
Description copied from class:Optional
Returns an immutable singletonSet
whose only element is the contained instance if it is present; an empty immutableSet
otherwise.Comparison to
java.util.Optional
: this method has no equivalent in Java 8'sOptional
class. However, this common usage:for (Foo foo : possibleFoo.asSet()) { doSomethingWith(foo); }
possibleFoo.ifPresent(foo -> doSomethingWith(foo));
-
transform
public <V> Optional<V> transform(Function<? super T,V> function)
Description copied from class:Optional
If the instance is present, it is transformed with the givenFunction
; otherwise,Optional.absent()
is returned.Comparison to
java.util.Optional
: this method is similar to Java 8'sOptional.map
, except whenfunction
returnsnull
. In this case this method throws an exception, whereas the Java 8 method returnsOptional.absent()
.
-
equals
public boolean equals(@Nullable java.lang.Object object)
Description copied from class:Optional
Returnstrue
ifobject
is anOptional
instance, and either the contained references are equal to each other or both are absent. Note thatOptional
instances of differing parameterized types can be equal.Comparison to
java.util.Optional
: no differences.
-
hashCode
public int hashCode()
Description copied from class:Optional
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.
-
toString
public java.lang.String toString()
Description copied from class:Optional
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.
-
-