Skip to content

Commit

Permalink
Added missing comments to Socket Connection DTO
Browse files Browse the repository at this point in the history
  • Loading branch information
amitjoy committed Jun 25, 2024
1 parent 43eab02 commit 313ec73
Showing 1 changed file with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,27 @@

import static java.util.Objects.requireNonNull;

/**
* The SocketConnection class represents a socket connection configuration.
* It encapsulates various parameters required to establish a connection
* to a socket server.
*/
public class SocketConnection {

// The host address of the socket server
private final String host;
// The port number to connect to the socket server
private final int port;
// The connection timeout value
private final int timeout;
// The trust store file path for SSL/TLS
private final String trustStore;
// The password for the trust store
private final String trustStorePassword;

/**
* Private constructor to enforce the use of the builder for object creation.
*/
private SocketConnection(final String host,
final int port,
final int timeout,
Expand All @@ -37,6 +50,8 @@ private SocketConnection(final String host,
this.trustStorePassword = trustStorePassword;
}

// Getter methods for each field

public String host() {
return host;
}
Expand All @@ -57,10 +72,16 @@ public String trustStorePassword() {
return trustStorePassword;
}

/**
* Returns a new builder instance for constructing a SocketConnection.
*/
public static SocketConnectionBuilder builder() {
return new SocketConnectionBuilder();
}

/**
* Builder class for constructing SocketConnection instances.
*/
public static class SocketConnectionBuilder {

private String host;
Expand All @@ -69,11 +90,18 @@ public static class SocketConnectionBuilder {
private String trustStore;
private String trustStorePassword;

/**
* Sets the host field and returns the builder instance.
*/
public SocketConnectionBuilder host(final String host) {
this.host = requireNonNull(host, "'host' cannot be null");
return this;
}

/**
* Sets the port field and returns the builder instance.
* Validates that the port is greater than zero.
*/
public SocketConnectionBuilder port(final int port) {
if (port <= 0) {
throw new IllegalArgumentException("'port' cannot be less than or equal to zero");
Expand All @@ -82,6 +110,10 @@ public SocketConnectionBuilder port(final int port) {
return this;
}

/**
* Sets the timeout field and returns the builder instance.
* Validates that the timeout is not negative.
*/
public SocketConnectionBuilder timeout(final int timeout) {
if (timeout < -1) {
throw new IllegalArgumentException("'timeout' cannot be negative");
Expand All @@ -90,20 +122,27 @@ public SocketConnectionBuilder timeout(final int timeout) {
return this;
}

/**
* Sets the trustStore field and returns the builder instance.
*/
public SocketConnectionBuilder truststore(final String trustStore) {
this.trustStore = trustStore;
return this;
}

/**
* Sets the trustStorePassword field and returns the builder instance.
*/
public SocketConnectionBuilder truststorePass(final String trustStorePassword) {
this.trustStorePassword = trustStorePassword;
return this;
}

/**
* Builds and returns a SocketConnection instance with the specified parameters.
*/
public SocketConnection build() {
return new SocketConnection(host, port, timeout, trustStore, trustStorePassword);
}

}

}

0 comments on commit 313ec73

Please sign in to comment.