Created
October 29, 2012 22:16
-
-
Save eribeiro/3976902 to your computer and use it in GitHub Desktop.
Java Pair
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
public class Pair<U,T> { | |
public final U left; | |
public final T right; | |
public static <U,T> Pair<U,T> of(U u, T t) { | |
return new Pair<U,T>(u, t); | |
} | |
public Pair(U left, T right) { | |
if (left == null) throw new IllegalArgumentException("left cannot be null"); | |
if (right == null) throw new IllegalArgumentException("right cannot be null"); | |
this.left = left; | |
this.right = right; | |
} | |
@Override public int hashCode() { | |
return 31 * left.hashCode() + right.hashCode(); | |
} | |
@Override public boolean equals(Object obj) { | |
if (this == obj) return true; | |
if (obj == null ) return false; | |
if (!(obj instanceof Pair)) return false; | |
Pair p = (Pair) obj; | |
return left.equals(p.left) && right.equals(p.right); | |
} | |
@Override public String toString() { | |
return "Pair(" + left + "," + right + ")"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment