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 com.android.utils; 18 19 /** 20 * A Pair class is simply a 2-tuple for use in this package. We might want to 21 * think about adding something like this to a more central utility place, or 22 * replace it by a common tuple class if one exists, or even rewrite the layout 23 * classes using this Pair by a more dedicated data structure (so we don't have 24 * to pass around generic signatures as is currently done, though at least the 25 * construction is helped a bit by the {@link #of} factory method. 26 * 27 * @param <S> The type of the first value 28 * @param <T> The type of the second value 29 */ 30 public class Pair<S,T> { 31 private final S mFirst; 32 private final T mSecond; 33 34 // Use {@link Pair#of} factory instead since it infers generic types Pair(S first, T second)35 private Pair(S first, T second) { 36 this.mFirst = first; 37 this.mSecond = second; 38 } 39 40 /** 41 * Return the first item in the pair 42 * 43 * @return the first item in the pair 44 */ getFirst()45 public S getFirst() { 46 return mFirst; 47 } 48 49 /** 50 * Return the second item in the pair 51 * 52 * @return the second item in the pair 53 */ getSecond()54 public T getSecond() { 55 return mSecond; 56 } 57 58 /** 59 * Constructs a new pair of the given two objects, inferring generic types. 60 * 61 * @param first the first item to store in the pair 62 * @param second the second item to store in the pair 63 * @param <S> the type of the first item 64 * @param <T> the type of the second item 65 * @return a new pair wrapping the two items 66 */ of(S first, T second)67 public static <S,T> Pair<S,T> of(S first, T second) { 68 return new Pair<S,T>(first,second); 69 } 70 71 @Override toString()72 public String toString() { 73 return "Pair [first=" + mFirst + ", second=" + mSecond + "]"; 74 } 75 76 @Override hashCode()77 public int hashCode() { 78 final int prime = 31; 79 int result = 1; 80 result = prime * result + ((mFirst == null) ? 0 : mFirst.hashCode()); 81 result = prime * result + ((mSecond == null) ? 0 : mSecond.hashCode()); 82 return result; 83 } 84 85 @SuppressWarnings("unchecked") 86 @Override equals(Object obj)87 public boolean equals(Object obj) { 88 if (this == obj) 89 return true; 90 if (obj == null) 91 return false; 92 if (getClass() != obj.getClass()) 93 return false; 94 Pair other = (Pair) obj; 95 if (mFirst == null) { 96 if (other.mFirst != null) 97 return false; 98 } else if (!mFirst.equals(other.mFirst)) 99 return false; 100 if (mSecond == null) { 101 if (other.mSecond != null) 102 return false; 103 } else if (!mSecond.equals(other.mSecond)) 104 return false; 105 return true; 106 } 107 } 108