-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing ThrowingSupplier to deal with checked exception on Supplier
- Loading branch information
1 parent
c454469
commit f797c78
Showing
5 changed files
with
85 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.soumakis.safetype; | ||
|
||
import java.util.function.BiFunction; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* A {@link Supplier} that allows invocation of code that throws a checked exception. | ||
* | ||
* @param <T> the type of results supplied by this supplier | ||
*/ | ||
public interface ThrowingSupplier<T> extends Supplier<T> { | ||
|
||
/** | ||
* Gets a result, possibly throwing a checked exception. | ||
* | ||
* @return a result | ||
* @throws Exception on error | ||
*/ | ||
T getWithException() throws Exception; | ||
|
||
/** | ||
* Default {@link Supplier#get()} that wraps any thrown checked exceptions (by default in a | ||
* {@link RuntimeException}). | ||
* | ||
* @see java.util.function.Supplier#get() | ||
*/ | ||
@Override | ||
default T get() { | ||
return get(RuntimeException::new); | ||
} | ||
|
||
/** | ||
* Gets a result, wrapping any thrown checked exceptions using the given | ||
* {@code exceptionWrapper}. | ||
* | ||
* @param exceptionWrapper {@link BiFunction} that wraps the given message and checked exception | ||
* into a runtime exception | ||
* @return a result | ||
*/ | ||
default T get(BiFunction<String, Exception, RuntimeException> exceptionWrapper) { | ||
try { | ||
return getWithException(); | ||
} catch (RuntimeException ex) { | ||
throw ex; | ||
} catch (Exception ex) { | ||
throw exceptionWrapper.apply(ex.getMessage(), ex); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/test/java/com/soumakis/safetype/ThrowingSupplierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.soumakis.safetype; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class ThrowingSupplierTest { | ||
|
||
@Test | ||
void testGet() { | ||
ThrowingSupplier<Integer> supplier = () -> 1; | ||
assertEquals(1, supplier.get()); | ||
} | ||
|
||
@Test | ||
void testGetWithException() { | ||
ThrowingSupplier<Integer> supplier = () -> { | ||
throw new Exception("Test exception"); | ||
}; | ||
assertThrows(RuntimeException.class, supplier::get); | ||
} | ||
|
||
@Test | ||
void testGetWithExceptionCustomWrapper() { | ||
ThrowingSupplier<Integer> supplier = () -> { | ||
throw new Exception("Test exception"); | ||
}; | ||
assertThrows(IllegalStateException.class, () -> supplier.get((msg, ex) -> new IllegalStateException(msg, ex))); | ||
} | ||
} |