1 package org.mortbay.jetty.client;
2
3 import java.net.InetSocketAddress;
4
5
6
7
8 public class Address
9 {
10 private final String host;
11 private final int port;
12
13 public static Address from(String hostAndPort)
14 {
15 String host;
16 int port;
17 int colon = hostAndPort.indexOf(':');
18 if (colon >= 0)
19 {
20 host = hostAndPort.substring(0, colon);
21 port = Integer.parseInt(hostAndPort.substring(colon + 1));
22 }
23 else
24 {
25 host = hostAndPort;
26 port = 0;
27 }
28 return new Address(host, port);
29 }
30
31 public Address(String host, int port)
32 {
33 this.host = host.trim();
34 this.port = port;
35 }
36
37 public boolean equals(Object obj)
38 {
39 if (this == obj) return true;
40 if (obj == null || getClass() != obj.getClass()) return false;
41 Address that = (Address)obj;
42 if (!host.equals(that.host)) return false;
43 return port == that.port;
44 }
45
46 public int hashCode()
47 {
48 int result = host.hashCode();
49 result = 31 * result + port;
50 return result;
51 }
52
53 public String getHost()
54 {
55 return host;
56 }
57
58 public int getPort()
59 {
60 return port;
61 }
62
63 public InetSocketAddress toSocketAddress()
64 {
65 return new InetSocketAddress(getHost(), getPort());
66 }
67
68 @Override
69 public String toString()
70 {
71 return host + ":" + port;
72 }
73 }