001    /*
002    Galois, a framework to exploit amorphous data-parallelism in irregular
003    programs.
004    
005    Copyright (C) 2010, The University of Texas at Austin. All rights reserved.
006    UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS SOFTWARE
007    AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR ANY
008    PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF PERFORMANCE, AND ANY
009    WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF DEALING OR USAGE OF TRADE.
010    NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH RESPECT TO THE USE OF THE
011    SOFTWARE OR DOCUMENTATION. Under no circumstances shall University be liable
012    for incidental, special, indirect, direct or consequential damages or loss of
013    profits, interruption of business, or related expenses which may arise from use
014    of Software or Documentation, including but not limited to those resulting from
015    defects in Software and/or Documentation, or loss or inaccuracy of data of any
016    kind.
017    
018    File: Pair.java 
019    
020    */
021    
022    
023    
024    package util;
025    
026    /**
027     * An ordered pair.
028     * 
029     * @param <A> the type of the first component
030     * @param <B> the type of the second component
031     */
032    public class Pair<A, B> {
033      protected final A first;
034      protected final B second;
035    
036      /**
037       * Creates an ordered pair with the specified components
038       * 
039       * @param first  the first component
040       * @param second the second component
041       */
042      public Pair(A first, B second) {
043        this.first = first;
044        this.second = second;
045      }
046    
047      /**
048       * @return  the first component of the pair
049       */
050      public A getFirst() {
051        return first;
052      }
053    
054      /**
055       * @return  the second component of the pair
056       */
057      public B getSecond() {
058        return second;
059      }
060    
061      @Override
062      public String toString() {
063        return "(" + first + ", " + second + ")";
064      }
065    
066      @Override
067      public boolean equals(Object other) {
068        if (!(other instanceof Pair<?, ?>)) {
069          return false;
070        }
071        Pair<?, ?> otherPair = (Pair<?, ?>) other;
072        boolean ret = equals(first, otherPair.getFirst()) && equals(second, otherPair.getSecond());
073        return ret;
074      }
075    
076      /**
077       * Compares two objects for equality taking care of null references
078       * 
079       * @param x  an object
080       * @param y  an object
081       * @return   returns true when <code>x</code> equals <code>y</code> or if
082       *           both are null
083       */
084      protected final boolean equals(Object x, Object y) {
085        return (x == null && y == null) || (x != null && x.equals(y));
086      }
087    
088      @Override
089      public int hashCode() {
090        // hashcode (a,b) <> hashcode (b,a)
091        if (first == null) {
092          return (second == null) ? 0 : second.hashCode() + 1;
093        } else if (second == null) {
094          return first.hashCode() + 2;
095        } else {
096          return first.hashCode() * 17 + second.hashCode();
097        }
098      }
099    }