Class Absent<T>
- All Implemented Interfaces:
Serializable
Optional not containing a reference.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionasSet()booleanReturnstrueifobjectis anOptionalinstance, and either the contained references are equal to each other or both are absent.get()Returns the contained instance, which must be present.inthashCode()Returns a hash code for this instance.booleanReturnstrueif this holder contains a (non-null) instance.Returns thisOptionalif it has a value present;secondChoiceotherwise.Returns the contained instance if it is present;supplier.get()otherwise.Returns the contained instance if it is present;defaultValueotherwise.orNull()Returns the contained instance if it is present;nullotherwise.private ObjecttoString()Returns a string representation for this instance.<V> Optional<V> If the instance is present, it is transformed with the givenFunction; otherwise,Optional.absent()is returned.(package private) static <T> Optional<T> withType()Methods inherited from class com.google.common.base.Optional
absent, fromJavaUtil, fromNullable, of, presentInstances, toJavaUtil, toJavaUtil
-
Field Details
-
INSTANCE
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
-
Constructor Details
-
Absent
private Absent()
-
-
Method Details
-
withType
-
isPresent
public boolean isPresent()Description copied from class:OptionalReturnstrueif this holder contains a (non-null) instance.Comparison to
java.util.Optional: no differences. -
get
Description copied from class:OptionalReturns 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 thejava.utilcounterpart throwsNoSuchElementException. -
or
Description copied from class:OptionalReturns the contained instance if it is present;defaultValueotherwise. 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); // errorAs a workaround, it is always safe to cast an
Optional<? extends T>toOptional<T>. Casting either of the above exampleOptionalinstances toOptional<Number>(whereNumberis 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); // fineComparison to
java.util.Optional: this method is similar to Java 8'sOptional.orElse, but will not acceptnullas 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.utilequivalent. -
or
Description copied from class:OptionalReturns thisOptionalif it has a value present;secondChoiceotherwise.Comparison to
java.util.Optional: this method has no equivalent in Java 8'sOptionalclass; writethisOptional.isPresent() ? thisOptional : secondChoiceinstead. -
or
Description copied from class:OptionalReturns 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 whensupplierreturnsnull. In this case this method throws an exception, whereas the Java 8+ method returns thenullto the caller. -
orNull
Description copied from class:OptionalReturns the contained instance if it is present;nullotherwise. 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
Description copied from class:OptionalReturns an immutable singletonSetwhose only element is the contained instance if it is present; an empty immutableSetotherwise.Comparison to
java.util.Optional: this method has no equivalent in Java 8'sOptionalclass. However, this common usage:
... can be replaced with:for (Foo foo : possibleFoo.asSet()) { doSomethingWith(foo); }possibleFoo.ifPresent(foo -> doSomethingWith(foo));Java 9 users: some use cases can be written with calls to
optional.stream(). -
transform
Description copied from class:OptionalIf 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 whenfunctionreturnsnull. In this case this method throws an exception, whereas the Java 8+ method returnsOptional.absent(). -
equals
Description copied from class:OptionalReturnstrueifobjectis anOptionalinstance, and either the contained references are equal to each other or both are absent. Note thatOptionalinstances of differing parameterized types can be equal.Comparison to
java.util.Optional: no differences. -
hashCode
public int hashCode()Description copied from class:OptionalReturns 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
Description copied from class:OptionalReturns a string representation for this instance.Comparison to
java.util.Optional: this class leaves the specific string representation unspecified, unlike the Java 8+ equivalent. -
readResolve
-