[9] RFR: 8151182: HttpHeaders.allValues should return unmodifiable List as per JavaDoc

Paul Sandoz paul.sandoz at oracle.com
Fri Mar 4 14:05:55 UTC 2016


Hi Vaibhav,

This will not work, as Claes points out. You also might wanna check if there are tests in place asserting unmodfiablity, if there are none you could add some. Michael can point you in the right direction.

Follow the trail of HttpHeaders1.makeUnmodifiable(), which is used to transition map values from mutable to unmodifiable:

    @Override
    public void makeUnmodifiable() {
        if (isUnmodifiable)
            return;

        Set<String> keys = new HashSet<>(headers.keySet());
        for (String key : keys) {
            List<String> values = headers.remove(key);
            if (values != null) {
                headers.put(key, Collections.unmodifiableList(values));
            }
        }
        isUnmodifiable = true;
    }

In fact duplication of the key set can be avoided if one does this:

  Iterator<Map.Entry<…>> ie = headers.entrySet().iterator();
  while (ie.hashNext()) {
    Map.Entry<…> e = ie.next();
    if (e.getValue() != null) {
      e.setValue(Collections.unmodifiableList(e.getValue()));
    }
    else {
      ie.remove();
    }
  }

However, i suspect this could be simplified to:

  headers.replaceAll((k, v) -> Collections.unmodifiableList(v)))

If there are never any explicit null values placed in the map, which should be the case as that is really an anti-pattern.

Also this:

    private List<String> getOrCreate(String name) {
        List<String> l = headers.get(name);
        if (l == null) {
            l = new LinkedList<>();
            headers.put(name, l);
        }
        return l;
    }

can be replaced with this:

  return headers.computeIfAbsent(name, k -> new LinkedList<>());

Paul.

> On 4 Mar 2016, at 14:19, vaibhav x.choudhary <vaibhav.x.choudhary at oracle.com> wrote:
> 
> Hi,
> 
> Please review :-
> 
> Review Link :- http://cr.openjdk.java.net/~ntv/vaibhav/JDK8151182/webrev.00/
> Bug ID: https://bugs.openjdk.java.net/browse/JDK-8151182
> 
> --
> Thank You,
> Vaibhav Choudhary
> http://blogs.oracle.com/vaibhav
> 




More information about the core-libs-dev mailing list