001 package edu.rice.cs.cunit.util; 002 003 /** 004 * Generic pair container. 005 * @author Mathias Ricken 006 */ 007 public class Pair<FIRST, SECOND> { 008 /** 009 * First value of the pair. 010 */ 011 FIRST _first; 012 013 /** 014 * Second value of the pair. 015 */ 016 SECOND _second; 017 018 /** 019 * Create a new pair of the specified values. 020 * @param first first value 021 * @param second second value 022 */ 023 public Pair(FIRST first, SECOND second) { 024 _first = first; 025 _second = second; 026 } 027 028 /** 029 * Accessor for the first value. 030 * @return first value 031 */ 032 public FIRST first() { 033 return _first; 034 } 035 036 /** 037 * Mutator for the first value. 038 * @param first new first value 039 */ 040 public void setFirst(FIRST first) { 041 _first = first; 042 } 043 044 /** 045 * Accessor for the second value. 046 * @return second value 047 */ 048 public SECOND second() { 049 return _second; 050 } 051 052 /** 053 * Mutator for the second value. 054 * @param second new second value 055 */ 056 public void setSecond(SECOND second) { 057 _second = second; 058 } 059 060 /** 061 * Return true if the two values of this value are equal to the two values of the other value. 062 * @param o other value 063 * @return true if equal 064 */ 065 public boolean equals(Object o) { 066 if (this == o) { 067 return true; 068 } 069 if (o == null || getClass() != o.getClass()) { 070 return false; 071 } 072 073 Pair pair = (Pair)o; 074 075 if (_first != null ? !_first.equals(pair._first) : pair._first != null) { 076 return false; 077 } 078 if (_second != null ? !_second.equals(pair._second) : pair._second != null) { 079 return false; 080 } 081 082 return true; 083 } 084 085 /** 086 * Return a hashcode for this pair. 087 * @return hashcode 088 */ 089 public int hashCode() { 090 int result; 091 result = (_first != null ? _first.hashCode() : 0); 092 result = 31 * result + (_second != null ? _second.hashCode() : 0); 093 return result; 094 } 095 }