junit-jupiter-api/src/main/java/org/junit/jupiter/api/function/ThrowingSupplier.java
raw symbols SCIP| 1 | /* |
| 2 | * Copyright 2015-2024 the original author or authors. |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | * accompanies this distribution and is available at |
| 7 | * |
| 8 | * https://www.eclipse.org/legal/epl-v20.html |
| 9 | */ |
| 10 | |
| 11 | package org.junit.jupiter.api.function; |
| 12 | |
| 13 | import static org.apiguardian.api.API.Status.STABLE; |
| 14 | |
| 15 | import org.apiguardian.api.API; |
| 16 | |
| 17 | /** |
| 18 | * {@code ThrowingSupplier} is a functional interface that can be used to |
| 19 | * implement any generic block of code that returns an object and |
| 20 | * potentially throws a {@link Throwable}. |
| 21 | * |
| 22 | * <p>The {@code ThrowingSupplier} interface is similar to |
| 23 | * {@link java.util.function.Supplier}, except that a {@code ThrowingSupplier} |
| 24 | * can throw any kind of exception, including checked exceptions. |
| 25 | * |
| 26 | * <h2>Rationale for throwing {@code Throwable} instead of {@code Exception}</h2> |
| 27 | * |
| 28 | * <p>Although Java applications typically throw exceptions that are instances |
| 29 | * of {@link Exception}, {@link RuntimeException}, |
| 30 | * {@link Error}, or {@link AssertionError} (in testing |
| 31 | * scenarios), there may be use cases where a {@code ThrowingSupplier} needs to |
| 32 | * explicitly throw a {@code Throwable}. In order to support such specialized |
| 33 | * use cases, {@link #get} is declared to throw {@code Throwable}. |
| 34 | * |
| 35 | * @param <T> the type of argument supplied |
| 36 | * @since 5.0 |
| 37 | * @see java.util.function.Supplier |
| 38 | * @see org.junit.jupiter.api.Assertions#assertTimeout(java.time.Duration, ThrowingSupplier) |
| 39 | * @see org.junit.jupiter.api.Assertions#assertTimeoutPreemptively(java.time.Duration, ThrowingSupplier) |
| 40 | * @see Executable |
| 41 | * @see ThrowingConsumer |
| 42 | */ |
| 43 | @FunctionalInterface |
| 44 | @API(status = STABLE, since = "5.0") |
| 45 | public interface ThrowingSupplier<T> { |
| 46 | |
| 47 | /** |
| 48 | * Get a result, potentially throwing an exception. |
| 49 | * |
| 50 | * @return a result |
| 51 | */ |
| 52 | T get() throws Throwable; |
| 53 | |
| 54 | } |