diff --git a/common-types/src/main/java/io/carbynestack/common/function/AnyThrowingFunction.java b/common-types/src/main/java/io/carbynestack/common/function/AnyThrowingFunction.java new file mode 100644 index 0000000..643ce54 --- /dev/null +++ b/common-types/src/main/java/io/carbynestack/common/function/AnyThrowingFunction.java @@ -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 {@link ThrowingFunction} throwing any kind of {@link Throwable}. + * + * @param the type of the input to the function + * @param the type of the result of the function + * @version JDK 8 + * @since 0.2.0 + */ +public interface AnyThrowingFunction extends ThrowingFunction { + /** + * {@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; +} diff --git a/common-types/src/main/java/io/carbynestack/common/function/ThrowingFunction.java b/common-types/src/main/java/io/carbynestack/common/function/ThrowingFunction.java new file mode 100644 index 0000000..a38838c --- /dev/null +++ b/common-types/src/main/java/io/carbynestack/common/function/ThrowingFunction.java @@ -0,0 +1,29 @@ +/* + * 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 the type of the input to the function + * @param the type of the result of the function + * @version JDK 8 + * @since 0.2.0 + */ +public interface ThrowingFunction { + /** + * 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; +} diff --git a/common-types/src/test/java/io/carbynestack/common/function/AnyThrowingFunctionTest.java b/common-types/src/test/java/io/carbynestack/common/function/AnyThrowingFunctionTest.java new file mode 100644 index 0000000..6a4ea97 --- /dev/null +++ b/common-types/src/test/java/io/carbynestack/common/function/AnyThrowingFunctionTest.java @@ -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; + +class AnyThrowingFunctionTest { + @Test + void apply() throws Throwable { + var value = 12; + AnyThrowingFunction function = v -> v; + assertThat(function.apply(value)).isEqualTo(value); + } + + @Test + void applyThrowsException() { + AnyThrowingFunction function = v -> { + throw new IOException(); + }; + assertThatThrownBy(() -> function.apply(12)) + .isExactlyInstanceOf(IOException.class); + } +} diff --git a/common-types/src/test/java/io/carbynestack/common/function/ThrowingFunctionTest.java b/common-types/src/test/java/io/carbynestack/common/function/ThrowingFunctionTest.java new file mode 100644 index 0000000..4eec290 --- /dev/null +++ b/common-types/src/test/java/io/carbynestack/common/function/ThrowingFunctionTest.java @@ -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 function = v -> v; + assertThat(function.apply(value)).isEqualTo(value); + } + + @Test + void applyCheckedException() throws IOException { + var value = 12; + ThrowingFunction function = v -> v; + assertThat(function.apply(value)).isEqualTo(value); + } + + @Test + void applyThrowsException() { + ThrowingFunction function = v -> { + throw new IOException(); + }; + assertThatThrownBy(() -> function.apply(12)) + .isExactlyInstanceOf(IOException.class); + } +}