1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 28 package org.apache.hc.client5.http.impl; 29 30 import java.util.concurrent.ExecutionException; 31 import java.util.concurrent.Future; 32 import java.util.concurrent.TimeUnit; 33 import java.util.concurrent.TimeoutException; 34 35 import org.apache.hc.core5.concurrent.Cancellable; 36 37 /** 38 * Common cancellable operations. 39 * 40 * @since 5.0 41 */ 42 public final class Operations { 43 44 private final static Cancellable NOOP_CANCELLABLE = () -> false; 45 46 /** 47 * This class represents a {@link Future} in the completed state with a fixed result. 48 * The outcome of the future cannot be altered and it cannot be cancelled. 49 * 50 * @param <T> operation result representation. 51 * 52 * @deprecated Use {@link org.apache.hc.core5.concurrent.CompletedFuture}. 53 */ 54 @Deprecated 55 public static class CompletedFuture<T> implements Future<T> { 56 57 private final T result; 58 59 public CompletedFuture(final T result) { 60 this.result = result; 61 } 62 63 @Override 64 public T get() throws InterruptedException, ExecutionException { 65 return result; 66 } 67 68 @Override 69 public T get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { 70 return result; 71 } 72 @Override 73 public boolean cancel(final boolean mayInterruptIfRunning) { 74 return false; 75 } 76 77 @Override 78 public boolean isCancelled() { 79 return false; 80 } 81 82 @Override 83 public boolean isDone() { 84 return true; 85 } 86 87 } 88 89 /** 90 * Creates a {@link Cancellable} operation handle for an ongoing process 91 * or operation that cannot be cancelled. Attempts to cancel the operation 92 * with this handle will have no effect. 93 * 94 * @return the no-op cancellable operation handle. 95 */ 96 public static Cancellable nonCancellable() { 97 return NOOP_CANCELLABLE; 98 } 99 100 /** 101 * Creates a {@link Cancellable} operation handle for an ongoing process 102 * or operation represented by a {@link Future}. 103 * 104 * @param future the result future 105 * @return the cancellable operation handle. 106 */ 107 public static Cancellable cancellable(final Future<?> future) { 108 if (future == null) { 109 return NOOP_CANCELLABLE; 110 } 111 if (future instanceof Cancellable) { 112 return (Cancellable) future; 113 } 114 return () -> future.cancel(true); 115 } 116 117 }