fr.vergne.pester.util.optional.BiOptional Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pester-core Show documentation
Show all versions of pester-core Show documentation
Implementation of the Pester library.
The newest version!
package fr.vergne.pester.util.optional;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
public class BiOptional {
private final Optional u;
private final Optional v;
BiOptional(Optional u, Optional v) {
this.u = u;
this.v = v;
}
public static BiOptional ofNullables(U u, V v) {
return new BiOptional<>(Optional.ofNullable(u), Optional.ofNullable(v));
}
public BiOptional mapEach(Function mapper) {
return new BiOptional<>(u.map(mapper), v.map(mapper));
}
public Optional mapBoth(BiFunction mapper, Function mapperFirst,
Function mapperSecond) {
if (u.isPresent() && v.isPresent()) {
return Optional.ofNullable(mapper.apply(u.get(), v.get()));
} else if (u.isPresent()) {
return u.map(mapperFirst);
} else if (v.isPresent()) {
return v.map(mapperSecond);
} else {
return Optional.empty();
}
}
}