1 /* 2 * Copyright (c) 2016, 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.util.concurrent.CompletableFuture; 29 import java.util.concurrent.CompletionStage; 30 import java.util.concurrent.atomic.AtomicLong; 31 32 import static java.util.Objects.requireNonNull; 33 34 /* 35 * A CompletableFuture which does not allow any obtrusion logic. 36 * All methods of CompletionStage return instances of this class. 37 */ 38 public final class MinimalFuture<T> extends CompletableFuture<T> { 39 40 @FunctionalInterface 41 public interface ExceptionalSupplier<U> { get()42 U get() throws Throwable; 43 } 44 45 private final static AtomicLong TOKENS = new AtomicLong(); 46 private final long id; 47 completedFuture(U value)48 public static <U> MinimalFuture<U> completedFuture(U value) { 49 MinimalFuture<U> f = new MinimalFuture<>(); 50 f.complete(value); 51 return f; 52 } 53 failedFuture(Throwable ex)54 public static <U> CompletableFuture<U> failedFuture(Throwable ex) { 55 requireNonNull(ex); 56 MinimalFuture<U> f = new MinimalFuture<>(); 57 f.completeExceptionally(ex); 58 return f; 59 } 60 supply(ExceptionalSupplier<U> supplier)61 public static <U> CompletableFuture<U> supply(ExceptionalSupplier<U> supplier) { 62 CompletableFuture<U> cf = new MinimalFuture<>(); 63 try { 64 U value = supplier.get(); 65 cf.complete(value); 66 } catch (Throwable t) { 67 cf.completeExceptionally(t); 68 } 69 return cf; 70 } 71 MinimalFuture()72 public MinimalFuture() { 73 super(); 74 this.id = TOKENS.incrementAndGet(); 75 } 76 77 @Override newIncompleteFuture()78 public <U> MinimalFuture<U> newIncompleteFuture() { 79 return new MinimalFuture<>(); 80 } 81 82 @Override obtrudeValue(T value)83 public void obtrudeValue(T value) { 84 throw new UnsupportedOperationException(); 85 } 86 87 @Override obtrudeException(Throwable ex)88 public void obtrudeException(Throwable ex) { 89 throw new UnsupportedOperationException(); 90 } 91 92 @Override toString()93 public String toString() { 94 return super.toString() + " (id=" + id +")"; 95 } 96 of(CompletionStage<U> stage)97 public static <U> MinimalFuture<U> of(CompletionStage<U> stage) { 98 MinimalFuture<U> cf = new MinimalFuture<>(); 99 stage.whenComplete((r,t) -> complete(cf, r, t)); 100 return cf; 101 } 102 complete(CompletableFuture<U> cf, U result, Throwable t)103 private static <U> void complete(CompletableFuture<U> cf, U result, Throwable t) { 104 if (t == null) { 105 cf.complete(result); 106 } else { 107 cf.completeExceptionally(t); 108 } 109 } 110 } 111