Skip to content

Add new SocketOptionBuilder which helps creating IO.Option as builder… #304

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/io/socket/client/IO.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,22 @@ 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()
* @see SocketOptionBuilder#builder(Options)
*/
public static SocketOptionBuilder builder() {
return SocketOptionBuilder.builder();
}
}
}
208 changes: 208 additions & 0 deletions src/main/java/io/socket/client/SocketOptionBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package io.socket.client;

import javax.net.ssl.HostnameVerifier;


/**
* 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)
.setHostnameVerifier(options.hostnameVerifier)
.setPort(options.port)
.setPolicyPort(options.policyPort)
.setSecure(options.secure)
.setPath(options.path)
.setQuery(options.query);
}
}


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 setHostnameVerifier(HostnameVerifier hostnameVerifier) {
this.options.hostnameVerifier = hostnameVerifier;
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;
}


/**
* 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;
}
}
Loading