View Javadoc
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      public static class CompletedFuture<T> implements Future<T> {
53  
54          private final T result;
55  
56          public CompletedFuture(final T result) {
57              this.result = result;
58          }
59  
60          @Override
61          public T get() throws InterruptedException, ExecutionException {
62              return result;
63          }
64  
65          @Override
66          public T get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
67              return result;
68          }
69          @Override
70          public boolean cancel(final boolean mayInterruptIfRunning) {
71              return false;
72          }
73  
74          @Override
75          public boolean isCancelled() {
76              return false;
77          }
78  
79          @Override
80          public boolean isDone() {
81              return true;
82          }
83  
84      }
85  
86      /**
87       * Creates a {@link Cancellable} operation handle for an ongoing process
88       * or operation that cannot be cancelled. Attempts to cancel the operation
89       * with this handle will have no effect.
90       *
91       * @return the no-op cancellable operation handle.
92       */
93      public static Cancellable nonCancellable() {
94          return NOOP_CANCELLABLE;
95      }
96  
97      /**
98       * Creates a {@link Cancellable} operation handle for an ongoing process
99       * or operation represented by a {@link Future}.
100      *
101      * @param future the result future
102      * @return the cancellable operation handle.
103      */
104     public static Cancellable cancellable(final Future<?> future) {
105         if (future == null) {
106             return NOOP_CANCELLABLE;
107         }
108         if (future instanceof Cancellable) {
109             return (Cancellable) future;
110         }
111         return () -> future.cancel(true);
112     }
113 
114 }