UncheckedFuture.java

  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. package org.apache.commons.lang3.concurrent;

  18. import java.util.Collection;
  19. import java.util.concurrent.ExecutionException;
  20. import java.util.concurrent.Future;
  21. import java.util.concurrent.TimeUnit;
  22. import java.util.concurrent.TimeoutException;
  23. import java.util.stream.Collectors;
  24. import java.util.stream.Stream;

  25. import org.apache.commons.lang3.exception.UncheckedInterruptedException;

  26. /**
  27.  * An {@link Future} that throws unchecked instead checked exceptions.
  28.  *
  29.  * @param <V> The result type returned by this Future's {@link #get()} and {@link #get(long, TimeUnit)} methods.
  30.  * @see Future
  31.  * @see Exception
  32.  * @since 3.13.0
  33.  */
  34. public interface UncheckedFuture<V> extends Future<V> {

  35.     /**
  36.      * Maps the given instances as unchecked.
  37.      *
  38.      * @param <T> The result type returned by the Futures' {@link #get()} and {@link #get(long, TimeUnit)} methods.
  39.      *
  40.      * @param futures The Futures to uncheck.
  41.      * @return a new stream.
  42.      */
  43.     static <T> Stream<UncheckedFuture<T>> map(final Collection<Future<T>> futures) {
  44.         return futures.stream().map(UncheckedFuture::on);
  45.     }

  46.     /**
  47.      * Maps the given instances as unchecked.
  48.      *
  49.      * @param <T> The result type returned by the Futures' {@link #get()} and {@link #get(long, TimeUnit)} methods.
  50.      *
  51.      * @param futures The Futures to uncheck.
  52.      * @return a new collection.
  53.      */
  54.     static <T> Collection<UncheckedFuture<T>> on(final Collection<Future<T>> futures) {
  55.         return map(futures).collect(Collectors.toList());
  56.     }

  57.     /**
  58.      * Creates a new instance on the given Future.
  59.      *
  60.      * @param <T> The result type returned by this Future's {@link #get()} and {@link #get(long, TimeUnit)} methods.
  61.      *
  62.      * @param future The Future to uncheck.
  63.      * @return a new instance.
  64.      */
  65.     static <T> UncheckedFuture<T> on(final Future<T> future) {
  66.         return new UncheckedFutureImpl<>(future);
  67.     }

  68.     /**
  69.      * Gets per {@link Future#get()} but rethrows checked exceptions as unchecked.
  70.      * <p>
  71.      * The default mapping from checked to unchecked is:
  72.      * </p>
  73.      * <ul>
  74.      * <li>{@link InterruptedException} \u2192 {@link UncheckedInterruptedException}</li>
  75.      * <li>{@link ExecutionException} \u2192 {@link UncheckedExecutionException}</li>
  76.      * </ul>
  77.      */
  78.     @Override
  79.     V get();

  80.     /**
  81.      * Gets per {@link Future#get(long, TimeUnit)} but rethrows checked exceptions as unchecked.
  82.      * <p>
  83.      * The default mapping from checked to unchecked is:
  84.      * </p>
  85.      * <ul>
  86.      * <li>{@link InterruptedException} \u2192 {@link UncheckedInterruptedException}</li>
  87.      * <li>{@link ExecutionException} \u2192 {@link UncheckedExecutionException}</li>
  88.      * <li>{@link TimeoutException} \u2192 {@link UncheckedTimeoutException}</li>
  89.      * </ul>
  90.      */
  91.     @Override
  92.     V get(long timeout, TimeUnit unit);

  93. }