1 /**
2 * Copyright 2010 The Apache Software Foundation
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 package org.apache.hadoop.hbase.util;
22
23 import java.io.Serializable;
24
25 /**
26 * A generic class for pairs.
27 * @param <T1>
28 * @param <T2>
29 */
30 public class Pair<T1, T2> implements Serializable
31 {
32 private static final long serialVersionUID = -3986244606585552569L;
33 protected T1 first = null;
34 protected T2 second = null;
35
36 /**
37 * Default constructor.
38 */
39 public Pair()
40 {
41 }
42
43 /**
44 * Constructor
45 * @param a operand
46 * @param b operand
47 */
48 public Pair(T1 a, T2 b)
49 {
50 this.first = a;
51 this.second = b;
52 }
53
54 /**
55 * Replace the first element of the pair.
56 * @param a operand
57 */
58 public void setFirst(T1 a)
59 {
60 this.first = a;
61 }
62
63 /**
64 * Replace the second element of the pair.
65 * @param b operand
66 */
67 public void setSecond(T2 b)
68 {
69 this.second = b;
70 }
71
72 /**
73 * Return the first element stored in the pair.
74 * @return T1
75 */
76 public T1 getFirst()
77 {
78 return first;
79 }
80
81 /**
82 * Return the second element stored in the pair.
83 * @return T2
84 */
85 public T2 getSecond()
86 {
87 return second;
88 }
89
90 private static boolean equals(Object x, Object y)
91 {
92 return (x == null && y == null) || (x != null && x.equals(y));
93 }
94
95 @Override
96 @SuppressWarnings("unchecked")
97 public boolean equals(Object other)
98 {
99 return other instanceof Pair && equals(first, ((Pair)other).first) &&
100 equals(second, ((Pair)other).second);
101 }
102
103 @Override
104 public int hashCode()
105 {
106 if (first == null)
107 return (second == null) ? 0 : second.hashCode() + 1;
108 else if (second == null)
109 return first.hashCode() + 2;
110 else
111 return first.hashCode() * 17 + second.hashCode();
112 }
113
114 @Override
115 public String toString()
116 {
117 return "{" + getFirst() + "," + getSecond() + "}";
118 }
119 }