-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHostPort.java
45 lines (43 loc) · 1.07 KB
/
HostPort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package unimelb.bitbox.util;
/**
* Simple class to manage a host string and port number. Provides conversion to and from a {@link Document}
* which further provides conversion to a JSON string.
* @author aaron
*
*/
public class HostPort {
public String host;
public int port;
public HostPort(String host, int port) {
this.host=host;
this.port=port;
}
public HostPort(String hostPort) {
this.host=hostPort.split(":")[0];
this.port=Integer.parseInt(hostPort.split(":")[1]);
}
public HostPort(Document hostPort) {
this.host=hostPort.getString("host");
this.port=(int) hostPort.getLong("port");
}
public Document toDoc() {
Document hp = new Document();
hp.append("host", host);
hp.append("port", port);
return hp;
}
public String toString() {
return host+":"+port;
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof HostPort)) {
return false;
}
HostPort c = (HostPort) o;
return host.equals(c.host) && port==c.port;
}
}