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 org
.apache
.commons
.lang3
.StringUtils
;
21 import org
.apache
.yetus
.audience
.InterfaceAudience
;
23 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.net
.HostAndPort
;
26 * An immutable type to hold a hostname and port combo, like an Endpoint
27 * or java.net.InetSocketAddress (but without danger of our calling
28 * resolve -- we do NOT want a resolve happening every time we want
29 * to hold a hostname and port combo). This class is also {@link Comparable}
30 * <p>In implementation this class is a facade over Guava's {@link HostAndPort}.
31 * We cannot have Guava classes in our API hence this Type.
33 @InterfaceAudience.Public
34 public class Address
implements Comparable
<Address
> {
35 private HostAndPort hostAndPort
;
37 private Address(HostAndPort hostAndPort
) {
38 this.hostAndPort
= hostAndPort
;
41 public static Address
fromParts(String hostname
, int port
) {
42 return new Address(HostAndPort
.fromParts(hostname
, port
));
45 public static Address
fromString(String hostnameAndPort
) {
46 return new Address(HostAndPort
.fromString(hostnameAndPort
));
49 public String
getHostname() {
50 return this.hostAndPort
.getHost();
53 public int getPort() {
54 return this.hostAndPort
.getPort();
58 public String
toString() {
59 return this.hostAndPort
.toString();
63 * If hostname is a.b.c and the port is 123, return a:123 instead of a.b.c:123.
64 * @return if host looks like it is resolved -- not an IP -- then strip the domain portion
65 * otherwise returns same as {@link #toString()}}
67 public String
toStringWithoutDomain() {
68 String hostname
= getHostname();
69 String
[] parts
= hostname
.split("\\.");
70 if (parts
.length
> 1) {
71 for (String part
: parts
) {
72 if (!StringUtils
.isNumeric(part
)) {
73 return Address
.fromParts(parts
[0], getPort()).toString();
81 // Don't use HostAndPort equals... It is wonky including
83 public boolean equals(Object other
) {
87 if (other
instanceof Address
) {
88 Address that
= (Address
)other
;
89 return this.getHostname().equals(that
.getHostname()) &&
90 this.getPort() == that
.getPort();
96 public int hashCode() {
97 return this.getHostname().hashCode() ^
getPort();
101 public int compareTo(Address that
) {
102 int compare
= this.getHostname().compareTo(that
.getHostname());
107 return this.getPort() - that
.getPort();