add an optionals utility and fix push challenge metric

This commit is contained in:
Ravi Khadiwala
2022-09-26 10:26:13 -05:00
committed by ravi-signal
parent a79d709039
commit 6e595a0959
2 changed files with 29 additions and 7 deletions

View File

@@ -0,0 +1,21 @@
package org.whispersystems.textsecuregcm.util;
import java.util.Optional;
import java.util.function.BiFunction;
public class Optionals {
private Optionals() {}
/**
* Apply a function to two optional arguments, returning empty if either argument is empty
*
* @param optionalT Optional of type T
* @param optionalU Optional of type U
* @param fun Function of T and U that returns R
* @return The function applied to the values of optionalT and optionalU, or empty
*/
public static <T, U, R> Optional<R> zipWith(Optional<T> optionalT, Optional<U> optionalU, BiFunction<T, U, R> fun) {
return optionalT.flatMap(t -> optionalU.map(u -> fun.apply(t, u)));
}
}