Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2010 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 package tests.util;
     17 /**
     18  * Pair of typed values.
     19  *
     20  * <p>Pairs are obtained using {@link #of(Object, Object) of}.
     21  *
     22  * @param <F> type of the first value.
     23  * @param <S> type of the second value.
     24  */
     25 public class Pair<F, S> {
     26     private final F mFirst;
     27     private final S mSecond;
     28     private Pair(F first, S second) {
     29         mFirst = first;
     30         mSecond = second;
     31     }
     32     /**
     33      * Gets the pair consisting of the two provided values.
     34      *
     35      * @param first first value or {@code null}.
     36      * @param second second value or {@code null}.
     37      */
     38     public static <F, S> Pair<F, S> of(F first, S second) {
     39         return new Pair<F, S>(first, second);
     40     }
     41     /**
     42      * Gets the first value from this pair.
     43      *
     44      * @return value or {@code null}.
     45      */
     46     public F getFirst() {
     47         return mFirst;
     48     }
     49     /**
     50      * Gets the second value from this pair.
     51      *
     52      * @return value or {@code null}.
     53      */
     54     public S getSecond() {
     55         return mSecond;
     56     }
     57     @Override
     58     public String toString() {
     59         return "Pair[" + mFirst + ", " + mSecond + "]";
     60     }
     61     @Override
     62     public int hashCode() {
     63         final int prime = 31;
     64         int result = 1;
     65         result = prime * result + ((mFirst == null) ? 0 : mFirst.hashCode());
     66         result = prime * result + ((mSecond == null) ? 0 : mSecond.hashCode());
     67         return result;
     68     }
     69     @Override
     70     public boolean equals(Object obj) {
     71         if (this == obj) {
     72             return true;
     73         }
     74         if (obj == null) {
     75             return false;
     76         }
     77         if (getClass() != obj.getClass()) {
     78             return false;
     79         }
     80         @SuppressWarnings("rawtypes")
     81         Pair other = (Pair) obj;
     82         if (mFirst == null) {
     83             if (other.mFirst != null) {
     84                 return false;
     85             }
     86         } else if (!mFirst.equals(other.mFirst)) {
     87             return false;
     88         }
     89         if (mSecond == null) {
     90             if (other.mSecond != null) {
     91                 return false;
     92             }
     93         } else if (!mSecond.equals(other.mSecond)) {
     94             return false;
     95         }
     96         return true;
     97     }
     98 }
     99