Class Maybe<T>
- Type Parameters:
T- the type of value
null value.
If a value is present, isPresent() returns true. If no
value is present, the object is considered empty and
isPresent() returns false.
Additional methods that depend on the presence or absence of a contained
value are provided, such as orElse()
(returns a default value if no value is present) and
ifPresent() (performs an
action if a value is present).
Maybe is primarily intended for use as a method return type where
there is a clear need to represent "no result," and where using null
is likely to cause errors. A variable whose type is Maybe should
never itself be null; it should always point to an Maybe
instance.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Maybe<T> empty()Returns an emptyMaybeinstance.If a value is present, and the value matches the given predicate, returns anMaybedescribing the value, otherwise returns an emptyMaybe.<R> Maybe<R> If a value is present, returns the result of applying the givenMaybe-bearing mapping function to the value, otherwise returns an emptyMaybe.voidIf a value is present, performs the given action with the value, otherwise does nothing.<R> Maybe<R> If a value is present, returns anMaybedescribing the result of applying the given mapping function to the value, otherwise returns an emptyMaybe.static <T> Maybe<T> of(T value) Returns anMaybedescribing the given value, if non-null, otherwise returns an emptyMaybe.If a value is present, returns anMaybedescribing the value, otherwise returns anMaybeproduced by the supplying function.If a value is present, returns the value, otherwise returnsother.If a value is present, returns the value, otherwise returns the result produced by the supplying function.toString()Returns a non-empty string representation of thisMaybesuitable for debugging.
-
Method Details
-
of
Returns anMaybedescribing the given value, if non-null, otherwise returns an emptyMaybe.- Type Parameters:
T- the type of the value- Parameters:
value- the possibly-nullvalue to describe- Returns:
- an
Maybewith a present value if the specified value is non-null, otherwise an emptyMaybe
-
empty
Returns an emptyMaybeinstance. No value is present for thisMaybe.- Type Parameters:
T- The type of the non-existent value- Returns:
- an empty
Maybe
-
filter
If a value is present, and the value matches the given predicate, returns anMaybedescribing the value, otherwise returns an emptyMaybe.- Parameters:
predicate- the predicate to apply to a value, if present- Returns:
- an
Maybedescribing the value of thisMaybe, if a value is present and the value matches the given predicate, otherwise an emptyMaybe - Throws:
NullPointerException- if the predicate isnull
-
ifPresent
If a value is present, performs the given action with the value, otherwise does nothing.- Parameters:
action- the action to be performed, if a value is present- Throws:
NullPointerException- if value is present and the given action isnull
-
map
If a value is present, returns anMaybedescribing the result of applying the given mapping function to the value, otherwise returns an emptyMaybe.If the mapping function returns a
nullresult then this method returns an emptyMaybe.- Type Parameters:
R- The type of the value returned from the mapping function- Parameters:
mapper- the mapping function to apply to a value, if present- Returns:
- an
Maybedescribing the result of applying a mapping function to the value of thisMaybe, if a value is present, otherwise an emptyMaybe - Throws:
NullPointerException- if the mapping function isnull
-
flatMap
If a value is present, returns the result of applying the givenMaybe-bearing mapping function to the value, otherwise returns an emptyMaybe.This method is similar to
map(Function), but the mapping function is one whose result is already anMaybe, and if invoked,flatMapdoes not wrap it within an additionalMaybe.- Type Parameters:
R- The type of value of theMaybereturned by the mapping function- Parameters:
mapper- the mapping function to apply to a value, if present- Returns:
- the result of applying an
Maybe-bearing mapping function to the value of thisMaybe, if a value is present, otherwise an emptyMaybe - Throws:
NullPointerException- if the mapping function isnullor returns anullresult
-
or
If a value is present, returns anMaybedescribing the value, otherwise returns anMaybeproduced by the supplying function.- Parameters:
supplier- the supplying function that produces anMaybeto be returned- Returns:
- returns an
Maybedescribing the value of thisMaybe, if a value is present, otherwise anMaybeproduced by the supplying function. - Throws:
NullPointerException- if the supplying function isnullor produces anullresult
-
orElse
If a value is present, returns the value, otherwise returnsother.- Parameters:
other- the value to be returned, if no value is present. May benull.- Returns:
- the value, if present, otherwise
other
-
orElseGet
If a value is present, returns the value, otherwise returns the result produced by the supplying function.- Parameters:
supplier- the supplying function that produces a value to be returned- Returns:
- the value, if present, otherwise the result produced by the supplying function
- Throws:
NullPointerException- if no value is present and the supplying function isnull
-
toString
Returns a non-empty string representation of thisMaybesuitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions.If a value is present the result must include its string representation in the result. Empty and present
Maybes must be unambiguously differentiable.
-