Skip to content

Commit

Permalink
Rename throwable FunctionalInterfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
chiquitinxx committed Mar 8, 2023
1 parent 25cb725 commit 32b6d47
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/main/java/dev/yila/functional/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static <T> Result<T> create(Supplier<T> supplier) {
* @param <K>
* @return Result<T>
*/
public static <T, K extends Throwable> Result<T> createChecked(ThrowingSupplierException<T, K> throwingSupplier, Class<K> throwableClass) {
public static <T, K extends Throwable> Result<T> createChecked(ThrowingSupplier<T, K> throwingSupplier, Class<K> throwableClass) {
try {
return Result.ok(throwingSupplier.get());
} catch (Throwable throwable) {
Expand Down Expand Up @@ -193,7 +193,7 @@ public <R> Result<R> flatMap(Function<T, Result<R>> function) {
* @param <R>
* @param <K>
*/
public <R, K extends Throwable> Result<R> flatMap(ThrowingFunctionException<T, R, K> function, Class<K> throwableClass) {
public <R, K extends Throwable> Result<R> flatMap(ThrowingFunction<T, R, K> function, Class<K> throwableClass) {
if (hasFailure()) {
return (Result<R>) this;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.yila.functional;

@FunctionalInterface
public interface ThrowingFunctionException<T, R, E extends Throwable> {
public interface ThrowingFunction<T, R, E extends Throwable> {
R apply(T input) throws E;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.yila.functional;

@FunctionalInterface
public interface ThrowingSupplierException<T, E extends Throwable> {
public interface ThrowingSupplier<T, E extends Throwable> {
T get() throws E;
}
22 changes: 11 additions & 11 deletions src/test/java/dev/yila/functional/ResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,21 @@ void checkedExceptionNotThrown() {
@Test
void checkedExceptionLaunched() {
String exceptionMessage = "fail :(";
ThrowingSupplierException supplierException = () -> {
ThrowingSupplier throwingSupplier = () -> {
throw new RuntimeException(exceptionMessage);
};
Result<Integer> result = Result.createChecked(supplierException, RuntimeException.class);
Result<Integer> result = Result.createChecked(throwingSupplier, RuntimeException.class);
assertEquals("ThrowableFailure: java.lang.RuntimeException: fail :(", result.failure().get().toString());
ThrowableFailure failure = (ThrowableFailure) result.failure().get();
assertEquals(exceptionMessage, failure.getThrowable().getMessage());
}

@Test
void checkedExceptionInFlatMapOnFail() {
ThrowingSupplierException supplierException = () -> {
ThrowingSupplier throwingSupplier = () -> {
throw new RuntimeException("aaa");
};
Result<Integer> result = Result.createChecked(supplierException, RuntimeException.class)
Result<Integer> result = Result.createChecked(throwingSupplier, RuntimeException.class)
.flatMap(number -> {
throw new RuntimeException("uuu");
}, RuntimeException.class);
Expand All @@ -123,21 +123,21 @@ void checkedExceptionInFlatMapOnFail() {

@Test
void checkedExceptionInFlatMap() {
ThrowingFunctionException functionException = (input) -> {
ThrowingFunction throwingFunction = (input) -> {
throw new RuntimeException("Fail");
};
Result<Integer> result = Result.create(() -> 3)
.flatMap(functionException, RuntimeException.class);
.flatMap(throwingFunction, RuntimeException.class);
assertEquals("ThrowableFailure: java.lang.RuntimeException: Fail", result.failure().get().toString());
}

@Test
void getFailureAsThrowable() {
String exceptionMessage = "fail :(";
ThrowingSupplierException supplierException = () -> {
ThrowingSupplier throwingSupplier = () -> {
throw new RuntimeException(exceptionMessage);
};
Result<Integer> result = Result.createChecked(supplierException, RuntimeException.class);
Result<Integer> result = Result.createChecked(throwingSupplier, RuntimeException.class);
Throwable throwable = result.failure().get().toThrowable();
assertEquals(exceptionMessage, throwable.getMessage());
}
Expand All @@ -153,11 +153,11 @@ void throwableFailure() {
@Test
void unexpectedCheckedExceptions() {
String exceptionMessage = "fail :(";
ThrowingSupplierException supplierException = () -> {
ThrowingSupplier throwingSupplier = () -> {
throw new RuntimeException(exceptionMessage);
};
assertThrows(RuntimeException.class, () ->
Result.createChecked(supplierException, TestException.class));
Result.createChecked(throwingSupplier, TestException.class));
}

@Test
Expand Down Expand Up @@ -187,7 +187,7 @@ void toOptional() {

@Test
void flatMapWithCheckedException() {
ThrowingFunctionException<Integer, Integer, RuntimeException> function =
ThrowingFunction<Integer, Integer, RuntimeException> function =
(input) -> input + 2;
Result<Integer> result = Result.ok(6)
.flatMap(function, RuntimeException.class);
Expand Down

0 comments on commit 32b6d47

Please sign in to comment.