src/java.net.http/share/classes/jdk/internal/net/http/common/HttpHeadersImpl.java
changeset 49765 ee6f7a61f3a5
parent 48083 b1c1b4ef4be2
child 56451 9585061fdb04
equal deleted inserted replaced
49707:f7fd051519ac 49765:ee6f7a61f3a5
       
     1 /*
       
     2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.internal.net.http.common;
       
    27 
       
    28 import java.net.http.HttpHeaders;
       
    29 import java.util.ArrayList;
       
    30 import java.util.Collections;
       
    31 import java.util.List;
       
    32 import java.util.Map;
       
    33 import java.util.TreeMap;
       
    34 
       
    35 /**
       
    36  * Implementation of HttpHeaders.
       
    37  *
       
    38  * The public HttpHeaders API provides a read-only view, while the
       
    39  * non-HttpHeaders members allow for implementation specific mutation, e.g.
       
    40  * during creation, etc.
       
    41  */
       
    42 public class HttpHeadersImpl extends HttpHeaders {
       
    43 
       
    44     private final TreeMap<String, List<String>> headers;
       
    45 
       
    46     public HttpHeadersImpl() {
       
    47         headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
       
    48     }
       
    49 
       
    50     @Override
       
    51     public Map<String, List<String>> map() {
       
    52         return Collections.unmodifiableMap(headersMap());
       
    53     }
       
    54 
       
    55     // non-HttpHeaders private mutators
       
    56 
       
    57     public HttpHeadersImpl deepCopy() {
       
    58         HttpHeadersImpl h1 = newDeepCopy();
       
    59         for (Map.Entry<String, List<String>> entry : headersMap().entrySet()) {
       
    60             List<String> valuesCopy = new ArrayList<>(entry.getValue());
       
    61             h1.headersMap().put(entry.getKey(), valuesCopy);
       
    62         }
       
    63         return h1;
       
    64     }
       
    65 
       
    66     public void addHeader(String name, String value) {
       
    67         headersMap().computeIfAbsent(name, k -> new ArrayList<>(1))
       
    68                     .add(value);
       
    69     }
       
    70 
       
    71     public void setHeader(String name, String value) {
       
    72         // headers typically have one value
       
    73         List<String> values = new ArrayList<>(1);
       
    74         values.add(value);
       
    75         headersMap().put(name, values);
       
    76     }
       
    77 
       
    78     public void clear() {
       
    79         headersMap().clear();
       
    80     }
       
    81 
       
    82     protected HttpHeadersImpl newDeepCopy() {
       
    83         return new HttpHeadersImpl();
       
    84     }
       
    85 
       
    86     protected Map<String, List<String>> headersMap() {
       
    87         return headers;
       
    88     }
       
    89 }