View Javadoc

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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
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   * 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.
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     * @deprecated Use {@link #getHostName()} instead
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    // Don't use HostAndPort equals... It is wonky including
94    // ipv6 brackets
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 }