3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 package org
.apache
.hadoop
.hbase
.util
;
22 import java
.io
.Serializable
;
24 import org
.apache
.yetus
.audience
.InterfaceAudience
;
27 * A generic class for pairs.
31 @InterfaceAudience.Public
32 public class Pair
<T1
, T2
> implements Serializable
34 private static final long serialVersionUID
= -3986244606585552569L;
35 protected T1 first
= null;
36 protected T2 second
= null;
39 * Default constructor.
50 public Pair(T1 a
, T2 b
)
57 * Constructs a new pair, inferring the type via the passed arguments
58 * @param <T1> type for first
59 * @param <T2> type for second
60 * @param a first element
61 * @param b second element
62 * @return a new pair containing the passed arguments
64 public static <T1
,T2
> Pair
<T1
,T2
> newPair(T1 a
, T2 b
) {
65 return new Pair
<>(a
, b
);
69 * Replace the first element of the pair.
72 public void setFirst(T1 a
)
78 * Replace the second element of the pair.
81 public void setSecond(T2 b
)
87 * Return the first element stored in the pair.
96 * Return the second element stored in the pair.
104 private static boolean equals(Object x
, Object y
)
106 return (x
== null && y
== null) || (x
!= null && x
.equals(y
));
110 @SuppressWarnings("unchecked")
111 public boolean equals(Object other
)
113 return other
instanceof Pair
&& equals(first
, ((Pair
)other
).first
) &&
114 equals(second
, ((Pair
)other
).second
);
118 public int hashCode()
121 return (second
== null) ?
0 : second
.hashCode() + 1;
122 else if (second
== null)
123 return first
.hashCode() + 2;
125 return first
.hashCode() * 17 + second
.hashCode();
129 public String
toString()
131 return "{" + getFirst() + "," + getSecond() + "}";