001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.lang3.concurrent; 019 020import java.util.Collection; 021import java.util.concurrent.ExecutionException; 022import java.util.concurrent.Future; 023import java.util.concurrent.TimeUnit; 024import java.util.concurrent.TimeoutException; 025import java.util.stream.Collectors; 026import java.util.stream.Stream; 027 028import org.apache.commons.lang3.exception.UncheckedInterruptedException; 029 030/** 031 * An {@link Future} that throws unchecked instead checked exceptions. 032 * 033 * @param <V> The result type returned by this Future's {@link #get()} and {@link #get(long, TimeUnit)} methods. 034 * @see Future 035 * @see Exception 036 * @since 3.13.0 037 */ 038public interface UncheckedFuture<V> extends Future<V> { 039 040 /** 041 * Maps the given instances as unchecked. 042 * 043 * @param <T> The result type returned by the Futures' {@link #get()} and {@link #get(long, TimeUnit)} methods. 044 * @param futures The Futures to uncheck. 045 * @return a new stream. 046 */ 047 static <T> Stream<UncheckedFuture<T>> map(final Collection<Future<T>> futures) { 048 return futures.stream().map(UncheckedFuture::on); 049 } 050 051 /** 052 * Maps the given instances as unchecked. 053 * 054 * @param <T> The result type returned by the Futures' {@link #get()} and {@link #get(long, TimeUnit)} methods. 055 * @param futures The Futures to uncheck. 056 * @return a new collection. 057 */ 058 static <T> Collection<UncheckedFuture<T>> on(final Collection<Future<T>> futures) { 059 return map(futures).collect(Collectors.toList()); 060 } 061 062 /** 063 * Creates a new instance on the given Future. 064 * 065 * @param <T> The result type returned by this Future's {@link #get()} and {@link #get(long, TimeUnit)} methods. 066 * @param future The Future to uncheck. 067 * @return a new instance. 068 */ 069 static <T> UncheckedFuture<T> on(final Future<T> future) { 070 return new UncheckedFutureImpl<>(future); 071 } 072 073 /** 074 * Gets per {@link Future#get()} but rethrows checked exceptions as unchecked. 075 * <p> 076 * The default mapping from checked to unchecked is: 077 * </p> 078 * <ul> 079 * <li>{@link InterruptedException} \u2192 {@link UncheckedInterruptedException}</li> 080 * <li>{@link ExecutionException} \u2192 {@link UncheckedExecutionException}</li> 081 * </ul> 082 */ 083 @Override 084 V get(); 085 086 /** 087 * Gets per {@link Future#get(long, TimeUnit)} but rethrows checked exceptions as unchecked. 088 * <p> 089 * The default mapping from checked to unchecked is: 090 * </p> 091 * <ul> 092 * <li>{@link InterruptedException} \u2192 {@link UncheckedInterruptedException}</li> 093 * <li>{@link ExecutionException} \u2192 {@link UncheckedExecutionException}</li> 094 * <li>{@link TimeoutException} \u2192 {@link UncheckedTimeoutException}</li> 095 * </ul> 096 */ 097 @Override 098 V get(long timeout, TimeUnit unit); 099 100}