-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AnyThrowingFunction & ThrowingFunction
Signed-off-by: David Greven <fixed-term.David.Greven@de.bosch.com>
- Loading branch information
1 parent
8167216
commit e01b399
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
common-types/src/main/java/io/carbynestack/common/function/AnyThrowingFunction.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,27 @@ | ||
/* | ||
* Copyright (c) 2021 - for information on the respective copyright owner | ||
* see the NOTICE file and/or the repository https://github.com/carbynestack/common. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package io.carbynestack.common.function; | ||
|
||
/** | ||
* Represents a {@link ThrowingFunction} throwing any kind of {@link Throwable}. | ||
* | ||
* @param <T> the type of the input to the function | ||
* @param <R> the type of the result of the function | ||
* @since 0.2.0 | ||
*/ | ||
public interface AnyThrowingFunction<T, R> extends ThrowingFunction<T, R, Throwable> { | ||
/** | ||
* {@inheritDoc} | ||
* | ||
* @param t the function argument | ||
* @return the function result | ||
* @throws Throwable some throwable | ||
* @since 0.2.0 | ||
*/ | ||
@Override | ||
R apply(T t) throws Throwable; | ||
} |
28 changes: 28 additions & 0 deletions
28
common-types/src/main/java/io/carbynestack/common/function/ThrowingFunction.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,28 @@ | ||
/* | ||
* Copyright (c) 2021 - for information on the respective copyright owner | ||
* see the NOTICE file and/or the repository https://github.com/carbynestack/common. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package io.carbynestack.common.function; | ||
|
||
/** | ||
* Represents a throwing function that accepts one | ||
* argument and produces a result. | ||
* | ||
* @param <T> the type of the input to the function | ||
* @param <R> the type of the result of the function | ||
* @since 0.2.0 | ||
*/ | ||
public interface ThrowingFunction<T, R, E extends Throwable> { | ||
/** | ||
* Applies this function to the given argument | ||
* or throws a {@link Throwable} of type {@link E}. | ||
* | ||
* @param t the function argument | ||
* @return the function result | ||
* @throws E a throwable | ||
* @since 0.2.0 | ||
*/ | ||
R apply(T t) throws E; | ||
} |
32 changes: 32 additions & 0 deletions
32
common-types/src/test/java/io/carbynestack/common/function/AnyThrowingFunctionTest.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,32 @@ | ||
/* | ||
* Copyright (c) 2021 - for information on the respective copyright owner | ||
* see the NOTICE file and/or the repository https://github.com/carbynestack/common. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package io.carbynestack.common.function; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class AnyThrowingFunctionTest { | ||
@Test | ||
void apply() throws Throwable { | ||
var value = 12; | ||
AnyThrowingFunction<Integer, Integer> function = v -> v; | ||
assertThat(function.apply(value)).isEqualTo(value); | ||
} | ||
|
||
@Test | ||
void applyThrowsException() { | ||
AnyThrowingFunction<Integer, Integer> function = v -> { | ||
throw new IOException(); | ||
}; | ||
assertThatThrownBy(() -> function.apply(12)) | ||
.isExactlyInstanceOf(IOException.class); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
common-types/src/test/java/io/carbynestack/common/function/ThrowingFunctionTest.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,39 @@ | ||
/* | ||
* Copyright (c) 2021 - for information on the respective copyright owner | ||
* see the NOTICE file and/or the repository https://github.com/carbynestack/common. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package io.carbynestack.common.function; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class ThrowingFunctionTest { | ||
@Test | ||
void apply() { | ||
var value = 12; | ||
ThrowingFunction<Integer, Integer, RuntimeException> function = v -> v; | ||
assertThat(function.apply(value)).isEqualTo(value); | ||
} | ||
|
||
@Test | ||
void applyCheckedException() throws IOException { | ||
var value = 12; | ||
ThrowingFunction<Integer, Integer, IOException> function = v -> v; | ||
assertThat(function.apply(value)).isEqualTo(value); | ||
} | ||
|
||
@Test | ||
void applyThrowsException() { | ||
ThrowingFunction<Integer, Integer, IOException> function = v -> { | ||
throw new IOException(); | ||
}; | ||
assertThatThrownBy(() -> function.apply(12)) | ||
.isExactlyInstanceOf(IOException.class); | ||
} | ||
} |