Skip to content

Commit c8351dd

Browse files
committed
Update ThrowableSupplier.java
1 parent 5b36b9d commit c8351dd

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

microsphere-java-core/src/main/java/io/microsphere/lang/function/ThrowableSupplier.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,42 @@
11
package io.microsphere.lang.function;
22

33
import java.util.function.Function;
4+
import java.util.function.Supplier;
45

56
import 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
841
public interface ThrowableSupplier<T> {
942

0 commit comments

Comments
 (0)