View Javadoc

1   // ========================================================================
2   // Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.client;
15  
16  import java.net.InetSocketAddress;
17  
18  /**
19   * @version $Revision: 4135 $ $Date: 2008-12-02 11:57:07 +0100 (Tue, 02 Dec 2008) $
20   */
21  public class Address
22  {
23      private final String host;
24      private final int port;
25  
26      public static Address from(String hostAndPort)
27      {
28          String host;
29          int port;
30          int colon = hostAndPort.indexOf(':');
31          if (colon >= 0)
32          {
33              host = hostAndPort.substring(0, colon);
34              port = Integer.parseInt(hostAndPort.substring(colon + 1));
35          }
36          else
37          {
38              host = hostAndPort;
39              port = 0;
40          }
41          return new Address(host, port);
42      }
43  
44      public Address(String host, int port)
45      {
46          if (host == null)
47              throw new IllegalArgumentException("Host is null");
48          
49          this.host = host.trim();
50          this.port = port;
51      }
52  
53      @Override
54      public boolean equals(Object obj)
55      {
56          if (this == obj) return true;
57          if (obj == null || getClass() != obj.getClass()) return false;
58          Address that = (Address)obj;
59          if (!host.equals(that.host)) return false;
60          return port == that.port;
61      }
62  
63      @Override
64      public int hashCode()
65      {
66          int result = host.hashCode();
67          result = 31 * result + port;
68          return result;
69      }
70  
71      public String getHost()
72      {
73          return host;
74      }
75  
76      public int getPort()
77      {
78          return port;
79      }
80  
81      public InetSocketAddress toSocketAddress()
82      {
83          return new InetSocketAddress(getHost(), getPort());
84      }
85  
86      @Override
87      public String toString()
88      {
89          return host + ":" + port;
90      }
91  }