diff --git a/common-testing/src/test/java/io/carbynestack/common/result/ResultTest.java b/common-testing/src/test/java/io/carbynestack/common/result/ResultTest.java index d1dc12b..6acc1cb 100644 --- a/common-testing/src/test/java/io/carbynestack/common/result/ResultTest.java +++ b/common-testing/src/test/java/io/carbynestack/common/result/ResultTest.java @@ -6,7 +6,7 @@ */ package io.carbynestack.common.result; -import io.carbynestack.common.function.ThrowingSupplier; +import io.carbynestack.common.function.AnyThrowingSupplier; import io.carbynestack.testing.nullable.NullableParamSource; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -19,7 +19,7 @@ class ResultTest { @SuppressWarnings("unused") - private static final Arguments OF = Arguments.of((ThrowingSupplier) () -> 12, "some"); + private static final Arguments OF = Arguments.of((AnyThrowingSupplier) () -> 12, "some"); @Test void of() { @@ -39,7 +39,7 @@ void ofThrowsToFailure() { @ParameterizedTest @NullableParamSource("OF") - void ofNullPointerException(ThrowingSupplier supplier, String reason) { + void ofNullPointerException(AnyThrowingSupplier supplier, String reason) { assertThatThrownBy(() -> Result.of(supplier, reason)) .isExactlyInstanceOf(NullPointerException.class); } diff --git a/common-types/src/main/java/io/carbynestack/common/function/AnyThrowingConsumer.java b/common-types/src/main/java/io/carbynestack/common/function/AnyThrowingConsumer.java new file mode 100644 index 0000000..8310296 --- /dev/null +++ b/common-types/src/main/java/io/carbynestack/common/function/AnyThrowingConsumer.java @@ -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 ThrowingConsumer} throwing any kind of {@link Throwable}. + * + * @param the type of values consumed by this consumer + * @version JDK 8 + * @since 0.1.0 + */ +@FunctionalInterface +public interface AnyThrowingConsumer extends ThrowingConsumer { + /** + * {@inheritDoc} + * + * @param t the input argument + * @throws Throwable some throwable + * @since 0.1.0 + */ + @Override + void accept(T t) throws Throwable; +} diff --git a/common-types/src/main/java/io/carbynestack/common/function/AnyThrowingSupplier.java b/common-types/src/main/java/io/carbynestack/common/function/AnyThrowingSupplier.java new file mode 100644 index 0000000..0a002e9 --- /dev/null +++ b/common-types/src/main/java/io/carbynestack/common/function/AnyThrowingSupplier.java @@ -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 ThrowingSupplier} throwing any kind of {@link Throwable}. + * + * @param the type of results supplied by this supplier + * @version JDK 8 + * @since 0.1.0 + */ +@FunctionalInterface +public interface AnyThrowingSupplier extends ThrowingSupplier { + /** + * {@inheritDoc} + * + * @return a result + * @throws Throwable some throwable + * @since 0.1.0 + */ + @Override + T get() throws Throwable; +} diff --git a/common-types/src/main/java/io/carbynestack/common/function/ThrowingConsumer.java b/common-types/src/main/java/io/carbynestack/common/function/ThrowingConsumer.java new file mode 100644 index 0000000..f7b176d --- /dev/null +++ b/common-types/src/main/java/io/carbynestack/common/function/ThrowingConsumer.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; + +import java.util.function.Consumer; + +/** + * Represents a throwing {@link Consumer} of values. + * + * @param the type of values consumed by this consumer + * @param the type of throwable permitted by this consumer + * @version JDK 8 + * @since 0.1.0 + */ +@FunctionalInterface +public interface ThrowingConsumer { + /** + * Performs this operation on the given argument. + * + * @param t the input argument + * @throws E a throwable + * @since 0.1.0 + */ + void accept(T t) throws E; +} diff --git a/common-types/src/main/java/io/carbynestack/common/function/ThrowingSupplier.java b/common-types/src/main/java/io/carbynestack/common/function/ThrowingSupplier.java index bb8f8d4..6cb7f94 100644 --- a/common-types/src/main/java/io/carbynestack/common/function/ThrowingSupplier.java +++ b/common-types/src/main/java/io/carbynestack/common/function/ThrowingSupplier.java @@ -6,16 +6,18 @@ */ package io.carbynestack.common.function; +import java.util.function.Supplier; + /** - * Represents a throwing supplier of results. + * Represents a throwing {@link Supplier} of results. * - * @param the type of throwable permitted by this supplier * @param the type of results supplied by this supplier + * @param the type of throwable permitted by this supplier * @version JDK 8 * @since 0.1.0 */ @FunctionalInterface -public interface ThrowingSupplier { +public interface ThrowingSupplier { /** * Gets a result or throw a {@link Throwable} of type {@link E}. * diff --git a/common-types/src/main/java/io/carbynestack/common/result/Result.java b/common-types/src/main/java/io/carbynestack/common/result/Result.java index fe1acb8..cfb9935 100644 --- a/common-types/src/main/java/io/carbynestack/common/result/Result.java +++ b/common-types/src/main/java/io/carbynestack/common/result/Result.java @@ -6,7 +6,7 @@ */ package io.carbynestack.common.result; -import io.carbynestack.common.function.ThrowingSupplier; +import io.carbynestack.common.function.AnyThrowingSupplier; import java.util.Optional; import java.util.function.Consumer; @@ -29,19 +29,18 @@ */ public interface Result { /** - * Returns the {@link Success} value of the {@link ThrowingSupplier} or - * the supplied {@link Failure} reason if the supplier has thrown a - * {@link Throwable} of type {@link E}. + * Returns the {@link Success} value of the {@link AnyThrowingSupplier} + * or the supplied {@link Failure} reason if the supplier has thrown a + * {@link Throwable}. * * @param supplier the {@code Success} result supplier * @param reason the {@code Failure} reason - * @param the type of throwable permitted by this supplier * @param the success value type * @param the failure reason type - * @return the supplied {@code Success} or {@code Failure} result + * @return the supplied {@code Success} or a {@code Failure} result * @since 0.1.0 */ - static Result of(ThrowingSupplier supplier, F reason) { + static Result of(AnyThrowingSupplier supplier, F reason) { requireNonNull(supplier); requireNonNull(reason); try { diff --git a/common-types/src/main/java17/io/carbynestack/common/result/Result.java b/common-types/src/main/java17/io/carbynestack/common/result/Result.java index 158536c..3d63643 100644 --- a/common-types/src/main/java17/io/carbynestack/common/result/Result.java +++ b/common-types/src/main/java17/io/carbynestack/common/result/Result.java @@ -6,6 +6,7 @@ */ package io.carbynestack.common.result; +import io.carbynestack.common.function.AnyThrowingSupplier; import io.carbynestack.common.function.ThrowingSupplier; import java.util.Optional; @@ -29,19 +30,18 @@ */ public sealed interface Result permits Failure, Success { /** - * Returns the {@link Success} value of the {@link ThrowingSupplier} or - * the supplied {@link Failure} reason if the supplier has thrown a - * {@link Throwable} of type {@link E}. + * Returns the {@link Success} value of the {@link AnyThrowingSupplier} + * or the supplied {@link Failure} reason if the supplier has thrown a + * {@link Throwable}. * * @param supplier the {@code Success} result supplier * @param reason the {@code Failure} reason - * @param the type of throwable permitted by this supplier * @param the success value type * @param the failure reason type - * @return the supplied {@code Success} or {@code Failure} result + * @return the supplied {@code Success} or a {@code Failure} result * @since 0.1.0 */ - static Result of(ThrowingSupplier supplier, F reason) { + static Result of(AnyThrowingSupplier supplier, F reason) { requireNonNull(supplier); requireNonNull(reason); try { diff --git a/common-types/src/test/java/io/carbynestack/common/function/AnyThrowingConsumerTest.java b/common-types/src/test/java/io/carbynestack/common/function/AnyThrowingConsumerTest.java new file mode 100644 index 0000000..9fb5c68 --- /dev/null +++ b/common-types/src/test/java/io/carbynestack/common/function/AnyThrowingConsumerTest.java @@ -0,0 +1,35 @@ +/* + * 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 java.util.concurrent.atomic.AtomicInteger; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class AnyThrowingConsumerTest { + @Test + void accept() throws Throwable { + var value = 12; + var output = new AtomicInteger(-1); + AnyThrowingConsumer consumer = output::set; + consumer.accept(value); + assertThat(output).hasValue(value); + } + + @Test + void acceptThrowsException() { + AnyThrowingConsumer consumer = v -> { + throw new IOException(); + }; + assertThatThrownBy(() -> consumer.accept(12)) + .isExactlyInstanceOf(IOException.class); + } +} diff --git a/common-types/src/test/java/io/carbynestack/common/function/AnyThrowingSupplierTest.java b/common-types/src/test/java/io/carbynestack/common/function/AnyThrowingSupplierTest.java new file mode 100644 index 0000000..08a07e9 --- /dev/null +++ b/common-types/src/test/java/io/carbynestack/common/function/AnyThrowingSupplierTest.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 AnyThrowingSupplierTest { + @Test + void get() throws Throwable { + var value = 12; + AnyThrowingSupplier supplier = () -> value; + assertThat(supplier.get()).isEqualTo(value); + } + + @Test + void getThrowsException() { + AnyThrowingSupplier supplier = () -> { + throw new IOException(); + }; + assertThatThrownBy(supplier::get) + .isExactlyInstanceOf(IOException.class); + } +} diff --git a/common-types/src/test/java/io/carbynestack/common/function/ThrowingConsumerTest.java b/common-types/src/test/java/io/carbynestack/common/function/ThrowingConsumerTest.java new file mode 100644 index 0000000..529f89f --- /dev/null +++ b/common-types/src/test/java/io/carbynestack/common/function/ThrowingConsumerTest.java @@ -0,0 +1,44 @@ +/* + * 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 java.util.concurrent.atomic.AtomicInteger; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class ThrowingConsumerTest { + @Test + void accept() { + var value = 12; + var output = new AtomicInteger(-1); + ThrowingConsumer consumer = output::set; + consumer.accept(value); + assertThat(output).hasValue(value); + } + + @Test + void acceptCheckedException() throws IOException { + var value = 12; + var output = new AtomicInteger(-1); + ThrowingConsumer consumer = output::set; + consumer.accept(value); + assertThat(output).hasValue(value); + } + + @Test + void acceptThrowsException() { + ThrowingConsumer consumer = v -> { + throw new IOException(); + }; + assertThatThrownBy(() -> consumer.accept(12)) + .isExactlyInstanceOf(IOException.class); + } +} diff --git a/common-types/src/test/java/io/carbynestack/common/function/ThrowingSupplierTest.java b/common-types/src/test/java/io/carbynestack/common/function/ThrowingSupplierTest.java index 6dcdca3..e48b499 100644 --- a/common-types/src/test/java/io/carbynestack/common/function/ThrowingSupplierTest.java +++ b/common-types/src/test/java/io/carbynestack/common/function/ThrowingSupplierTest.java @@ -17,20 +17,20 @@ class ThrowingSupplierTest { @Test void get() { var value = 12; - ThrowingSupplier supplier = () -> value; + ThrowingSupplier supplier = () -> value; assertThat(supplier.get()).isEqualTo(value); } @Test void getCheckedException() throws IOException { var value = 12; - ThrowingSupplier supplier = () -> value; + ThrowingSupplier supplier = () -> value; assertThat(supplier.get()).isEqualTo(value); } @Test void getThrowsException() { - ThrowingSupplier supplier = () -> { + ThrowingSupplier supplier = () -> { throw new IOException(); }; assertThatThrownBy(supplier::get) diff --git a/common-types/src/test/java/io/carbynestack/common/result/ResultTest.java b/common-types/src/test/java/io/carbynestack/common/result/ResultTest.java index 8189296..27f9869 100644 --- a/common-types/src/test/java/io/carbynestack/common/result/ResultTest.java +++ b/common-types/src/test/java/io/carbynestack/common/result/ResultTest.java @@ -19,7 +19,7 @@ void success() { var value = 12; var other = new Success(value); - var res = Result.of(() -> { + var res = Result.of(() -> { throw new IOException(); }, 21) .map(v -> v * 2)