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 import java.util.Objects;
     20 
     21 /**
     22  * Container to ease passing around a tuple of two objects. This object provides a sensible
     23  * implementation of equals(), returning true if equals() is true on each of the contained
     24  * objects.
     25  */
     26 public class Pair<F, S> {
     27     public final F first;
     28     public final S second;
     29 
     30     /**
     31      * Constructor for a Pair.
     32      *
     33      * @param first the first object in the Pair
     34      * @param second the second object in the pair
     35      */
     36     public Pair(F first, S second) {
     37         this.first = first;
     38         this.second = second;
     39     }
     40 
     41     /**
     42      * Checks the two objects for equality by delegating to their respective
     43      * {@link Object#equals(Object)} methods.
     44      *
     45      * @param o the {@link Pair} to which this one is to be checked for equality
     46      * @return true if the underlying objects of the Pair are both considered
     47      *         equal
     48      */
     49     @Override
     50     public boolean equals(Object o) {
     51         if (!(o instanceof Pair)) {
     52             return false;
     53         }
     54         Pair<?, ?> p = (Pair<?, ?>) o;
     55         return Objects.equals(p.first, first) && Objects.equals(p.second, second);
     56     }
     57 
     58     /**
     59      * Compute a hash code using the hash codes of the underlying objects
     60      *
     61      * @return a hashcode of the Pair
     62      */
     63     @Override
     64     public int hashCode() {
     65         return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
     66     }
     67 
     68     @Override
     69     public String toString() {
     70         return "Pair{" + String.valueOf(first) + " " + String.valueOf(second) + "}";
     71     }
     72 
     73     /**
     74      * Convenience method for creating an appropriately typed pair.
     75      * @param a the first object in the Pair
     76      * @param b the second object in the pair
     77      * @return a Pair that is templatized with the types of a and b
     78      */
     79     public static <A, B> Pair <A, B> create(A a, B b) {
     80         return new Pair<A, B>(a, b);
     81     }
     82 }
     83