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

de.mklinger.commons.httpclient.internal.HttpHeadersImpl Maven / Gradle / Ivy

There is a newer version: 0.10
Show newest version
package de.mklinger.commons.httpclient.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import de.mklinger.commons.httpclient.HttpHeaders;

public class HttpHeadersImpl implements HttpHeaders {
	private final Map> map;

	public HttpHeadersImpl() {
		this.map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
	}

	// TODO use builder pattern for HTTPHeaders
	public static HttpHeaders deepCopy(final HttpHeadersImpl other) {
		final HttpHeadersImpl copy = new HttpHeadersImpl();
		final Set keys = other.map.keySet();
		for (final String key : keys) {
			final List values = other.map.get(key);
			final List copiedValues = new ArrayList<>(values);
			copy.map.put(key, copiedValues);
		}
		return copy;
	}

	@Override
	public Map> map() {
		return Collections.unmodifiableMap(map);
	}

	public void addHeader(final String name, final String value) {
		map.computeIfAbsent(name, unused -> new ArrayList<>(1))
		.add(value);
	}

	public void setHeader(final String name, final String value) {
		final List values = new ArrayList<>(1);
		values.add(value);
		map.put(name, values);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy