View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3.concurrent;
19  
20  import java.util.Collection;
21  import java.util.concurrent.ExecutionException;
22  import java.util.concurrent.Future;
23  import java.util.concurrent.TimeUnit;
24  import java.util.concurrent.TimeoutException;
25  import java.util.stream.Collectors;
26  import java.util.stream.Stream;
27  
28  import org.apache.commons.lang3.exception.UncheckedInterruptedException;
29  
30  /**
31   * An {@link Future} that throws unchecked instead checked exceptions.
32   *
33   * @param <V> The result type returned by this Future's {@link #get()} and {@link #get(long, TimeUnit)} methods.
34   * @see Future
35   * @see Exception
36   * @since 3.13.0
37   */
38  public interface UncheckedFuture<V> extends Future<V> {
39  
40      /**
41       * Maps the given instances as unchecked.
42       *
43       * @param <T> The result type returned by the Futures' {@link #get()} and {@link #get(long, TimeUnit)} methods.
44       *
45       * @param futures The Futures to uncheck.
46       * @return a new stream.
47       */
48      static <T> Stream<UncheckedFuture<T>> map(final Collection<Future<T>> futures) {
49          return futures.stream().map(UncheckedFuture::on);
50      }
51  
52      /**
53       * Maps the given instances as unchecked.
54       *
55       * @param <T> The result type returned by the Futures' {@link #get()} and {@link #get(long, TimeUnit)} methods.
56       *
57       * @param futures The Futures to uncheck.
58       * @return a new collection.
59       */
60      static <T> Collection<UncheckedFuture<T>> on(final Collection<Future<T>> futures) {
61          return map(futures).collect(Collectors.toList());
62      }
63  
64      /**
65       * Creates a new instance on the given Future.
66       *
67       * @param <T> The result type returned by this Future's {@link #get()} and {@link #get(long, TimeUnit)} methods.
68       *
69       * @param future The Future to uncheck.
70       * @return a new instance.
71       */
72      static <T> UncheckedFuture<T> on(final Future<T> future) {
73          return new UncheckedFutureImpl<>(future);
74      }
75  
76      /**
77       * Gets per {@link Future#get()} but rethrows checked exceptions as unchecked.
78       * <p>
79       * The default mapping from checked to unchecked is:
80       * </p>
81       * <ul>
82       * <li>{@link InterruptedException} \u2192 {@link UncheckedInterruptedException}</li>
83       * <li>{@link ExecutionException} \u2192 {@link UncheckedExecutionException}</li>
84       * </ul>
85       */
86      @Override
87      V get();
88  
89      /**
90       * Gets per {@link Future#get(long, TimeUnit)} but rethrows checked exceptions as unchecked.
91       * <p>
92       * The default mapping from checked to unchecked is:
93       * </p>
94       * <ul>
95       * <li>{@link InterruptedException} \u2192 {@link UncheckedInterruptedException}</li>
96       * <li>{@link ExecutionException} \u2192 {@link UncheckedExecutionException}</li>
97       * <li>{@link TimeoutException} \u2192 {@link UncheckedTimeoutException}</li>
98       * </ul>
99       */
100     @Override
101     V get(long timeout, TimeUnit unit);
102 
103 }