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 emptyBiOptionalinstance.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-bearingmapperto 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 inoptionalto aBiOptionalusingmapper, or else returns empty.static <T> BiOptional<T, T> Ifoptionalis present with valuev, adapts it toBiOptionalcontaining values(v, v).abstract voidifPresent(BiConsumer<? super A, ? super B> consumer) If a pair of values is present, invokeconsumerwith them.abstract booleanReturns 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, applyaMapperandbMapperto them, and if both results are non-null, return a newBiOptionalinstance containing the results.abstract <T> Optional<T> map(BiFunction<? super A, ? super B, ? extends T> mapper) If a pair of values is present, applymapperto them, and if the result is non-null, return anOptionalcontaining it.abstract <A2,B2> BiOptional <A2, B2> If a pair of values is present, applyaMapperandbMapperto each respectively, and if both results are non-null, return a newBiOptionalinstance containing the results.abstract booleanmatches(BiPredicate<? super A, ? super B> condition) Returns true if the pair exists and satisfies theconditionpredicate.static <A,B> BiOptional <A, B> of(A a, B b) Returns aBiOptionalcontaining 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 thisBiOptionalinstance.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 throwsNoSuchElementExceptionwithmessageformatted withargs.orElseThrow(Function<String, E> exceptionFactory, String message, Object... args) Ensures that the pair must be present or else throws the exception returned byexceptionFactorywithmessageformatted 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) Invokesconsumerwith 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 aBiStreamview of this BiOptional.
-
Method Details
-
empty
Returns an emptyBiOptionalinstance. -
of
Returns aBiOptionalcontaining the pair(a, b).- Throws:
NullPointerException- if eitheraorbis null
-
from
Ifoptionalis present with valuev, adapts it toBiOptionalcontaining values(v, v).- Throws:
NullPointerException- ifoptionalis null.
-
map
If a pair of values is present, applymapperto them, and if the result is non-null, return anOptionalcontaining it. Otherwise return an emptyOptional.- Throws:
NullPointerException- ifmapperis 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, applyaMapperandbMapperto them, and if both results are non-null, return a newBiOptionalinstance containing the results. Otherwise return an emptyBiOptional.- Throws:
NullPointerException- ifaMapperorbMapperis 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, applyaMapperandbMapperto each respectively, and if both results are non-null, return a newBiOptionalinstance 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- ifaMapperorbMapperis 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-bearingmapperto them, and return that result. Otherwise, return an emptyBiOptional.- Throws:
NullPointerException- ifmapperis 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 inoptionalto aBiOptionalusingmapper, or else returns empty. For example, the following code usesSubstring.Pattern.split(java.lang.CharSequence, java.util.function.BiFunction<? super java.lang.String, ? super java.lang.String, ? extends T>)to split an optional string:Optional<KeyValue> keyValue = BiOptional.flatMap(getOptionalInput(), Substring.first(':')::split) .map(KeyValue::new);Use this method to bridge from an
OptionaltoBiOptionalchain.- Throws:
NullPointerException- ifoptionalormapperis null, or ifmapperreturns null.
-
filter
If a pair of values is present and matchespredicate, return thisBiOptional instance. Otherwise, return an emptyBiOptional.- Throws:
NullPointerException- ifpredicateis null
-
skipIf
If a pair of values is present and matchespredicate, the pair is skipped (returns empty).- Throws:
NullPointerException- ifpredicateis null- Since:
- 6.6
-
peek
Invokesconsumerwith 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 theconditionpredicate.- Since:
- 5.7
-
ifPresent
If a pair of values is present, invokeconsumerwith 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 thisBiOptionalinstance. Otherwise, returns aBiOptionalproduced byalternative. -
orElse
-
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- ifexceptionSupplieris null, or returns null.E- if the pair is absent.- Since:
- 5.1
-
orElseThrow
Ensures that the pair must be present or else throwsNoSuchElementExceptionwithmessageformatted withargs.- Throws:
NullPointerException- ifmessageis null, or ifexceptionFactoryreturns 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 byexceptionFactorywithmessageformatted withargs.- Throws:
NullPointerException- ifexceptionFactoryormessageis null, or ifexceptionFactoryreturns null.E- if the pair is absent.- Since:
- 6.6
-
stream
-