1 /* 2 * Copyright (c) 2016, 2017, 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.Collection; 29 import java.util.Map; 30 import java.util.Set; 31 import java.util.concurrent.CompletableFuture; 32 33 /** 34 * A {@link java.util.Map} containing the result of a HTTP/2 request and multi-response. 35 * {@Incubating} 36 * <p> 37 * This is one possible implementation of the aggregate result type {@code <U>} returned 38 * from {@link HttpClient#sendAsync(HttpRequest,HttpResponse.MultiSubscriber) }. 39 * The map is indexed by {@link HttpRequest} and each value is a 40 * {@link java.util.concurrent.CompletableFuture}< 41 * {@link HttpResponse}{@code <V>}> 42 * <p> 43 * A {@code MultiMapResult} is obtained from an invocation such as the one shown below: 44 * <p> 45 * {@link CompletableFuture}<{@code MultiMapResult<V>}> 46 * {@link HttpClient#sendAsync(HttpRequest, 47 * HttpResponse.MultiSubscriber) HttpClient.sendAsync(}{@link 48 * HttpResponse.MultiSubscriber#asMap(java.util.function.Function) 49 * MultiSubscriber.asMap(Function)}) 50 * 51 * @param <V> the response body type for all responses 52 */ 53 public class MultiMapResult<V> implements Map<HttpRequest,CompletableFuture<HttpResponse<V>>> { 54 private final Map<HttpRequest,CompletableFuture<HttpResponse<V>>> map; 55 56 MultiMapResult(Map<HttpRequest,CompletableFuture<HttpResponse<V>>> map) { 57 this.map = map; 58 } 59 60 @Override 61 public int size() { 62 return map.size(); 63 } 64 65 @Override 66 public boolean isEmpty() { 67 return map.isEmpty(); 68 } 69 70 @Override 71 public boolean containsKey(Object key) { 72 return map.containsKey(key); 73 } 74 75 @Override 76 public boolean containsValue(Object value) { 77 return map.containsValue(value); 78 } 79 80 @Override 81 public CompletableFuture<HttpResponse<V>> get(Object key) { 82 return map.get(key); 83 } 84 85 @Override 86 public CompletableFuture<HttpResponse<V>> put(HttpRequest key, CompletableFuture<HttpResponse<V>> value) { 87 return map.put(key, value); 88 } 89 90 @Override 91 public CompletableFuture<HttpResponse<V>> remove(Object key) { 92 return map.remove(key); 93 } 94 95 @Override 96 public void putAll(Map<? extends HttpRequest, ? extends CompletableFuture<HttpResponse<V>>> m) { 97 map.putAll(m); 98 } 99 100 @Override 101 public void clear() { 102 map.clear(); 103 } 104 105 @Override 106 public Set<HttpRequest> keySet() { 107 return map.keySet(); 108 } 109 110 @Override 111 public Collection<CompletableFuture<HttpResponse<V>>> values() { 112 return map.values(); 113 } 114 115 @Override 116 public Set<Entry<HttpRequest, CompletableFuture<HttpResponse<V>>>> entrySet() { 117 return map.entrySet(); 118 } 119 }