jdk/src/java.httpclient/share/classes/java/net/http/HttpHeadersImpl.java
changeset 42483 3850c235c3fb
parent 42482 15297dde0d55
parent 42479 a80dbf731cbe
child 42489 a9e4de33da2e
equal deleted inserted replaced
42482:15297dde0d55 42483:3850c235c3fb
     1 /*
       
     2  * Copyright (c) 2015, 2016, 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 java.net.http;
       
    27 
       
    28 import java.util.Collections;
       
    29 import java.util.LinkedList;
       
    30 import java.util.List;
       
    31 import java.util.Map;
       
    32 import java.util.Optional;
       
    33 import java.util.Set;
       
    34 import java.util.TreeMap;
       
    35 
       
    36 /**
       
    37  * Implementation of HttpHeaders.
       
    38  */
       
    39 class HttpHeadersImpl implements HttpHeaders {
       
    40 
       
    41     private final TreeMap<String,List<String>> headers;
       
    42 
       
    43     public HttpHeadersImpl() {
       
    44         headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
       
    45     }
       
    46 
       
    47     @Override
       
    48     public Optional<String> firstValue(String name) {
       
    49         List<String> l = headers.get(name);
       
    50         return Optional.ofNullable(l == null ? null : l.get(0));
       
    51     }
       
    52 
       
    53     @Override
       
    54     public List<String> allValues(String name) {
       
    55         return headers.get(name);
       
    56     }
       
    57 
       
    58     @Override
       
    59     public Map<String, List<String>> map() {
       
    60         return Collections.unmodifiableMap(headers);
       
    61     }
       
    62 
       
    63     Map<String, List<String>> directMap() {
       
    64         return headers;
       
    65     }
       
    66 
       
    67     // package private mutators
       
    68 
       
    69     public HttpHeadersImpl deepCopy() {
       
    70         HttpHeadersImpl h1 = new HttpHeadersImpl();
       
    71         TreeMap<String,List<String>> headers1 = h1.headers;
       
    72         Set<String> keys = headers.keySet();
       
    73         for (String key : keys) {
       
    74             List<String> vals = headers.get(key);
       
    75             LinkedList<String> vals1 = new LinkedList<>(vals);
       
    76             headers1.put(key, vals1);
       
    77         }
       
    78         return h1;
       
    79     }
       
    80 
       
    81     void addHeader(String name, String value) {
       
    82         headers.computeIfAbsent(name, k -> new LinkedList<>())
       
    83                .add(value);
       
    84     }
       
    85 
       
    86     void setHeader(String name, String value) {
       
    87         List<String> l = headers.computeIfAbsent(name, k -> new LinkedList<>());
       
    88         l.clear();
       
    89         l.add(value);
       
    90     }
       
    91 
       
    92     @Override
       
    93     public Optional<Long> firstValueAsLong(String name) {
       
    94         List<String> l = headers.get(name);
       
    95         if (l == null) {
       
    96             return Optional.empty();
       
    97         } else {
       
    98             String v = l.get(0);
       
    99             Long lv = Long.parseLong(v);
       
   100             return Optional.of(lv);
       
   101         }
       
   102     }
       
   103 
       
   104     void clear() {
       
   105         headers.clear();
       
   106     }
       
   107 }