Skip to content

Instantly share code, notes, and snippets.

@anuvrat
Created April 19, 2010 10:37
Show Gist options
  • Select an option

  • Save anuvrat/370901 to your computer and use it in GitHub Desktop.

Select an option

Save anuvrat/370901 to your computer and use it in GitHub Desktop.
class Pair<L, R>
import java.io.Serializable;
/**
* A pair utility class
*
* @author AnuvratSingh
* @param <L> The left component type
* @param <R> The right component type
*/
public class Pair<L, R> implements Serializable, Comparable<Pair<L, R>>, Cloneable {
private static final long serialVersionUID = 2256100280569043695L;
/**
* A factory to create a new pair
*
* @param <L> The left component type
* @param <R> The right component type
* @param left The left component value
* @param right The right component value
* @return A new Pair object
*/
public static <L, R> Pair<L, R> of(L left, R right) {
return new Pair<L, R>(left, right);
}
/**
* A factory to create a new pair
*
* @param <L> The left component type
* @param <R> The right component type
* @param p A pair of objects
* @return A new Pair object
*/
public static <L, R> Pair<L, R> of(Pair<? extends L, ? extends R> p) {
return new Pair<L, R>(p);
}
private L m_left;
private R m_right;
/**
* Constructor to create the Pair object
*
* @param left The left component value
* @param right The right component value
*/
private Pair(L left, R right) {
m_left = left;
m_right = right;
}
/**
* Constructor to create the Pair object
*
* @param p A pair of objects
*/
private Pair(Pair<? extends L, ? extends R> p) {
m_left = p.getLeft();
m_right = p.getRight();
}
/**
* @return The left component value
*/
public L getLeft() {
return m_left;
}
/**
* @return The right component value
*/
public R getRight() {
return m_right;
}
public int compareTo(Pair<L, R> pair) {
if (null != pair) {
if (pair.equals(this))
return 0;
else if (pair.hashCode() > this.hashCode())
return 1;
return -1;
}
return -1;
}
@SuppressWarnings("unchecked")
public Pair<L, R> clone() {
try {
return (Pair<L, R>) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError(e);
}
}
public boolean equals(Object object) {
if (object instanceof Pair<?, ?>) {
Pair<?, ?> other = (Pair<?, ?>) object;
return Pair.eq(m_left, other.getLeft()) && Pair.eq(m_right, other.getRight());
}
return false;
}
/**
* Check if two objects are equal or not
*
* @param a Object to be compared
* @param b Object to be compared
* @return true if the two objects are equal or the same
*/
private static boolean eq(Object a, Object b) {
return a == b || a != null && a.equals(b);
}
public String toString() {
return String.format("(%s, %s)", m_left, m_right);
}
public int hashCode() {
return Pair.hash(m_left, 0) + 0xDeadBeef ^ Pair.hash(m_right, 0);
}
/**
* @param object Object whose hash is to be calculated
* @param nullHash The value to be returned if the object is null
* @return nullHash if the object is null, hashCode otherwise
*/
private static int hash(Object object, int nullHash) {
return object == null ? nullHash : object.hashCode();
}
/**
* Swap the left and right components of the Pair
*
* @return A new Pair object whose left and right have been swapped
*/
public Pair<R, L> swap() {
return Pair.of(getRight(), getLeft());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment