Package com.google.mu.util
Class BiOptional<A,B>
java.lang.Object
com.google.mu.util.BiOptional<A,B>
An optional pair of values; either the pair is present with both values, or is absent with no
value.
This is essentially an Optional<Pair>
that users can access through fluent methods
like map(java.util.function.BiFunction<? super A, ? super B, ? extends T>)
, filter(java.util.function.BiPredicate<? super A, ? super B>)
, or(java.util.function.Supplier<? extends com.google.mu.util.BiOptional<? extends A, ? extends B>>)
instead of opaque names like
first
, second
.
- Since:
- 5.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <A,
B> BiOptional <A, B> empty()
Returns an emptyBiOptional
instance.abstract BiOptional
<A, B> filter
(BiPredicate<? super A, ? super B> predicate) If a pair of values is present and matchespredicate
, return thisBiOptional instance
.abstract <T> Optional
<T> flatMap
(BiFunction<? super A, ? super B, ? extends Optional<? extends T>> mapper) If a pair of values is present, apply theBiOptional
-bearingmapper
to them, and return that result.static <T,
A, B> BiOptional <A, B> flatMap
(Optional<T> optional, Function<? super T, ? extends BiOptional<? extends A, ? extends B>> mapper) Maps the value contained inoptional
to aBiOptional
usingmapper
, or else returns empty.static <T> BiOptional
<T, T> Ifoptional
is present with valuev
, adapts it toBiOptional
containing values(v, v)
.abstract void
ifPresent
(BiConsumer<? super A, ? super B> consumer) If a pair of values is present, invokeconsumer
with them.abstract boolean
Returns true if the pair of values is present.abstract <A2,
B2> BiOptional <A2, B2> map
(BiFunction<? super A, ? super B, ? extends A2> aMapper, BiFunction<? super A, ? super B, ? extends B2> bMapper) If a pair of values is present, applyaMapper
andbMapper
to them, and if both results are non-null, return a newBiOptional
instance containing the results.abstract <T> Optional
<T> map
(BiFunction<? super A, ? super B, ? extends T> mapper) If a pair of values is present, applymapper
to them, and if the result is non-null, return anOptional
containing it.abstract <A2,
B2> BiOptional <A2, B2> If a pair of values is present, applyaMapper
andbMapper
to each respectively, and if both results are non-null, return a newBiOptional
instance containing the results.abstract boolean
matches
(BiPredicate<? super A, ? super B> condition) Returns true if the pair exists and satisfies thecondition
predicate.static <A,
B> BiOptional <A, B> of
(A a, B b) Returns aBiOptional
containing the pair(a, b)
.abstract BiOptional
<A, B> or
(Supplier<? extends BiOptional<? extends A, ? extends B>> alternative) If a pair of values are present, return thisBiOptional
instance.Returns the pair if present, or else returns(a, b)
.Ensures that the pair must be present or else throwsNoSuchElementException
.orElseThrow
(String message, Object... args) Ensures that the pair must be present or else throwsNoSuchElementException
withmessage
formatted withargs
.orElseThrow
(Function<String, E> exceptionFactory, String message, Object... args) Ensures that the pair must be present or else throws the exception returned byexceptionFactory
withmessage
formatted withargs
.orElseThrow
(Supplier<E> exceptionSupplier) Ensures that the pair must be present or else throws the exception returned byexceptionSupplier
.abstract BiOptional
<A, B> peek
(BiConsumer<? super A, ? super B> consumer) Invokesconsumer
with the pair if present and returns this object as is.BiOptional
<A, B> skipIf
(BiPredicate<? super A, ? super B> predicate) If a pair of values is present and matchespredicate
, the pair is skipped (returns empty).stream()
Returns aBiStream
view of this BiOptional.
-
Method Details
-
empty
Returns an emptyBiOptional
instance. -
of
Returns aBiOptional
containing the pair(a, b)
.- Throws:
NullPointerException
- if eithera
orb
is null
-
from
Ifoptional
is present with valuev
, adapts it toBiOptional
containing values(v, v)
.- Throws:
NullPointerException
- ifoptional
is null.
-
map
If a pair of values is present, applymapper
to them, and if the result is non-null, return anOptional
containing it. Otherwise return an emptyOptional
.- Throws:
NullPointerException
- ifmapper
is null
-
map
public abstract <A2,B2> BiOptional<A2,B2> map(BiFunction<? super A, ? super B, ? extends A2> aMapper, BiFunction<? super A, ? super B, ? extends B2> bMapper) If a pair of values is present, applyaMapper
andbMapper
to them, and if both results are non-null, return a newBiOptional
instance containing the results. Otherwise return an emptyBiOptional
.- Throws:
NullPointerException
- ifaMapper
orbMapper
is null
-
map
public abstract <A2,B2> BiOptional<A2,B2> map(Function<? super A, ? extends A2> aMapper, Function<? super B, ? extends B2> bMapper) If a pair of values is present, applyaMapper
andbMapper
to each respectively, and if both results are non-null, return a newBiOptional
instance containing the results. Otherwise return an emptyBiOptional
.The following example parses and evaluates a numeric comparison:
if (Substring.first('=') .splitThenTrim("10 = 20") .map(Ints::tryParse, Ints::tryParse) .filter((a, b) -> a.equals(b)) .isPresent()) { // The two numbers are equal. }
- Throws:
NullPointerException
- ifaMapper
orbMapper
is null
-
flatMap
public abstract <T> Optional<T> flatMap(BiFunction<? super A, ? super B, ? extends Optional<? extends T>> mapper) If a pair of values is present, apply theBiOptional
-bearingmapper
to them, and return that result. Otherwise, return an emptyBiOptional
.- Throws:
NullPointerException
- ifmapper
is null or returns a null result
-
flatMap
public static <T,A, BiOptional<A,B> B> flatMap(Optional<T> optional, Function<? super T, ? extends BiOptional<? extends A, ? extends B>> mapper) Maps the value contained inoptional
to aBiOptional
usingmapper
, or else returns empty. For example, the following code usesSubstring.Pattern.split(java.lang.CharSequence)
to split an optional string:Optional<KeyValue> keyValue = BiOptional.flatMap(getOptionalInput(), Substring.first(':')::split) .map(KeyValue::new);
Use this method to bridge from an
Optional
toBiOptional
chain.- Throws:
NullPointerException
- ifoptional
ormapper
is null, or ifmapper
returns null.
-
filter
If a pair of values is present and matchespredicate
, return thisBiOptional instance
. Otherwise, return an emptyBiOptional
.- Throws:
NullPointerException
- ifpredicate
is null
-
skipIf
If a pair of values is present and matchespredicate
, the pair is skipped (returns empty).- Throws:
NullPointerException
- ifpredicate
is null- Since:
- 6.6
-
peek
Invokesconsumer
with the pair if present and returns this object as is.- Throws:
NullPointerException
- if consumer is null- Since:
- 5.1
-
matches
Returns true if the pair exists and satisfies thecondition
predicate.- Since:
- 5.7
-
ifPresent
If a pair of values is present, invokeconsumer
with them. Otherwise, do nothing.- Throws:
NullPointerException
- if consumer is null
-
isPresent
public abstract boolean isPresent()Returns true if the pair of values is present. -
or
public abstract BiOptional<A,B> or(Supplier<? extends BiOptional<? extends A, ? extends B>> alternative) If a pair of values are present, return thisBiOptional
instance. Otherwise, returns aBiOptional
produced byalternative
. -
orElse
Returns the pair if present, or else returns(a, b)
.a
andb
are allowed to be null.- Since:
- 5.1
-
orElseThrow
Ensures that the pair must be present or else throwsNoSuchElementException
.- Since:
- 5.1
-
orElseThrow
Ensures that the pair must be present or else throws the exception returned byexceptionSupplier
.- Throws:
NullPointerException
- ifexceptionSupplier
is null, or returns null.E
- if the pair is absent.- Since:
- 5.1
-
orElseThrow
Ensures that the pair must be present or else throwsNoSuchElementException
withmessage
formatted withargs
.- Throws:
NullPointerException
- ifmessage
is null, or ifexceptionFactory
returns null.NoSuchElementException
- if the pair is absent.E
- Since:
- 7.2
-
orElseThrow
public final <E extends Throwable> Both<A,B> orElseThrow(Function<String, E> exceptionFactory, String message, Object... args) throws EEnsures that the pair must be present or else throws the exception returned byexceptionFactory
withmessage
formatted withargs
.- Throws:
NullPointerException
- ifexceptionFactory
ormessage
is null, or ifexceptionFactory
returns null.E
- if the pair is absent.- Since:
- 6.6
-
stream
Returns aBiStream
view of this BiOptional.
-