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 = new Cancellable() {
45  
46          @Override
47          public boolean cancel() {
48              return false;
49          }
50  
51      };
52  
53      /**
54       * This class represents a {@link Future} in the completed state with a fixed result.
55       * The outcome of the future cannot be altered and it cannot be cancelled.
56       *
57       * @param <T> operation result representation.
58       */
59      public static class CompletedFuture<T> implements Future<T> {
60  
61          private final T result;
62  
63          public CompletedFuture(final T result) {
64              this.result = result;
65          }
66  
67          @Override
68          public T get() throws InterruptedException, ExecutionException {
69              return result;
70          }
71  
72          @Override
73          public T get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
74              return result;
75          }
76          @Override
77          public boolean cancel(final boolean mayInterruptIfRunning) {
78              return false;
79          }
80  
81          @Override
82          public boolean isCancelled() {
83              return false;
84          }
85  
86          @Override
87          public boolean isDone() {
88              return true;
89          }
90  
91      }
92  
93      /**
94       * Creates a {@link Cancellable} operation handle for an ongoing process
95       * or operation that cannot be cancelled. Attempts to cancel the operation
96       * with this handle will have no effect.
97       *
98       * @return the no-op cancellable operation handle.
99       */
100     public static Cancellable nonCancellable() {
101         return NOOP_CANCELLABLE;
102     }
103 
104     /**
105      * Creates a {@link Cancellable} operation handle for an ongoing process
106      * or operation represented by a {@link Future}.
107      *
108      * @param future the result future
109      * @return the cancellable operation handle.
110      */
111     public static Cancellable cancellable(final Future<?> future) {
112         if (future == null) {
113             return NOOP_CANCELLABLE;
114         }
115         if (future instanceof Cancellable) {
116             return (Cancellable) future;
117         }
118         return new Cancellable() {
119 
120             @Override
121             public boolean cancel() {
122                 return future.cancel(true);
123             }
124 
125         };
126     }
127 
128 }