001/* 002 * BioJava development code 003 * 004 * This code may be freely distributed and modified under the 005 * terms of the GNU Lesser General Public Licence. This should 006 * be distributed with the code. If you do not have a copy, 007 * see: 008 * 009 * http://www.gnu.org/copyleft/lesser.html 010 * 011 * Copyright for this code is held jointly by the individual 012 * authors. These should be listed in @author doc comments. 013 * 014 * For more information on the BioJava project and its aims, 015 * or to join the biojava-l mailing list, visit the home page 016 * at: 017 * 018 * http://www.biojava.org/ 019 * 020 */ 021package org.biojava.nbio.structure.contact; 022 023import java.io.Serializable; 024 025/** 026 * A Pair of objects. Implements an equals and hashCode so 027 * that it can be used as key in hashes. 028 * Based on the JUNG graph library implementation. 029 * 030 * @author duarte_j 031 * 032 * @param <T> 033 */ 034public final class Pair<T> implements Serializable { 035 036 private static final long serialVersionUID = 1L; 037 038 private T first; 039 private T second; 040 041 /** 042 * Creates a <code>Pair</code> from the specified elements. 043 * @param value1 the first value in the new <code>Pair</code> 044 * @param value2 the second value in the new <code>Pair</code> 045 * @throws IllegalArgumentException if either argument is null 046 */ 047 public Pair(T value1, T value2) { 048 if(value1 == null || value2 == null) 049 throw new IllegalArgumentException("Pair cannot contain null values"); 050 first = value1; 051 second = value2; 052 } 053 054 public T getFirst() { 055 return first; 056 } 057 058 public T getSecond() { 059 return second; 060 } 061 062 063 064 @Override 065 public int hashCode() { 066 final int prime = 31; 067 int result = 1; 068 result = prime * result + ((first == null) ? 0 : first.hashCode()); 069 result = prime * result + ((second == null) ? 0 : second.hashCode()); 070 return result; 071 } 072 073 @Override 074 public boolean equals(Object obj) { 075 if (this == obj) 076 return true; 077 if (obj == null) 078 return false; 079 if (getClass() != obj.getClass()) 080 return false; 081 @SuppressWarnings("unchecked") 082 Pair<T> other = (Pair<T>) obj; 083 if (first == null) { 084 if (other.first != null) 085 return false; 086 } else if (!first.equals(other.first)) 087 return false; 088 if (second == null) { 089 if (other.second != null) 090 return false; 091 } else if (!second.equals(other.second)) 092 return false; 093 return true; 094 } 095 096 @Override 097 public String toString() { 098 return "<" + first.toString() + ", " + second.toString() + ">"; 099 } 100}