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