KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > util > Pair


1 /* ====================================================================
2  * Trove - Copyright (c) 1997-2001 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.trove.util;
54
55 /******************************************************************************
56  * Simple object for pairing two objects together for use as a hash or tree
57  * key.
58  *
59  * @author Brian S O'Neill
60  * @version
61  * <!--$$Revision:--> 5 <!-- $-->, <!--$$JustDate:--> 01/07/09 <!-- $-->
62  * @see MultiKey
63  */

64 public class Pair implements Comparable JavaDoc, java.io.Serializable JavaDoc {
65     private final Object JavaDoc mObj1;
66     private final Object JavaDoc mObj2;
67
68     public Pair(Object JavaDoc obj1, Object JavaDoc obj2) {
69         mObj1 = obj1;
70         mObj2 = obj2;
71     }
72     
73     public int compareTo(Object JavaDoc obj) {
74         if (this == obj) {
75             return 0;
76         }
77
78         Pair other = (Pair)obj;
79
80         Object JavaDoc a = mObj1;
81         Object JavaDoc b = other.mObj1;
82
83         firstTest: {
84             if (a == null) {
85                 if (b != null) {
86                     return 1;
87                 }
88                 // Both a and b are null.
89
break firstTest;
90             }
91             else {
92                 if (b == null) {
93                     return -1;
94                 }
95             }
96
97             int result = ((Comparable JavaDoc)a).compareTo(b);
98             
99             if (result != 0) {
100                 return result;
101             }
102         }
103
104         a = mObj2;
105         b = other.mObj2;
106         
107         if (a == null) {
108             if (b != null) {
109                 return 1;
110             }
111             // Both a and b are null.
112
return 0;
113         }
114         else {
115             if (b == null) {
116                 return -1;
117             }
118         }
119         
120         return ((Comparable JavaDoc)a).compareTo(b);
121     }
122
123     public boolean equals(Object JavaDoc obj) {
124         if (this == obj) {
125             return true;
126         }
127
128         if (!(obj instanceof Pair)) {
129             return false;
130         }
131         
132         Pair key = (Pair)obj;
133         
134         return
135             (mObj1 == null ?
136              key.mObj1 == null : mObj1.equals(key.mObj1)) &&
137             (mObj2 == null ?
138              key.mObj2 == null : mObj2.equals(key.mObj2));
139     }
140     
141     public int hashCode() {
142         return
143             (mObj1 == null ? 0 : mObj1.hashCode()) +
144             (mObj2 == null ? 0 : mObj2.hashCode());
145     }
146     
147     public String JavaDoc toString() {
148         return "[" + mObj1 + ':' + mObj2 + ']';
149     }
150 }
151
Popular Tags