All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codehaus.httpcache4j.resolver.ConnectionConfiguration Maven / Gradle / Ivy

The newest version!
package org.codehaus.httpcache4j.resolver;

import org.codehaus.httpcache4j.HTTPHost;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author Erlend Hamnaberg
 * @version $Revision: $
 */
public class ConnectionConfiguration {
    private final Optional timeout;
    private final Optional socketTimeout;
    private final Optional connectionRequestTimeout;
    private final Optional defaultConnectionsPerHost;
    private final Optional maxConnections;
    private final Map connectionsPerHost = new HashMap<>();

    public ConnectionConfiguration(Optional timeout, Optional socketTimeout, Optional connectionRequestTimeout, Optional defaultConnectionsPerHost, Optional maxConnections, Map connectionsPerHost) {
        this.timeout = timeout;
        this.socketTimeout = socketTimeout;
        this.connectionRequestTimeout = connectionRequestTimeout;
        this.defaultConnectionsPerHost = defaultConnectionsPerHost;
        this.maxConnections = maxConnections;
        this.connectionsPerHost.putAll(connectionsPerHost);
    }

    public ConnectionConfiguration() {
        this(Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Collections.emptyMap());
    }

    public Optional getTimeout() {
        return timeout;
    }

    public Optional getSocketTimeout() {
        return socketTimeout;
    }

    public Optional getConnectionRequestTimeout() { return connectionRequestTimeout; }

    public Optional getDefaultConnectionsPerHost() {
        return defaultConnectionsPerHost;
    }

    public Optional getMaxConnections() {
        return maxConnections;
    }

    public Map getConnectionsPerHost() {
        return connectionsPerHost;
    }

    /**
     * Mutable builder.
     */
    public static class Builder {
        private Optional timeout = Optional.empty();
        private Optional socketTimeout = Optional.empty();
        private Optional connectionRequestTimeout = Optional.empty();
        private Optional defaultConnectionPerHost = Optional.empty();
        private Optional maxConnections = Optional.empty();
        private final Map connectionsPerHost = new HashMap();

        public Builder setTimeout(int timeout) {
            this.timeout = Optional.of(timeout);
            return this;
        }

        public Builder setSocketTimeout(int socketTimeout) {
            this.socketTimeout = Optional.of(socketTimeout);
            return this;
        }

        public Builder setConnectionRequestTimeout(int connectionRequestTimeout) {
            this.connectionRequestTimeout = Optional.of(connectionRequestTimeout);
            return this;
        }

        public Builder setDefaultConnectionPerHost(int defaultConnectionPerHost) {
            this.defaultConnectionPerHost = Optional.of(defaultConnectionPerHost);
            return this;
        }

        public Builder setMaxConnections(int maxConnections) {
            this.maxConnections = Optional.of(maxConnections);
            return this;
        }

        public Builder add(HTTPHost host, int connections) {
            this.connectionsPerHost.put(host, connections);
            return this;
        }

        public ConnectionConfiguration build() {
            return new ConnectionConfiguration(timeout, socketTimeout, connectionRequestTimeout, defaultConnectionPerHost, maxConnections, connectionsPerHost);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy