Skip to content

Commit

Permalink
feat: add options builder (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
junbong authored and darrachequesne committed Dec 14, 2020
1 parent 79cb27f commit 49068d3
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/io/socket/client/IO.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,21 @@ public static class Options extends Manager.Options {
* Whether to enable multiplexing. Default is true.
*/
public boolean multiplex = true;

/**
* <p>
* Retrieve new builder class that helps creating socket option as builder pattern.
* This method returns exactly same result as :
* </p>
* <code>
* SocketOptionBuilder builder = SocketOptionBuilder.builder();
* </code>
*
* @return builder class that helps creating socket option as builder pattern.
* @see SocketOptionBuilder#builder()
*/
public static SocketOptionBuilder builder() {
return SocketOptionBuilder.builder();
}
}
}
196 changes: 196 additions & 0 deletions src/main/java/io/socket/client/SocketOptionBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package io.socket.client;

import java.util.List;
import java.util.Map;


/**
* Convenient builder class that helps creating
* {@link io.socket.client.IO.Options Client Option} object as builder pattern.
* Finally, you can get option object with call {@link #build()} method.
*
* @author junbong
*/
public class SocketOptionBuilder {
/**
* Construct new builder with default preferences.
*
* @return new builder object
* @see SocketOptionBuilder#builder(IO.Options)
*/
public static SocketOptionBuilder builder() {
return new SocketOptionBuilder();
}


/**
* Construct this builder from specified option object.
* The option that returned from {@link #build()} method
* is not equals with given option.
* In other words, builder creates new option object
* and copy all preferences from given option.
*
* @param options option object which to copy preferences
* @return new builder object
*/
public static SocketOptionBuilder builder(IO.Options options) {
return new SocketOptionBuilder(options);
}


private final IO.Options options = new IO.Options();


/**
* Construct new builder with default preferences.
*/
protected SocketOptionBuilder() {
this(null);
}


/**
* Construct this builder from specified option object.
* The option that returned from {@link #build()} method
* is not equals with given option.
* In other words, builder creates new option object
* and copy all preferences from given option.
*
* @param options option object which to copy preferences. Null-ok.
*/
protected SocketOptionBuilder(IO.Options options) {
if (options != null) {
this.setForceNew(options.forceNew)
.setMultiplex(options.multiplex)
.setReconnection(options.reconnection)
.setReconnectionAttempts(options.reconnectionAttempts)
.setReconnectionDelay(options.reconnectionDelay)
.setReconnectionDelayMax(options.reconnectionDelayMax)
.setRandomizationFactor(options.randomizationFactor)
.setTimeout(options.timeout)
.setTransports(options.transports)
.setUpgrade(options.upgrade)
.setRememberUpgrade(options.rememberUpgrade)
.setHost(options.host)
.setHostname(options.hostname)
.setPort(options.port)
.setPolicyPort(options.policyPort)
.setSecure(options.secure)
.setPath(options.path)
.setQuery(options.query)
.setAuth(options.auth)
.setExtraHeaders(options.extraHeaders);
}
}

public SocketOptionBuilder setForceNew(boolean forceNew) {
this.options.forceNew = forceNew;
return this;
}

public SocketOptionBuilder setMultiplex(boolean multiplex) {
this.options.multiplex = multiplex;
return this;
}

public SocketOptionBuilder setReconnection(boolean reconnection) {
this.options.reconnection = reconnection;
return this;
}

public SocketOptionBuilder setReconnectionAttempts(int reconnectionAttempts) {
this.options.reconnectionAttempts = reconnectionAttempts;
return this;
}

public SocketOptionBuilder setReconnectionDelay(long reconnectionDelay) {
this.options.reconnectionDelay = reconnectionDelay;
return this;
}

public SocketOptionBuilder setReconnectionDelayMax(long reconnectionDelayMax) {
this.options.reconnectionDelayMax = reconnectionDelayMax;
return this;
}


public SocketOptionBuilder setRandomizationFactor(double randomizationFactor) {
this.options.randomizationFactor = randomizationFactor;
return this;
}

public SocketOptionBuilder setTimeout(long timeout) {
this.options.timeout = timeout;
return this;
}

public SocketOptionBuilder setTransports(String[] transports) {
this.options.transports = transports;
return this;
}

public SocketOptionBuilder setUpgrade(boolean upgrade) {
this.options.upgrade = upgrade;
return this;
}

public SocketOptionBuilder setRememberUpgrade(boolean rememberUpgrade) {
this.options.rememberUpgrade = rememberUpgrade;
return this;
}

public SocketOptionBuilder setHost(String host) {
this.options.host = host;
return this;
}

public SocketOptionBuilder setHostname(String hostname) {
this.options.hostname = hostname;
return this;
}

public SocketOptionBuilder setPort(int port) {
this.options.port = port;
return this;
}

public SocketOptionBuilder setPolicyPort(int policyPort) {
this.options.policyPort = policyPort;
return this;
}

public SocketOptionBuilder setQuery(String query) {
this.options.query = query;
return this;
}

public SocketOptionBuilder setSecure(boolean secure) {
this.options.secure = secure;
return this;
}

public SocketOptionBuilder setPath(String path) {
this.options.path = path;
return this;
}

public SocketOptionBuilder setAuth(Map<String, String> auth) {
this.options.auth = auth;
return this;
}

public SocketOptionBuilder setExtraHeaders(Map<String, List<String>> extraHeaders) {
this.options.extraHeaders = extraHeaders;
return this;
}

/**
* Finally retrieve {@link io.socket.client.IO.Options} object
* from this builder.
*
* @return option that built from this builder
*/
public IO.Options build() {
return this.options;
}
}

0 comments on commit 49068d3

Please sign in to comment.