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 org.apache.hadoop.hbase.classification.InterfaceAudience;
21  import org.apache.hadoop.hbase.classification.InterfaceStability;
22  
23  import com.google.common.net.HostAndPort;
24  
25  /**
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 <<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.
32   */
33  @InterfaceAudience.Public
34  @InterfaceStability.Stable
35  public class Address implements Comparable<Address> {
36    private HostAndPort hostAndPort;
37  
38    private Address(HostAndPort hostAndPort) {
39      this.hostAndPort = hostAndPort;
40    }
41  
42    public static Address fromParts(String hostname, int port) {
43      return new Address(HostAndPort.fromParts(hostname, port));
44    }
45  
46    public static Address fromString(String hostnameAndPort) {
47      return new Address(HostAndPort.fromString(hostnameAndPort));
48    }
49  
50    public String getHostname() {
51      return this.hostAndPort.getHostText();
52    }
53  
54    public int getPort() {
55      return this.hostAndPort.getPort();
56    }
57  
58    @Override
59    public String toString() {
60      return this.hostAndPort.toString();
61    }
62  
63    @Override
64    // Don't use HostAndPort equals... It is wonky including
65    // ipv6 brackets
66    public boolean equals(Object other) {
67      if (this == other) {
68        return true;
69      }
70      if (other instanceof Address) {
71        Address that = (Address)other;
72        return this.getHostname().equals(that.getHostname()) &&
73            this.getPort() == that.getPort();
74      }
75      return false;
76    }
77  
78    @Override
79    public int hashCode() {
80      return this.getHostname().hashCode() ^ getPort();
81    }
82  
83    @Override
84    public int compareTo(Address that) {
85      int compare = this.getHostname().compareTo(that.getHostname());
86      if (compare != 0) return compare;
87      return this.getPort() - that.getPort();
88    }
89  }