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