Created
February 13, 2019 19:24
-
-
Save NetzwergX/ca5e6958aa5244ea0ff781cf3e506eee to your computer and use it in GitHub Desktop.
Extension methods for Optional<E> via Manifold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Optional; | |
import java.util.function.Function; | |
import java.util.function.Supplier; | |
import manifold.ext.api.Extension; | |
import manifold.ext.api.This; | |
/** | |
* Extends {@link Optional<T>} with two new methods for acquiring values. | |
*/ | |
@Extension | |
public class OptionalExt { | |
public <T, R> R applyOrGet(@This Optional<T> optional, Function<T, R> function, Supplier<R> supplier) { | |
if (optional.isPresent()) | |
return function.apply(optional.get()); | |
else return supplier.get(); | |
} | |
public <T, R> R applyOrReturn(@This Optional<T> optional, Function<T, R> function, R value) { | |
if (optional.isPresent()) | |
return function.apply(optional.get()); | |
else return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment