Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builder for Options #60

Open
FZambia opened this issue Feb 14, 2023 · 0 comments
Open

Builder for Options #60

FZambia opened this issue Feb 14, 2023 · 0 comments

Comments

@FZambia
Copy link
Member

FZambia commented Feb 14, 2023

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

Code
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<String, String> 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<String, String> 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<String, String> 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<String, String> 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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant