001    package edu.harvard.deas.hyperenc.util;
002    
003    import java.io.Serializable;
004    
005    /**
006     * Represents a pair of objects of different types. Pairs are immutable so long
007     * as the contained objects are immutable.
008     * 
009     * @author Jason Juang
010     */
011    public final class Pair<T1,T2> implements Serializable {
012      private static final long serialVersionUID = 1L;
013      
014            final private T1 t1;
015            final private T2 t2;
016            
017            public Pair(T1 t1, T2 t2) {
018                    this.t1 = t1;
019                    this.t2 = t2;
020            }
021            
022            public T1 first() {
023                    return t1;
024            }
025            
026            public T2 second() {
027                    return t2;
028            }
029    
030      /**
031       * Returns <code>true</code> if both components of this Pair are equal.
032       * 
033       * @return <code>true</code> if both the first and second components of each
034       *         Pair are equal according to their <code>equals</code> methods.
035       */
036            @Override
037      public boolean equals(Object o) {
038              if (o != null && o instanceof Pair) {
039                if (((Pair<?,?>)o).first().equals(this.first()) &&
040                    ((Pair<?,?>)o).second().equals(this.second()))
041                  return true;
042              }
043              return false;
044            }
045            
046            @Override
047      public int hashCode() {
048              return 2*t1.hashCode() + t2.hashCode();
049            }
050    }