1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.net;
19
20 import java.net.InetSocketAddress;
21
22 import org.apache.hadoop.hbase.classification.InterfaceAudience;
23 import org.apache.hadoop.hbase.classification.InterfaceStability;
24
25 import com.google.common.net.HostAndPort;
26
27
28
29
30
31
32
33
34
35 @InterfaceAudience.Public
36 @InterfaceStability.Stable
37 public class Address implements Comparable<Address> {
38 private final HostAndPort hostAndPort;
39
40 private Address(HostAndPort hostAndPort) {
41 this.hostAndPort = hostAndPort;
42 }
43
44 public static Address fromParts(String hostname, int port) {
45 return new Address(HostAndPort.fromParts(hostname, port));
46 }
47
48 public static Address fromString(String hostnameAndPort) {
49 return new Address(HostAndPort.fromString(hostnameAndPort));
50 }
51
52 public static Address fromSocketAddress(InetSocketAddress addr) {
53 return Address.fromParts(addr.getHostString(), addr.getPort());
54 }
55
56 public InetSocketAddress toSocketAddress() {
57 return new InetSocketAddress(getHostName(), getPort());
58 }
59
60 public static InetSocketAddress[] toSocketAddress(Address[] addrs) {
61 if (addrs == null) {
62 return null;
63 }
64 InetSocketAddress[] result = new InetSocketAddress[addrs.length];
65 for (int i = 0; i < addrs.length; i++) {
66 result[i] = addrs[i].toSocketAddress();
67 }
68 return result;
69 }
70
71 public String getHostName() {
72 return this.hostAndPort.getHostText();
73 }
74
75
76
77
78 @Deprecated
79 public String getHostname() {
80 return this.hostAndPort.getHostText();
81 }
82
83 public int getPort() {
84 return this.hostAndPort.getPort();
85 }
86
87 @Override
88 public String toString() {
89 return this.hostAndPort.toString();
90 }
91
92 @Override
93
94
95 public boolean equals(Object other) {
96 if (this == other) {
97 return true;
98 }
99 if (other instanceof Address) {
100 Address that = (Address)other;
101 return this.getHostName().equals(that.getHostName()) &&
102 this.getPort() == that.getPort();
103 }
104 return false;
105 }
106
107 @Override
108 public int hashCode() {
109 return this.getHostName().hashCode() ^ getPort();
110 }
111
112 @Override
113 public int compareTo(Address that) {
114 int compare = this.getHostName().compareTo(that.getHostName());
115 if (compare != 0) {
116 return compare;
117 }
118
119 return this.getPort() - that.getPort();
120 }
121 }