Source code
Description: source: package: org.apache.commons.lang3.concurrent, interface: UncheckedFuture

    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 *      http://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     *
    045     * @param futures The Futures to uncheck.
    046     * @return a new stream.
    047     */
    048    static <T> Stream<UncheckedFuture<T>> map(final Collection<Future<T>> futures) {
    049        return futures.stream().map(UncheckedFuture::on);
    050    }
    051
    052    /**
    053     * Maps the given instances as unchecked.
    054     *
    055     * @param <T> The result type returned by the Futures' {@link #get()} and {@link #get(long, TimeUnit)} methods.
    056     *
    057     * @param futures The Futures to uncheck.
    058     * @return a new collection.
    059     */
    060    static <T> Collection<UncheckedFuture<T>> on(final Collection<Future<T>> futures) {
    061        return map(futures).collect(Collectors.toList());
    062    }
    063
    064    /**
    065     * Creates a new instance on the given Future.
    066     *
    067     * @param <T> The result type returned by this Future's {@link #get()} and {@link #get(long, TimeUnit)} methods.
    068     *
    069     * @param future The Future to uncheck.
    070     * @return a new instance.
    071     */
    072    static <T> UncheckedFuture<T> on(final Future<T> future) {
    073        return new UncheckedFutureImpl<>(future);
    074    }
    075
    076    /**
    077     * Gets per {@link Future#get()} but rethrows checked exceptions as unchecked.
    078     * <p>
    079     * The default mapping from checked to unchecked is:
    080     * </p>
    081     * <ul>
    082     * <li>{@link InterruptedException} \u2192 {@link UncheckedInterruptedException}</li>
    083     * <li>{@link ExecutionException} \u2192 {@link UncheckedExecutionException}</li>
    084     * </ul>
    085     */
    086    @Override
    087    V get();
    088
    089    /**
    090     * Gets per {@link Future#get(long, TimeUnit)} but rethrows checked exceptions as unchecked.
    091     * <p>
    092     * The default mapping from checked to unchecked is:
    093     * </p>
    094     * <ul>
    095     * <li>{@link InterruptedException} \u2192 {@link UncheckedInterruptedException}</li>
    096     * <li>{@link ExecutionException} \u2192 {@link UncheckedExecutionException}</li>
    097     * <li>{@link TimeoutException} \u2192 {@link UncheckedTimeoutException}</li>
    098     * </ul>
    099     */
    100    @Override
    101    V get(long timeout, TimeUnit unit);
    102
    103}