HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-common / src / main / java / org / apache / hadoop / hbase / net / Address.java
blob725a3764a36a7b50d1ea0eadf5d3c21fd81f416c
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.net;
20 import java.net.InetSocketAddress;
22 import org.apache.commons.lang3.StringUtils;
23 import org.apache.yetus.audience.InterfaceAudience;
25 import org.apache.hbase.thirdparty.com.google.common.net.HostAndPort;
27 /**
28 * An immutable type to hold a hostname and port combo, like an Endpoint
29 * or java.net.InetSocketAddress (but without danger of our calling
30 * resolve -- we do NOT want a resolve happening every time we want
31 * to hold a hostname and port combo). This class is also {@link Comparable}
32 * <p>In implementation this class is a facade over Guava's {@link HostAndPort}.
33 * We cannot have Guava classes in our API hence this Type.
35 @InterfaceAudience.Public
36 public class Address implements Comparable<Address> {
37 private final HostAndPort hostAndPort;
39 private Address(HostAndPort hostAndPort) {
40 this.hostAndPort = hostAndPort;
43 public static Address fromParts(String hostname, int port) {
44 return new Address(HostAndPort.fromParts(hostname, port));
47 public static Address fromString(String hostnameAndPort) {
48 return new Address(HostAndPort.fromString(hostnameAndPort));
51 public static Address fromSocketAddress(InetSocketAddress addr) {
52 return Address.fromParts(addr.getHostString(), addr.getPort());
55 public static InetSocketAddress toSocketAddress(Address addr) {
56 return new InetSocketAddress(addr.getHostName(), addr.getPort());
59 public static InetSocketAddress[] toSocketAddress(Address[] addrs) {
60 if (addrs == null) {
61 return null;
63 InetSocketAddress[] result = new InetSocketAddress[addrs.length];
64 for (int i = 0; i < addrs.length; i++) {
65 result[i] = toSocketAddress(addrs[i]);
67 return result;
70 public String getHostName() {
71 return this.hostAndPort.getHost();
74 /**
75 * @deprecated Use {@link #getHostName()} instead
77 @Deprecated
78 public String getHostname() {
79 return this.hostAndPort.getHost();
82 public int getPort() {
83 return this.hostAndPort.getPort();
86 @Override
87 public String toString() {
88 return this.hostAndPort.toString();
91 /**
92 * If hostname is a.b.c and the port is 123, return a:123 instead of a.b.c:123.
93 * @return if host looks like it is resolved -- not an IP -- then strip the domain portion
94 * otherwise returns same as {@link #toString()}}
96 public String toStringWithoutDomain() {
97 String hostname = getHostName();
98 String [] parts = hostname.split("\\.");
99 if (parts.length > 1) {
100 for (String part: parts) {
101 if (!StringUtils.isNumeric(part)) {
102 return Address.fromParts(parts[0], getPort()).toString();
106 return toString();
109 @Override
110 // Don't use HostAndPort equals... It is wonky including
111 // ipv6 brackets
112 public boolean equals(Object other) {
113 if (this == other) {
114 return true;
116 if (other instanceof Address) {
117 Address that = (Address)other;
118 return this.getHostName().equals(that.getHostName()) &&
119 this.getPort() == that.getPort();
121 return false;
124 @Override
125 public int hashCode() {
126 return this.getHostName().hashCode() ^ getPort();
129 @Override
130 public int compareTo(Address that) {
131 int compare = this.getHostName().compareTo(that.getHostName());
132 if (compare != 0) {
133 return compare;
136 return this.getPort() - that.getPort();