Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. import java.util.function.Function;
  2. import java.util.Optional;
  3.  
  4. interface ObjectPath<T, R> extends Function<T, R> {
  5.  
  6. default R get(T t) {
  7. R r = this.apply(t);
  8. if (r == null) {
  9. throw new NoSuchElementException();
  10. }
  11. return r;
  12. }
  13.  
  14. default Optional<R> optionally(T t) {
  15. return Optional.ofNullable(this.apply(t));
  16. }
  17.  
  18. @Override
  19. default <V> ObjectPath<T, V> andThen(Function<? super R, ? extends V> after) {
  20. return (s) -> {
  21. R r = this.apply(s);
  22. return r != null ? after.apply(r) : null;
  23. }
  24. }
  25.  
  26. default <V> ObjectPath<T, V> specialize(Class<V> specialization) {
  27. return (s) -> {
  28. R r = this.apply(s);
  29. return specialization.isInstance(r) ? (V) r : null;
  30. }
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement