centrifugal / centrifuge-java

General Java and Android client SDK for bidirectional communication with Centrifugo and Centrifuge-based server over WebSocket
MIT License
65 stars 33 forks source link

Builder for Options #60

Open FZambia opened 1 year ago

FZambia commented 1 year ago

One more thing to improve is using builder pattern for options:

Code ```java package io.github.centrifugal.centrifuge; import java.net.Proxy; import java.util.Map; /** * Configuration for a {@link Client} instance. */ public class Options { private final String token; private final ConnectionTokenGetter tokenGetter; private final String name; private final String version; private final byte[] data; private final Map headers; private final int timeout; private final int minReconnectDelay; private final int maxReconnectDelay; private final int maxServerPingDelay; private final Proxy proxy; private final String proxyLogin; private final String proxyPassword; private final Dns dns; public String getToken() { return token; } public ConnectionTokenGetter getTokenGetter() { return tokenGetter; } public String getName() { return name; } public String getVersion() { return version; } public byte[] getData() { return data; } public Map getHeaders() { return headers; } public int getTimeout() { return timeout; } public int getMinReconnectDelay() { return minReconnectDelay; } public int getMaxReconnectDelay() { return maxReconnectDelay; } public int getMaxServerPingDelay() { return maxServerPingDelay; } public Proxy getProxy() { return proxy; } public String getProxyLogin() { return proxyLogin; } public String getProxyPassword() { return proxyPassword; } public Dns getDns() { return this.dns; } private Options(Builder builder) { this.token = builder.token; this.tokenGetter = builder.tokenGetter; this.name = builder.name; this.version = builder.version; this.data = builder.data; this.headers = builder.headers; this.timeout = builder.timeout; this.minReconnectDelay = builder.minReconnectDelay; this.maxReconnectDelay = builder.maxReconnectDelay; this.maxServerPingDelay = builder.maxServerPingDelay; this.proxy = builder.proxy; this.proxyLogin = builder.proxyLogin; this.proxyPassword = builder.proxyPassword; this.dns = builder.dns; } public static class Builder { private String token = ""; private ConnectionTokenGetter tokenGetter; private String name = "java"; private String version = ""; private byte[] data; private Map headers; private int timeout = 5000; private int minReconnectDelay = 500; private int maxReconnectDelay = 20000; private int maxServerPingDelay = 10000; private Proxy proxy; private String proxyLogin; private String proxyPassword; private Dns dns; /** * Set connection token. This is a token you have to receive from your application backend. * If your tokens expire and you want SDK to automatically refresh tokens then set * ConnectionTokenGetter (see below). */ public Builder setToken(String token) { this.token = token; return this; } /** * Set a method to extract new connection token upon expiration. */ public Builder setTokenGetter(ConnectionTokenGetter tokenGetter) { this.tokenGetter = tokenGetter; return this; } /** * Set client name - name of this client. This should not be unique per client – it * identifies client application name actually, so name should have a limited * number of possible values. By default this client uses "java" as a name. */ public Builder setName(String name) { this.name = name; return this; } /** * Set client version - version of application. This may be used for observability * on the server (for example in analytics). */ public Builder setVersion(String version) { this.version = version; return this; } /** * Set custom connection data. This data will be delivered to server in Connect command. * For Centrifugo this may be useful in case of using connect proxy. */ public Builder setData(byte[] data) { this.data = data; return this; } /** * Set custom headers for WebSocket Upgrade request. */ public Builder setHeaders(Map headers) { this.headers = headers; return this; } /** * Set custom timeout for requests in milliseconds. By default, 5000 is used. */ public Builder setTimeout(int timeout) { this.timeout = timeout; return this; } /** * Set minimal time before reconnect attempt in milliseconds. By default, 500 is used. */ public Builder setMinReconnectDelay(int minReconnectDelay) { this.minReconnectDelay = minReconnectDelay; return this; } /** * Set max time between reconnect attempts in milliseconds. By default, 20000 is used. */ public Builder setMaxReconnectDelay(int maxReconnectDelay) { this.maxReconnectDelay = maxReconnectDelay; return this; } /** * Set max time of ping delay from server in milliseconds. By default, 10000 is used. */ public Builder setMaxServerPingDelay(int maxServerPingDelay) { this.maxServerPingDelay = maxServerPingDelay; return this; } /** * Set proxy to use. */ public Builder setProxy(Proxy proxy) { this.proxy = proxy; return this; } /** * Set proxy credentials. */ public Builder setProxyCredentials(String login, String password) { this.proxyLogin = login; this.proxyPassword = password; return this; } /** * Set custom DNS resolver. */ public Builder setDns(Dns dns) { this.dns = dns; return this; } public Options build() { return new Options(this); } } } ```

Possibly we could have both current (for compatibility) and builder approach. Though the main benefit of Builder is immutability - combining both approaches makes the benefit less obvious (as we could just return this from current setters).