Created
April 30, 2017 10:20
-
-
Save jaycobbcruz/fbdbbd3fb40e15996777c95b68c915f9 to your computer and use it in GitHub Desktop.
A simple 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<L, R> { | |
private L left; | |
private R right; | |
public Pair(L left, R right) { | |
if (left == null) { throw new RuntimeException("Left must not be null"); } | |
if (right == null) { throw new RuntimeException("Right must not be null"); } | |
this.left = left; | |
this.right = right; | |
} | |
public static <L, R> Pair<L, R> valueOf(L l, R r) { | |
return new Pair<>(l, r); | |
} | |
public String leftString() { | |
return left.toString(); | |
} | |
public String rightString() { | |
return right.toString(); | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
Pair<?, ?> pair = (Pair<?, ?>) o; | |
if (left != null ? !left.equals(pair.left) : pair.left != null) return false; | |
return !(right != null ? !right.equals(pair.right) : pair.right != null); | |
} | |
@Override | |
public int hashCode() { | |
int result = left != null ? left.hashCode() : 0; | |
result = 31 * result + (right != null ? right.hashCode() : 0); | |
return result; | |
} | |
@Override | |
public String toString() { | |
return left + ":" + right; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment