/*
 * Copyright 2013 University of Chicago and Argonne National Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */
package exm.stc.common.util;

import java.util.ArrayList;
import java.util.List;


public class Pair<T1, T2> {
  public final T1 val1;
  public final T2 val2;

  public Pair(T1 first, T2 second) {
    super();
    this.val1 = first;
    this.val2 = second;
  }
  
  public static <T1, T2> Pair<T1, T2> create(T1 f, T2 s) {
    return new Pair<T1, T2>(f, s);
  }

  @Override
  public boolean equals(Object obj) {
    if (!(obj instanceof Pair<?, ?>)) {
      return false;
    }
    Pair<?, ?> p2 = (Pair<?, ?>)obj;
    return val1.equals(p2.val1) && val2.equals(p2.val2);
  }

  @Override
  public int hashCode() {
    return (17 * val1.hashCode()) ^ val2.hashCode();
  }
  
  @Override
  public String toString() {
    return "(" + val1.toString() + ", " + val2.toString() + ")";
  }
  
  public static <T, S> List<T> extract1(List<Pair<T, S>> list) {
    ArrayList<T> res = new ArrayList<T>(list.size());
    for (Pair<T, S> p: list) {
      res.add(p.val1);
    }
    return res;
  }
  
  public static <T, S> List<S> extract2(List<Pair<T, S>> list) {
    ArrayList<S> res = new ArrayList<S>(list.size());
    for (Pair<T, S> p: list) {
      res.add(p.val2);
    }
    return res;
  }
}

@@ -712,16 +712,123 @@
 List;%0A%0A%0A
+/**%0A * Represent a pair of data.  Supports equality and hash comparison.%0A * @param %3CT1%3E%0A * @param %3CT2%3E%0A */%0A
 public c
@@ -1105,121 +1105,466 @@
 %0A%0A  
-@Override%0A  public boolean equals(Object obj) %7B%0A    if (!(obj instanceof Pair%3C?, ?%3E)) %7B%0A      return false;%0A    %7D
+%0A  @Override%0A  public int hashCode() %7B%0A    final int prime = 31;%0A    int result = 1;%0A    result = prime * result + ((val1 == null) ? 0 : val1.hashCode());%0A    result = prime * result + ((val2 == null) ? 0 : val2.hashCode());%0A    return result;%0A  %7D%0A%0A  @Override%0A  public boolean equals(Object obj) %7B%0A    if (this == obj)%0A      return true;%0A    if (obj == null) %7B%0A      return false;%0A    %7D%0A    assert(obj instanceof Pair) : %22Comparing pair with non-pair%22;%0A    
 %0A   
@@ -1575,18 +1575,21 @@
 r%3C?, ?%3E 
-p2
+other
  = (Pair
@@ -1599,13 +1599,72 @@
  ?%3E)
+ 
 obj;%0A
+    if (val1 == null) %7B%0A      if (other.val1 != null)%0A    
     
@@ -1674,44 +1674,100 @@
 urn 
-val1.equals(p2.val1) && val2
+false;%0A    %7D else if (other.val2 == null) %7B%0A      return false;%0A    %7D else if (!val1
 .equals(
 p2.v
@@ -1766,65 +1766,158 @@
 als(
-p2.val2);%0A  %7D%0A%0A  @Override%0A  public int hashCode(
+other.val1))%0A      return false;%0A    if (val2 == null) %7B%0A      if (other.val2 != null)%0A        return false;%0A    %7D else if (other.val2 == null
 ) %7B%0A    
 retu
@@ -1916,63 +1916,106 @@
     
+  
 return 
-(17 * val1.hashCode()) %5E val2.hashCode()
+false;%0A    %7D else if (!val2.equals(other.val2))%0A      return false;%0A    return true
 ;%0A  %7D%0A
-  
 %0A  @

Fix Pair equals and hashcode to support null fields