package jdk.incubator.http.internal.websocket;
import jdk.incubator.http.HttpClient;
import jdk.incubator.http.WebSocket;
import jdk.incubator.http.WebSocket.Builder;
import jdk.incubator.http.WebSocket.Listener;
import jdk.incubator.http.internal.common.Pair;
import java.net.URI;
import java.time.Duration;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import static java.util.Objects.requireNonNull;
import static jdk.incubator.http.internal.common.Pair.pair;
public final class BuilderImpl implements Builder {
private final HttpClient client;
private final URI uri;
private final Listener listener;
private final Collection<Pair<String, String>> = new LinkedList<>();
private final Collection<String> subprotocols = new LinkedList<>();
private Duration timeout;
public BuilderImpl(HttpClient client, URI uri, Listener listener) {
this.client = requireNonNull(client, "client");
this.uri = requireNonNull(uri, "uri");
this.listener = requireNonNull(listener, "listener");
}
@Override
public Builder (String name, String value) {
requireNonNull(name, "name");
requireNonNull(value, "value");
headers.add(pair(name, value));
return this;
}
@Override
public Builder subprotocols(String mostPreferred,
String... lesserPreferred)
{
requireNonNull(mostPreferred, "mostPreferred");
requireNonNull(lesserPreferred, "lesserPreferred");
List<String> subprotocols = new LinkedList<>();
subprotocols.add(mostPreferred);
for (int i = 0; i < lesserPreferred.length; i++) {
String p = lesserPreferred[i];
requireNonNull(p, "lesserPreferred[" + i + "]");
subprotocols.add(p);
}
this.subprotocols.clear();
this.subprotocols.addAll(subprotocols);
return this;
}
@Override
public Builder connectTimeout(Duration timeout) {
this.timeout = requireNonNull(timeout, "timeout");
return this;
}
@Override
public CompletableFuture<WebSocket> buildAsync() {
return WebSocketImpl.newInstanceAsync(this);
}
HttpClient getClient() { return client; }
URI getUri() { return uri; }
Listener getListener() { return listener; }
Collection<Pair<String, String>> () { return headers; }
Collection<String> getSubprotocols() { return subprotocols; }
Duration getConnectTimeout() { return timeout; }
}