|
| 1 | +package io.socket.client; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | + |
| 7 | +/** |
| 8 | + * Convenient builder class that helps creating |
| 9 | + * {@link io.socket.client.IO.Options Client Option} object as builder pattern. |
| 10 | + * Finally, you can get option object with call {@link #build()} method. |
| 11 | + * |
| 12 | + * @author junbong |
| 13 | + */ |
| 14 | +public class SocketOptionBuilder { |
| 15 | + /** |
| 16 | + * Construct new builder with default preferences. |
| 17 | + * |
| 18 | + * @return new builder object |
| 19 | + * @see SocketOptionBuilder#builder(IO.Options) |
| 20 | + */ |
| 21 | + public static SocketOptionBuilder builder() { |
| 22 | + return new SocketOptionBuilder(); |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + /** |
| 27 | + * Construct this builder from specified option object. |
| 28 | + * The option that returned from {@link #build()} method |
| 29 | + * is not equals with given option. |
| 30 | + * In other words, builder creates new option object |
| 31 | + * and copy all preferences from given option. |
| 32 | + * |
| 33 | + * @param options option object which to copy preferences |
| 34 | + * @return new builder object |
| 35 | + */ |
| 36 | + public static SocketOptionBuilder builder(IO.Options options) { |
| 37 | + return new SocketOptionBuilder(options); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + private final IO.Options options = new IO.Options(); |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * Construct new builder with default preferences. |
| 46 | + */ |
| 47 | + protected SocketOptionBuilder() { |
| 48 | + this(null); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + /** |
| 53 | + * Construct this builder from specified option object. |
| 54 | + * The option that returned from {@link #build()} method |
| 55 | + * is not equals with given option. |
| 56 | + * In other words, builder creates new option object |
| 57 | + * and copy all preferences from given option. |
| 58 | + * |
| 59 | + * @param options option object which to copy preferences. Null-ok. |
| 60 | + */ |
| 61 | + protected SocketOptionBuilder(IO.Options options) { |
| 62 | + if (options != null) { |
| 63 | + this.setForceNew(options.forceNew) |
| 64 | + .setMultiplex(options.multiplex) |
| 65 | + .setReconnection(options.reconnection) |
| 66 | + .setReconnectionAttempts(options.reconnectionAttempts) |
| 67 | + .setReconnectionDelay(options.reconnectionDelay) |
| 68 | + .setReconnectionDelayMax(options.reconnectionDelayMax) |
| 69 | + .setRandomizationFactor(options.randomizationFactor) |
| 70 | + .setTimeout(options.timeout) |
| 71 | + .setTransports(options.transports) |
| 72 | + .setUpgrade(options.upgrade) |
| 73 | + .setRememberUpgrade(options.rememberUpgrade) |
| 74 | + .setHost(options.host) |
| 75 | + .setHostname(options.hostname) |
| 76 | + .setPort(options.port) |
| 77 | + .setPolicyPort(options.policyPort) |
| 78 | + .setSecure(options.secure) |
| 79 | + .setPath(options.path) |
| 80 | + .setQuery(options.query) |
| 81 | + .setAuth(options.auth) |
| 82 | + .setExtraHeaders(options.extraHeaders); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public SocketOptionBuilder setForceNew(boolean forceNew) { |
| 87 | + this.options.forceNew = forceNew; |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + public SocketOptionBuilder setMultiplex(boolean multiplex) { |
| 92 | + this.options.multiplex = multiplex; |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + public SocketOptionBuilder setReconnection(boolean reconnection) { |
| 97 | + this.options.reconnection = reconnection; |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + public SocketOptionBuilder setReconnectionAttempts(int reconnectionAttempts) { |
| 102 | + this.options.reconnectionAttempts = reconnectionAttempts; |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + public SocketOptionBuilder setReconnectionDelay(long reconnectionDelay) { |
| 107 | + this.options.reconnectionDelay = reconnectionDelay; |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + public SocketOptionBuilder setReconnectionDelayMax(long reconnectionDelayMax) { |
| 112 | + this.options.reconnectionDelayMax = reconnectionDelayMax; |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + public SocketOptionBuilder setRandomizationFactor(double randomizationFactor) { |
| 118 | + this.options.randomizationFactor = randomizationFactor; |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + public SocketOptionBuilder setTimeout(long timeout) { |
| 123 | + this.options.timeout = timeout; |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + public SocketOptionBuilder setTransports(String[] transports) { |
| 128 | + this.options.transports = transports; |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + public SocketOptionBuilder setUpgrade(boolean upgrade) { |
| 133 | + this.options.upgrade = upgrade; |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | + public SocketOptionBuilder setRememberUpgrade(boolean rememberUpgrade) { |
| 138 | + this.options.rememberUpgrade = rememberUpgrade; |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + public SocketOptionBuilder setHost(String host) { |
| 143 | + this.options.host = host; |
| 144 | + return this; |
| 145 | + } |
| 146 | + |
| 147 | + public SocketOptionBuilder setHostname(String hostname) { |
| 148 | + this.options.hostname = hostname; |
| 149 | + return this; |
| 150 | + } |
| 151 | + |
| 152 | + public SocketOptionBuilder setPort(int port) { |
| 153 | + this.options.port = port; |
| 154 | + return this; |
| 155 | + } |
| 156 | + |
| 157 | + public SocketOptionBuilder setPolicyPort(int policyPort) { |
| 158 | + this.options.policyPort = policyPort; |
| 159 | + return this; |
| 160 | + } |
| 161 | + |
| 162 | + public SocketOptionBuilder setQuery(String query) { |
| 163 | + this.options.query = query; |
| 164 | + return this; |
| 165 | + } |
| 166 | + |
| 167 | + public SocketOptionBuilder setSecure(boolean secure) { |
| 168 | + this.options.secure = secure; |
| 169 | + return this; |
| 170 | + } |
| 171 | + |
| 172 | + public SocketOptionBuilder setPath(String path) { |
| 173 | + this.options.path = path; |
| 174 | + return this; |
| 175 | + } |
| 176 | + |
| 177 | + public SocketOptionBuilder setAuth(Map<String, String> auth) { |
| 178 | + this.options.auth = auth; |
| 179 | + return this; |
| 180 | + } |
| 181 | + |
| 182 | + public SocketOptionBuilder setExtraHeaders(Map<String, List<String>> extraHeaders) { |
| 183 | + this.options.extraHeaders = extraHeaders; |
| 184 | + return this; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Finally retrieve {@link io.socket.client.IO.Options} object |
| 189 | + * from this builder. |
| 190 | + * |
| 191 | + * @return option that built from this builder |
| 192 | + */ |
| 193 | + public IO.Options build() { |
| 194 | + return this.options; |
| 195 | + } |
| 196 | +} |
0 commit comments