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: UnorderedPair.java 
019    
020    */
021    
022    
023    
024    package util;
025    
026    /**
027     * Unordered pair type. (a, b) == (b, a)
028     * 
029     * @param <A> Type of both components 
030     */
031    public class UnorderedPair<A> extends Pair<A, A> {
032      /**
033       * Creates an unordered pair with the specified components.
034       * 
035       * @param first  the first component
036       * @param second the second component
037       */
038      public UnorderedPair(A first, A second) {
039        super(first, second);
040      }
041    
042      @Override
043      public String toString() {
044        return "{" + first + ", " + second + "}";
045      }
046    
047      @Override
048      public boolean equals(Object other) {
049        if (!(other instanceof UnorderedPair<?>)) {
050          return false;
051        }
052        UnorderedPair<?> otherPair = (UnorderedPair<?>) other;
053        boolean ret = equals(first, otherPair.getFirst()) && equals(second, otherPair.getSecond());
054        return ret || equals(first, otherPair.getSecond()) && equals(second, otherPair.getFirst());
055      }
056    
057      @Override
058      public int hashCode() {
059        // if it is unordered, hashcode {a,b} = hashcode {b,a}
060        if (first == null) {
061          return (second == null) ? 0 : second.hashCode() + 1;
062        } else if (second == null) {
063          return first.hashCode() + 1;
064        } else {
065          return first.hashCode() ^ second.hashCode();
066        }
067      }
068    }