/
niceSOFT
/
openjdk21
Обзор
Документация
Войти
/
niceSOFT
/
openjdk21
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/java.net.http/share/classes/jdk/internal/net/http/common/MinimalFuture.java
131 строка
4 KB
Daniel Fuchs
8276401: Use blessed modifier order in java.net.http
03 ноя 2021, 19:23
03 ноя 2021, 19:23
7115892
Код
Авторство
О чём код?
/* * Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package jdk.internal.net.http.common; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; import java.util.concurrent.atomic.AtomicLong; import static java.util.Objects.requireNonNull; /* * A CompletableFuture which does not allow any obtrusion logic. * All methods of CompletionStage return instances of this class. */ public final class MinimalFuture<T> extends CompletableFuture<T> { @FunctionalInterface public interface ExceptionalSupplier<U> { U get() throws Throwable; } private static final AtomicLong TOKENS = new AtomicLong(); private final long id; private final Cancelable cancelable; public static <U> MinimalFuture<U> completedFuture(U value) { MinimalFuture<U> f = new MinimalFuture<>(); f.complete(value); return f; } public static <U> CompletableFuture<U> failedFuture(Throwable ex) { requireNonNull(ex); MinimalFuture<U> f = new MinimalFuture<>(); f.completeExceptionally(ex); return f; } public static <U> CompletableFuture<U> supply(ExceptionalSupplier<U> supplier) { CompletableFuture<U> cf = new MinimalFuture<>(); try { U value = supplier.get(); cf.complete(value); } catch (Throwable t) { cf.completeExceptionally(t); } return cf; } public MinimalFuture() { this(null); } public MinimalFuture(Cancelable cancelable) { super(); this.id = TOKENS.incrementAndGet(); this.cancelable = cancelable; } @Override public <U> MinimalFuture<U> newIncompleteFuture() { return new MinimalFuture<>(cancelable); } @Override public void obtrudeValue(T value) { throw new UnsupportedOperationException(); } @Override public void obtrudeException(Throwable ex) { throw new UnsupportedOperationException(); } @Override public String toString() { return super.toString() + " (id=" + id +")"; } @Override public boolean cancel(boolean mayInterruptIfRunning) { boolean result = false; if (cancelable != null && !isDone()) { result = cancelable.cancel(mayInterruptIfRunning); } return super.cancel(mayInterruptIfRunning) || result; } private Cancelable cancelable() { return cancelable; } public static <U> MinimalFuture<U> of(CompletionStage<U> stage) { Cancelable cancelable = stage instanceof MinimalFuture ? ((MinimalFuture)stage).cancelable() : null; MinimalFuture<U> cf = new MinimalFuture<>(cancelable); stage.whenComplete((r,t) -> complete(cf, r, t)); return cf; } private static <U> void complete(CompletableFuture<U> cf, U result, Throwable t) { if (t == null) { cf.complete(result); } else { cf.completeExceptionally(t); } } }