Skip to content

Instantly share code, notes, and snippets.

@jaycobbcruz
Created April 30, 2017 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaycobbcruz/fbdbbd3fb40e15996777c95b68c915f9 to your computer and use it in GitHub Desktop.
Save jaycobbcruz/fbdbbd3fb40e15996777c95b68c915f9 to your computer and use it in GitHub Desktop.
A simple pair
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