HBASE-24163 MOB compactor implementations should use format specifiers when calling...
[hbase.git] / hbase-common / src / main / java / org / apache / hadoop / hbase / util / Pair.java
blob43af0f644587be812f7c63e8703f0300368b92aa
1 /**
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;
26 /**
27 * A generic class for pairs.
28 * @param <T1>
29 * @param <T2>
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;
38 /**
39 * Default constructor.
41 public Pair()
45 /**
46 * Constructor
47 * @param a operand
48 * @param b operand
50 public Pair(T1 a, T2 b)
52 this.first = a;
53 this.second = b;
56 /**
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);
68 /**
69 * Replace the first element of the pair.
70 * @param a operand
72 public void setFirst(T1 a)
74 this.first = a;
77 /**
78 * Replace the second element of the pair.
79 * @param b operand
81 public void setSecond(T2 b)
83 this.second = b;
86 /**
87 * Return the first element stored in the pair.
88 * @return T1
90 public T1 getFirst()
92 return first;
95 /**
96 * Return the second element stored in the pair.
97 * @return T2
99 public T2 getSecond()
101 return second;
104 private static boolean equals(Object x, Object y)
106 return (x == null && y == null) || (x != null && x.equals(y));
109 @Override
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);
117 @Override
118 public int hashCode()
120 if (first == null)
121 return (second == null) ? 0 : second.hashCode() + 1;
122 else if (second == null)
123 return first.hashCode() + 2;
124 else
125 return first.hashCode() * 17 + second.hashCode();
128 @Override
129 public String toString()
131 return "{" + getFirst() + "," + getSecond() + "}";