File tree Expand file tree Collapse file tree
microsphere-java-core/src/main/java/io/microsphere/lang/function Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package io .microsphere .lang .function ;
22
33import java .util .function .Function ;
4+ import java .util .function .Supplier ;
45
56import static io .microsphere .util .Assert .assertNotNull ;
67
8+ /**
9+ * A functional interface similar to {@link Supplier}, but allows the {@code get()} method to throw a
10+ * {@link Throwable}. This is useful for functional constructs where operations may throw checked exceptions that need
11+ * to be handled or rethrown.
12+ *
13+ * <p>Example usage:</p>
14+ * <pre>{@code
15+ * // Using ThrowableSupplier to read a file content
16+ * ThrowableSupplier<String> fileReader = () -> {
17+ * Path path = Paths.get("example.txt");
18+ * return Files.readString(path);
19+ * };
20+ *
21+ * // Execute with default exception handling (converts to RuntimeException)
22+ * String content = fileReader.execute();
23+ *
24+ * // Execute with custom exception handling
25+ * String contentWithHandler = fileReader.execute(ex -> {
26+ * System.err.println("Error reading file: " + ex.getMessage());
27+ * return "default content";
28+ * });
29+ * }</pre>
30+ *
31+ * <p>This interface provides convenience methods to execute the supplier and handle exceptions using a custom handler,
32+ * making it easier to work with functional patterns in environments where exceptions must be managed.</p>
33+ *
34+ * @param <T> the type of result supplied by this supplier
35+ * @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
36+ * @see Supplier
37+ * @see Throwable
38+ * @since 1.0.0
39+ */
740@ FunctionalInterface
841public interface ThrowableSupplier <T > {
942
You can’t perform that action at this time.
0 commit comments