package pt.utl.ist.fenix.tools.util;
import java.io.Serializable;
import com.linkare.commons.metainfo.Linkare;
/**
* A pair is simple aggregation of two values. This class can be used to keep two values together, like a key and value, without depending in any particular
* data structure.
*
* @author Goncalo Luiz
*
*
* Created on 13:27:42,31/Mar/2006
* @version $Id: Pair.java 35158 2008-04-07 15:23:35Z nmgo $
*/
@Linkare(author = "Paulo Zenida", comments = "Changed the class to include the implementations of methods equals() and hashCode()")
public class Pair implements Serializable {
private static final long serialVersionUID = 1L;
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
@Override
public String toString() {
return "Pair(" + getKey() + ", " + getValue() + ")";
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Pair)) {
return false;
}
final Pair other = (Pair) obj;
return equals(other.getKey(), this.getKey()) && equals(other.getValue(), this.getValue());
}
@Override
public int hashCode() {
int result = 31;
result += getKey() == null ? 0 : getKey().hashCode();
result += getValue() == null ? 0 : getValue().hashCode();
return result;
}
private boolean equals(final Object obj1, final Object obj2) {
return obj1 == null ? obj2 == null : obj1.equals(obj2);
}
}