1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.client;
15
16 import java.net.InetSocketAddress;
17
18
19
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 this.host = host.trim();
47 this.port = port;
48 }
49
50 @Override
51 public boolean equals(Object obj)
52 {
53 if (this == obj) return true;
54 if (obj == null || getClass() != obj.getClass()) return false;
55 Address that = (Address)obj;
56 if (!host.equals(that.host)) return false;
57 return port == that.port;
58 }
59
60 @Override
61 public int hashCode()
62 {
63 int result = host.hashCode();
64 result = 31 * result + port;
65 return result;
66 }
67
68 public String getHost()
69 {
70 return host;
71 }
72
73 public int getPort()
74 {
75 return port;
76 }
77
78 public InetSocketAddress toSocketAddress()
79 {
80 return new InetSocketAddress(getHost(), getPort());
81 }
82
83 @Override
84 public String toString()
85 {
86 return host + ":" + port;
87 }
88 }