This commit is contained in:
Sebastian Stenzel
2017-05-11 17:14:23 +02:00
parent da94fe4f6f
commit 0906abdea9
4 changed files with 53 additions and 17 deletions
@@ -1,6 +1,7 @@
package org.cryptomator.common;
import java.util.Optional;
import java.util.function.Function;
public final class Optionals {
@@ -14,4 +15,16 @@ public final class Optionals {
}
}
/**
* Returns a function that is equivalent to the input function but immediately gets the value of the returned optional when invoked.
*
* @param <T> the type of the input to the function
* @param <R> the type of the result of the function
* @param optionalFunction An input function {@code Function<Foo, Optional<Bar>>}
* @return A {@code Function<Foo, Bar>}, that may throw an NoSuchElementException, if the original function returns an empty optional.
*/
public static <T, R> Function<T, R> unwrap(Function<T, Optional<R>> optionalFunction) {
return t -> optionalFunction.apply(t).get();
}
}