Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.util;
     18 
     19 /**
     20  * Container to ease passing around a tuple of two objects. This object provides a sensible
     21  * implementation of equals(), returning true if equals() is true on each of the contained
     22  * objects.
     23  */
     24 public class Pair<F, S> {
     25     public final F first;
     26     public final S second;
     27 
     28     /**
     29      * Constructor for a Pair. If either are null then equals() and hashCode() will throw
     30      * a NullPointerException.
     31      * @param first the first object in the Pair
     32      * @param second the second object in the pair
     33      */
     34     public Pair(F first, S second) {
     35         this.first = first;
     36         this.second = second;
     37     }
     38 
     39     /**
     40      * Checks the two objects for equality by delegating to their respective equals() methods.
     41      * @param o the Pair to which this one is to be checked for equality
     42      * @return true if the underlying objects of the Pair are both considered equals()
     43      */
     44     public boolean equals(Object o) {
     45         if (o == this) return true;
     46         if (!(o instanceof Pair)) return false;
     47         final Pair<F, S> other;
     48         try {
     49             other = (Pair<F, S>) o;
     50         } catch (ClassCastException e) {
     51             return false;
     52         }
     53         return first.equals(other.first) && second.equals(other.second);
     54     }
     55 
     56     /**
     57      * Compute a hash code using the hash codes of the underlying objects
     58      * @return a hashcode of the Pair
     59      */
     60     public int hashCode() {
     61         int result = 17;
     62         result = 31 * result + first.hashCode();
     63         result = 31 * result + second.hashCode();
     64         return result;
     65     }
     66 
     67     /**
     68      * Convenience method for creating an appropriately typed pair.
     69      * @param a the first object in the Pair
     70      * @param b the second object in the pair
     71      * @return a Pair that is templatized with the types of a and b
     72      */
     73     public static <A, B> Pair <A, B> create(A a, B b) {
     74         return new Pair<A, B>(a, b);
     75     }
     76 }
     77