Skip to content

Commit 8df737b

Browse files
authored
use lombok builder for Endpoint (#125)
1 parent 746bfd5 commit 8df737b

File tree

2 files changed

+37
-86
lines changed

2 files changed

+37
-86
lines changed

src/main/java/org/rocstreaming/roctoolkit/Endpoint.java

Lines changed: 26 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.rocstreaming.roctoolkit;
22

3+
import lombok.Builder;
4+
import lombok.EqualsAndHashCode;
5+
import lombok.Getter;
6+
37
/**
48
* Network endpoint.
59
* <p>
@@ -45,18 +49,40 @@
4549
* <p>
4650
* Can't be used concurrently
4751
*/
52+
@Getter
53+
@Builder(builderClassName = "Builder", toBuilder = true)
54+
@EqualsAndHashCode
4855
public class Endpoint {
4956

5057
static {
5158
RocLibrary.loadLibrary();
5259
}
5360

61+
/**
62+
* Protocol
63+
*/
5464
private Protocol protocol;
5565

66+
/**
67+
* Host specifies FQDN, IPv4 address, or IPv6 address
68+
*/
5669
private String host;
5770

71+
/**
72+
* Port specifies UDP or TCP port in range [0; 65535]
73+
* <p>
74+
* When binding an endpoint, the port may be set to zero to select a random port.
75+
* The selected port will be then written back to the endpoint. When connecting
76+
* an endpoint, the port should be positive.
77+
* <p>
78+
* If port is set to -1, the standard port for endpoint protocol is used. This is
79+
* allowed only if the protocol defines its standard port.
80+
*/
5881
private int port;
5982

83+
/**
84+
* Resource nullable. Specifies percent-encoded path and query
85+
*/
6086
private String resource;
6187

6288
/**
@@ -109,81 +135,6 @@ public Endpoint(Protocol protocol, String host, int port) {
109135
this(protocol, host, port, null);
110136
}
111137

112-
/**
113-
* Builder class for {@link Endpoint}
114-
*/
115-
public static class Builder {
116-
117-
private Protocol protocol;
118-
119-
private String host;
120-
121-
private int port;
122-
123-
private String resource;
124-
125-
/**
126-
* Set protocol
127-
* @param protocol protocol
128-
* @return this Builder
129-
*/
130-
public Builder setProtocol(Protocol protocol) {
131-
this.protocol = protocol;
132-
return this;
133-
}
134-
135-
136-
/**
137-
* Set host
138-
* @param host host
139-
* @return this Builder
140-
*/
141-
public Builder setHost(String host) {
142-
this.host = host;
143-
return this;
144-
}
145-
146-
/**
147-
* Set port
148-
* @param port port
149-
* @return this Builder
150-
*/
151-
public Builder setPort(int port) {
152-
this.port = port;
153-
return this;
154-
}
155-
156-
/**
157-
* Set resource
158-
* @param resource resource
159-
* @return this Builder
160-
*/
161-
public Builder setResource(String resource) {
162-
this.resource = resource;
163-
return this;
164-
}
165-
166-
public Endpoint build() {
167-
return new Endpoint(this.protocol, this.host, this.port, this.resource);
168-
}
169-
}
170-
171-
public Protocol getProtocol() {
172-
return protocol;
173-
}
174-
175-
public String getHost() {
176-
return host;
177-
}
178-
179-
public int getPort() {
180-
return port;
181-
}
182-
183-
public String getResource() {
184-
return resource;
185-
}
186-
187138
@Override
188139
public String toString() {
189140
return getUri();

src/test/java/org/rocstreaming/roctoolkit/EndpointTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -289,18 +289,18 @@ public void testEndpointComponents(Params params) {
289289
public void testEndpointBuilder(Params params) {
290290
if (params.componentsException != null) {
291291
assertThrows(params.componentsException, () -> new Endpoint.Builder()
292-
.setProtocol(params.protocol)
293-
.setHost(params.host)
294-
.setPort(params.port)
295-
.setResource(params.resource)
292+
.protocol(params.protocol)
293+
.host(params.host)
294+
.port(params.port)
295+
.resource(params.resource)
296296
.build());
297297
return;
298298
}
299299
Endpoint endpoint = new Endpoint.Builder()
300-
.setProtocol(params.protocol)
301-
.setHost(params.host)
302-
.setPort(params.port)
303-
.setResource(params.resource)
300+
.protocol(params.protocol)
301+
.host(params.host)
302+
.port(params.port)
303+
.resource(params.resource)
304304
.build();
305305
assertEquals(params.protocol, endpoint.getProtocol());
306306
assertEquals(params.host, endpoint.getHost());
@@ -312,9 +312,9 @@ public void testEndpointBuilder(Params params) {
312312
@Test
313313
public void testInvalidEndpointBuilder() {
314314
assertThrows(IllegalArgumentException.class, () -> new Endpoint.Builder().build());
315-
assertThrows(IllegalArgumentException.class, () -> new Endpoint.Builder().setHost("host").build());
316-
assertThrows(IllegalArgumentException.class, () -> new Endpoint.Builder().setHost("host").setPort(1).build());
317-
assertThrows(IllegalArgumentException.class, () -> new Endpoint.Builder().setHost("host").setResource("/resource").build());
315+
assertThrows(IllegalArgumentException.class, () -> new Endpoint.Builder().host("host").build());
316+
assertThrows(IllegalArgumentException.class, () -> new Endpoint.Builder().host("host").port(1).build());
317+
assertThrows(IllegalArgumentException.class, () -> new Endpoint.Builder().host("host").resource("/resource").build());
318318
}
319319

320320
@Test

0 commit comments

Comments
 (0)