Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. import java.util.function.Function;
  2.  
  3. /**
  4. * 関数インターフェースでペアを表現する。
  5. *
  6. */
  7. public class FunctionalPair {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. Pair<Integer, String> status = Pair.of(200, "OK");
  12.  
  13. System.out.println(status.first()); //200
  14. System.out.println(status.second()); //"OK"
  15. }
  16.  
  17. //ジェネリクスで A or B の表現ができないので Object をバインドしている
  18. @FunctionalInterface
  19. interface Pair<A, B> extends
  20. Function<Function<A, Function<B, Object>>, Object> {
  21.  
  22. static <A, B> Pair<A, B> of(A a, B b) {
  23. return f -> f.apply(a).apply(b);
  24. }
  25.  
  26. default A first() {
  27. return (A) apply(a -> b -> a);
  28. }
  29.  
  30. default B second() {
  31. return (B) apply(a -> b -> b);
  32. }
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement