|
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 jdk.incubator.http; |
|
27 |
|
28 import java.util.ArrayList; |
|
29 import java.util.List; |
|
30 import java.util.Map; |
|
31 import java.util.Optional; |
|
32 import java.util.OptionalLong; |
|
33 import java.util.TreeMap; |
|
34 import java.util.function.Predicate; |
|
35 import static java.util.Collections.emptyList; |
|
36 import static java.util.Collections.emptyMap; |
|
37 import static java.util.Collections.unmodifiableList; |
|
38 import static java.util.Collections.unmodifiableMap; |
|
39 import static java.util.Objects.requireNonNull; |
|
40 |
|
41 final class ImmutableHeaders implements HttpHeaders { |
|
42 |
|
43 private final Map<String, List<String>> map; |
|
44 |
|
45 public static ImmutableHeaders empty() { |
|
46 return of(emptyMap()); |
|
47 } |
|
48 |
|
49 public static ImmutableHeaders of(Map<String, List<String>> src) { |
|
50 return of(src, x -> true); |
|
51 } |
|
52 |
|
53 public static ImmutableHeaders of(Map<String, List<String>> src, |
|
54 Predicate<? super String> keyAllowed) { |
|
55 requireNonNull(src, "src"); |
|
56 requireNonNull(keyAllowed, "keyAllowed"); |
|
57 return new ImmutableHeaders(src, keyAllowed); |
|
58 } |
|
59 |
|
60 private ImmutableHeaders(Map<String, List<String>> src, |
|
61 Predicate<? super String> keyAllowed) { |
|
62 Map<String, List<String>> m = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); |
|
63 src.entrySet().stream() |
|
64 .filter(e -> keyAllowed.test(e.getKey())) |
|
65 .forEach(e -> |
|
66 { |
|
67 List<String> values = new ArrayList<>(e.getValue()); |
|
68 m.put(e.getKey(), unmodifiableList(values)); |
|
69 } |
|
70 ); |
|
71 this.map = unmodifiableMap(m); |
|
72 } |
|
73 |
|
74 @Override |
|
75 public Optional<String> firstValue(String name) { |
|
76 return allValues(name).stream().findFirst(); |
|
77 } |
|
78 |
|
79 @Override |
|
80 public OptionalLong firstValueAsLong(String name) { |
|
81 return allValues(name).stream().mapToLong(Long::valueOf).findFirst(); |
|
82 } |
|
83 |
|
84 @Override |
|
85 public List<String> allValues(String name) { |
|
86 requireNonNull(name); |
|
87 List<String> values = map.get(name); |
|
88 // Making unmodifiable list out of empty in order to make a list which |
|
89 // throws UOE unconditionally |
|
90 return values != null ? values : unmodifiableList(emptyList()); |
|
91 } |
|
92 |
|
93 @Override |
|
94 public Map<String, List<String>> map() { |
|
95 return map; |
|
96 } |
|
97 } |