Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.function.Function;
- import java.util.Optional;
- interface ObjectPath<T, R> extends Function<T, R> {
- default R get(T t) {
- R r = this.apply(t);
- if (r == null) {
- throw new NoSuchElementException();
- }
- return r;
- }
- default Optional<R> optionally(T t) {
- return Optional.ofNullable(this.apply(t));
- }
- @Override
- default <V> ObjectPath<T, V> andThen(Function<? super R, ? extends V> after) {
- return (s) -> {
- R r = this.apply(s);
- return r != null ? after.apply(r) : null;
- }
- }
- default <V> ObjectPath<T, V> specialize(Class<V> specialization) {
- return (s) -> {
- R r = this.apply(s);
- return specialization.isInstance(r) ? (V) r : null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement