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 * https://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 * @param futures The Futures to uncheck. 45 * @return a new stream. 46 */ 47 static <T> Stream<UncheckedFuture<T>> map(final Collection<Future<T>> futures) { 48 return futures.stream().map(UncheckedFuture::on); 49 } 50 51 /** 52 * Maps the given instances as unchecked. 53 * 54 * @param <T> The result type returned by the Futures' {@link #get()} and {@link #get(long, TimeUnit)} methods. 55 * @param futures The Futures to uncheck. 56 * @return a new collection. 57 */ 58 static <T> Collection<UncheckedFuture<T>> on(final Collection<Future<T>> futures) { 59 return map(futures).collect(Collectors.toList()); 60 } 61 62 /** 63 * Creates a new instance on the given Future. 64 * 65 * @param <T> The result type returned by this Future's {@link #get()} and {@link #get(long, TimeUnit)} methods. 66 * @param future The Future to uncheck. 67 * @return a new instance. 68 */ 69 static <T> UncheckedFuture<T> on(final Future<T> future) { 70 return new UncheckedFutureImpl<>(future); 71 } 72 73 /** 74 * Gets per {@link Future#get()} but rethrows checked exceptions as unchecked. 75 * <p> 76 * The default mapping from checked to unchecked is: 77 * </p> 78 * <ul> 79 * <li>{@link InterruptedException} \u2192 {@link UncheckedInterruptedException}</li> 80 * <li>{@link ExecutionException} \u2192 {@link UncheckedExecutionException}</li> 81 * </ul> 82 */ 83 @Override 84 V get(); 85 86 /** 87 * Gets per {@link Future#get(long, TimeUnit)} but rethrows checked exceptions as unchecked. 88 * <p> 89 * The default mapping from checked to unchecked is: 90 * </p> 91 * <ul> 92 * <li>{@link InterruptedException} \u2192 {@link UncheckedInterruptedException}</li> 93 * <li>{@link ExecutionException} \u2192 {@link UncheckedExecutionException}</li> 94 * <li>{@link TimeoutException} \u2192 {@link UncheckedTimeoutException}</li> 95 * </ul> 96 */ 97 @Override 98 V get(long timeout, TimeUnit unit); 99 100 }