src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/ImmutableHeaders.java
branchhttp-client-branch
changeset 56054 352e845ae744
parent 56041 b4b5e09ef3cc
equal deleted inserted replaced
56053:8588095e95b0 56054:352e845ae744
    27 
    27 
    28 import java.util.ArrayList;
    28 import java.util.ArrayList;
    29 import java.util.List;
    29 import java.util.List;
    30 import java.util.Map;
    30 import java.util.Map;
    31 import java.util.TreeMap;
    31 import java.util.TreeMap;
       
    32 import java.util.function.BiPredicate;
    32 import java.util.function.Predicate;
    33 import java.util.function.Predicate;
    33 import static java.util.Collections.emptyMap;
    34 import static java.util.Collections.emptyMap;
    34 import static java.util.Collections.unmodifiableList;
    35 import static java.util.Collections.unmodifiableList;
    35 import static java.util.Collections.unmodifiableMap;
    36 import static java.util.Collections.unmodifiableMap;
    36 import static java.util.Objects.requireNonNull;
    37 import static java.util.Objects.requireNonNull;
    55 
    56 
    56     public static ImmutableHeaders of(Map<String, List<String>> src,
    57     public static ImmutableHeaders of(Map<String, List<String>> src,
    57                                       Predicate<? super String> keyAllowed) {
    58                                       Predicate<? super String> keyAllowed) {
    58         requireNonNull(src, "src");
    59         requireNonNull(src, "src");
    59         requireNonNull(keyAllowed, "keyAllowed");
    60         requireNonNull(keyAllowed, "keyAllowed");
    60         return new ImmutableHeaders(src, keyAllowed);
    61         return new ImmutableHeaders(src, headerAllowed(keyAllowed));
       
    62     }
       
    63 
       
    64     public static ImmutableHeaders of(Map<String, List<String>> src,
       
    65                                       BiPredicate<? super String, ? super List<String>> headerAllowed) {
       
    66         requireNonNull(src, "src");
       
    67         requireNonNull(headerAllowed, "headerAllowed");
       
    68         return new ImmutableHeaders(src, headerAllowed);
    61     }
    69     }
    62 
    70 
    63     private ImmutableHeaders(Map<String, List<String>> src,
    71     private ImmutableHeaders(Map<String, List<String>> src,
    64                              Predicate<? super String> keyAllowed) {
    72                              BiPredicate<? super String, ? super List<String>> headerAllowed) {
    65         Map<String, List<String>> m = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    73         Map<String, List<String>> m = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    66         src.entrySet().stream()
    74         src.entrySet().stream()
    67                 .filter(e -> keyAllowed.test(e.getKey()))
    75                 .filter(e -> headerAllowed.test(e.getKey(), e.getValue()))
    68                 .forEach(e ->
    76                 .forEach(e ->
    69                         {
    77                         {
    70                             List<String> values = new ArrayList<>(e.getValue());
    78                             List<String> values = new ArrayList<>(e.getValue());
    71                             m.put(e.getKey(), unmodifiableList(values));
    79                             m.put(e.getKey(), unmodifiableList(values));
    72                         }
    80                         }
    73                 );
    81                 );
    74         this.map = unmodifiableMap(m);
    82         this.map = unmodifiableMap(m);
    75     }
    83     }
    76 
    84 
       
    85     private static BiPredicate<String, List<String>> headerAllowed(Predicate<? super String> keyAllowed) {
       
    86         return (n,v) -> keyAllowed.test(n);
       
    87     }
       
    88 
    77     @Override
    89     @Override
    78     public Map<String, List<String>> map() {
    90     public Map<String, List<String>> map() {
    79         return map;
    91         return map;
    80     }
    92     }
    81 }
    93 }