--- old/src/java.httpclient/share/classes/java/net/http/HttpHeadersImpl.java 2016-04-29 11:47:25.555376088 +0100 +++ new/src/java.httpclient/share/classes/java/net/http/HttpHeadersImpl.java 2016-04-29 11:47:25.387376083 +0100 @@ -24,44 +24,22 @@ package java.net.http; import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.TreeMap; /** * Implementation of HttpHeaders. */ -class HttpHeadersImpl implements HttpHeaders1 { +class HttpHeadersImpl implements HttpHeaders { - private final HashMap> headers; - private boolean isUnmodifiable = false; + private final TreeMap> headers; public HttpHeadersImpl() { - headers = new HashMap<>(); - } - - /** - * Replace all List in headers with unmodifiable Lists. Call - * this only after all headers are added. The headers HashMap - * is wrapped with an unmodifiable HashMap in map() - */ - @Override - public void makeUnmodifiable() { - if (isUnmodifiable) - return; - - Set keys = new HashSet<>(headers.keySet()); - for (String key : keys) { - List values = headers.remove(key); - if (values != null) { - headers.put(key, Collections.unmodifiableList(values)); - } - } - isUnmodifiable = true; + headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); } @Override @@ -88,7 +66,7 @@ public HttpHeadersImpl deepCopy() { HttpHeadersImpl h1 = new HttpHeadersImpl(); - HashMap> headers1 = h1.headers; + TreeMap> headers1 = h1.headers; Set keys = headers.keySet(); for (String key : keys) { List vals = headers.get(key); @@ -98,22 +76,13 @@ return h1; } - private List getOrCreate(String name) { - List l = headers.get(name); - if (l == null) { - l = new LinkedList<>(); - headers.put(name, l); - } - return l; - } - void addHeader(String name, String value) { - List l = getOrCreate(name); - l.add(value); + headers.computeIfAbsent(name, k -> new LinkedList<>()) + .add(value); } void setHeader(String name, String value) { - List l = getOrCreate(name); + List l = headers.computeIfAbsent(name, k -> new LinkedList<>()); l.clear(); l.add(value); } @@ -122,7 +91,7 @@ public Optional firstValueAsLong(String name) { List l = headers.get(name); if (l == null) { - return Optional.ofNullable(null); + return Optional.empty(); } else { String v = l.get(0); Long lv = Long.parseLong(v); @@ -133,4 +102,4 @@ void clear() { headers.clear(); } -} +} \ No newline at end of file