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 package org.apache.hc.core5.net;
28
29 import java.io.Serializable;
30 import java.net.URISyntaxException;
31 import java.util.Locale;
32
33 import org.apache.hc.core5.annotation.Contract;
34 import org.apache.hc.core5.annotation.ThreadingBehavior;
35 import org.apache.hc.core5.util.Args;
36 import org.apache.hc.core5.util.LangUtils;
37 import org.apache.hc.core5.util.TextUtils;
38
39
40
41
42
43
44
45 @Contract(threading = ThreadingBehavior.IMMUTABLE)
46 public final class Host implements NamedEndpoint, Serializable {
47
48 private static final long serialVersionUID = 1L;
49 private final String name;
50 private final String lcName;
51 private final int port;
52
53 public Host(final String name, final int port) {
54 super();
55 this.name = Args.containsNoBlanks(name, "Host name");
56 this.port = Ports.check(port);
57 this.lcName = this.name.toLowerCase(Locale.ROOT);
58 }
59
60 public static Host create(final String s) throws URISyntaxException {
61 Args.notEmpty(s, "HTTP Host");
62 final int portIdx = s.lastIndexOf(":");
63 final int port;
64 if (portIdx > 0) {
65 try {
66 port = Integer.parseInt(s.substring(portIdx + 1));
67 } catch (final NumberFormatException ex) {
68 throw new URISyntaxException(s, "invalid port");
69 }
70 final String hostname = s.substring(0, portIdx);
71 if (TextUtils.containsBlanks(hostname)) {
72 throw new URISyntaxException(s, "hostname contains blanks");
73 }
74 return new Host(hostname, port);
75 }
76 throw new URISyntaxException(s, "port not found");
77 }
78
79 @Override
80 public String getHostName() {
81 return name;
82 }
83
84 @Override
85 public int getPort() {
86 return port;
87 }
88
89 @Override
90 public boolean equals(final Object o) {
91 if (this == o) {
92 return true;
93 }
94 if (o instanceof Host) {
95 final Hostef="../../../../../org/apache/hc/core5/net/Host.html#Host">Host that = (Host) o;
96 return this.lcName.equals(that.lcName) && this.port == that.port;
97 }
98 return false;
99 }
100
101 @Override
102 public int hashCode() {
103 int hash = LangUtils.HASH_SEED;
104 hash = LangUtils.hashCode(hash, this.lcName);
105 hash = LangUtils.hashCode(hash, this.port);
106 return hash;
107 }
108
109 @Override
110 public String toString() {
111 return name + ":" + port;
112 }
113
114 }