Advertisement
mitrakov

Java Tuple (Pair<L, R>)

Oct 12th, 2019
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. /**
  2.  * Copy of com.google.gwt.dev.util.Pair
  3.  * @param <L>
  4.  * @param <R>
  5.  */
  6. public class Pair<L, R> {
  7.     public static <L, R> Pair<L, R> create(L l, R r) {
  8.         return new Pair<>(l, r);
  9.     }
  10.  
  11.     public final L left;
  12.     public final R right;
  13.  
  14.     private Pair(L left, R right) {
  15.         this.left = left;
  16.         this.right = right;
  17.     }
  18.  
  19.     @SuppressWarnings("rawtypes")
  20.     @Override
  21.     public boolean equals(Object obj) {
  22.         if (this == obj) {
  23.             return true;
  24.         }
  25.         if (obj == null) {
  26.             return false;
  27.         }
  28.         if (getClass() != obj.getClass()) {
  29.             return false;
  30.         }
  31.         Pair other = (Pair) obj;
  32.         if (left == null) {
  33.             if (other.left != null) {
  34.                 return false;
  35.             }
  36.         } else if (!left.equals(other.left)) {
  37.             return false;
  38.         }
  39.         if (right == null) {
  40.             if (other.right != null) {
  41.                 return false;
  42.             }
  43.         } else if (!right.equals(other.right)) {
  44.             return false;
  45.         }
  46.         return true;
  47.     }
  48.  
  49.     public L getLeft() {
  50.         return left;
  51.     }
  52.  
  53.     public R getRight() {
  54.         return right;
  55.     }
  56.  
  57.     @Override
  58.     public int hashCode() {
  59.         final int prime = 31;
  60.         int result = 1;
  61.         result = prime * result + ((left == null) ? 0 : left.hashCode());
  62.         result = prime * result + ((right == null) ? 0 : right.hashCode());
  63.         return result;
  64.     }
  65.  
  66.     @Override
  67.     public String toString() {
  68.         return "(" + left + "," + right + ")";
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement