1 /**
2  * The utillib library.
3  * More information is available at http://www.jinchess.com/.
4  * Copyright (C) 2002 Alexander Maryanovsky.
5  * All rights reserved.
6  *
7  * The utillib library is free software; you can redistribute
8  * it and/or modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * The utillib library is distributed in the hope that it will
13  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with utillib library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 package free.util;
23 
24 
25 /**
26  * A wrapper for any two other given objects.
27  */
28 
29 public final class Pair{
30 
31 
32   /**
33    * The first object.
34    */
35 
36   private final Object first;
37 
38 
39 
40 
41   /**
42    * The second object.
43    */
44 
45   private final Object second;
46 
47 
48 
49 
50   /**
51    * Creates a new <code>Pair</code> with the two given objects. Either of the
52    * objects may be <code>null</code>.
53    */
54 
Pair(Object first, Object second)55   public Pair(Object first, Object second){
56     this.first = first;
57     this.second = second;
58   }
59 
60 
61 
62 
63   /**
64    * Returns the first object.
65    */
66 
getFirst()67   public Object getFirst(){
68     return first;
69   }
70 
71 
72 
73 
74   /**
75    * Returns the second object.
76    */
77 
getSecond()78   public Object getSecond(){
79     return second;
80   }
81 
82 
83 
84 
85   /**
86    * Returns a hashcode combined from the hashcodes of the two target objects.
87    */
88 
hashCode()89   public int hashCode(){
90     int hash1 = (first == null ? 0 : first.hashCode());
91     int hash2 = (second == null ? 0 : second.hashCode());
92     return hash1^hash2;
93   }
94 
95 
96 
97 
98   /**
99    * Returns true iff the given Object is a Pair, and its two objects are the
100    * same as this one's (comparison done via <code>Utilities.areEqual</code>)
101    */
102 
equals(Object o)103   public boolean equals(Object o){
104     if (o == this)
105       return true;
106 
107     if (!(o instanceof Pair))
108       return false;
109 
110     Pair pair = (Pair)o;
111 
112     return Utilities.areEqual(pair.first, first) && Utilities.areEqual(pair.second, second);
113   }
114 
115 }
116