Skip to content

Commit

Permalink
Add ThrowingConsumer, AnyThrowingSupplier & AnyThrowingConsumer (#8)
Browse files Browse the repository at this point in the history
Signed-off-by: David Greven <[email protected]>
Signed-off-by: Sebastian Becker <[email protected]>
  • Loading branch information
grevend-bosch committed Nov 15, 2021
1 parent f60395b commit 91d8b58
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +19,7 @@

class ResultTest {
@SuppressWarnings("unused")
private static final Arguments OF = Arguments.of((ThrowingSupplier<Throwable, Integer>) () -> 12, "some");
private static final Arguments OF = Arguments.of((AnyThrowingSupplier<Integer>) () -> 12, "some");

@Test
void of() {
Expand All @@ -39,7 +39,7 @@ void ofThrowsToFailure() {

@ParameterizedTest
@NullableParamSource("OF")
void ofNullPointerException(ThrowingSupplier<Throwable, Integer> supplier, String reason) {
void ofNullPointerException(AnyThrowingSupplier<Integer> supplier, String reason) {
assertThatThrownBy(() -> Result.of(supplier, reason))
.isExactlyInstanceOf(NullPointerException.class);
}
Expand Down
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 ThrowingConsumer} throwing any kind of {@link Throwable}.
*
* @param <T> the type of values consumed by this consumer
* @version JDK 8
* @since 0.1.0
*/
@FunctionalInterface
public interface AnyThrowingConsumer<T> extends ThrowingConsumer<T, Throwable> {
/**
* {@inheritDoc}
*
* @param t the input argument
* @throws Throwable some throwable
* @since 0.1.0
*/
@Override
void accept(T t) throws Throwable;
}
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 ThrowingSupplier} throwing any kind of {@link Throwable}.
*
* @param <T> the type of results supplied by this supplier
* @version JDK 8
* @since 0.1.0
*/
@FunctionalInterface
public interface AnyThrowingSupplier<T> extends ThrowingSupplier<T, Throwable> {
/**
* {@inheritDoc}
*
* @return a result
* @throws Throwable some throwable
* @since 0.1.0
*/
@Override
T get() throws Throwable;
}
Original file line number Diff line number Diff line change
@@ -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 <T> the type of values consumed by this consumer
* @param <E> the type of throwable permitted by this consumer
* @version JDK 8
* @since 0.1.0
*/
@FunctionalInterface
public interface ThrowingConsumer<T, E extends Throwable> {
/**
* 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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <E> the type of throwable permitted by this supplier
* @param <T> the type of results supplied by this supplier
* @param <E> the type of throwable permitted by this supplier
* @version JDK 8
* @since 0.1.0
*/
@FunctionalInterface
public interface ThrowingSupplier<E extends Throwable, T> {
public interface ThrowingSupplier<T, E extends Throwable> {
/**
* Gets a result or throw a {@link Throwable} of type {@link E}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,19 +29,18 @@
*/
public interface Result<S, F> {
/**
* 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 <E> the type of throwable permitted by this supplier
* @param <S> the success value type
* @param <F> 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 <E extends Throwable, S, F> Result<S, F> of(ThrowingSupplier<E, S> supplier, F reason) {
static <S, F> Result<S, F> of(AnyThrowingSupplier<S> supplier, F reason) {
requireNonNull(supplier);
requireNonNull(reason);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,19 +30,18 @@
*/
public sealed interface Result<S, F> 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 <E> the type of throwable permitted by this supplier
* @param <S> the success value type
* @param <F> 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 <E extends Throwable, S, F> Result<S, F> of(ThrowingSupplier<E, S> supplier, F reason) {
static <S, F> Result<S, F> of(AnyThrowingSupplier<S> supplier, F reason) {
requireNonNull(supplier);
requireNonNull(reason);
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer> consumer = output::set;
consumer.accept(value);
assertThat(output).hasValue(value);
}

@Test
void acceptThrowsException() {
AnyThrowingConsumer<Integer> consumer = v -> {
throw new IOException();
};
assertThatThrownBy(() -> consumer.accept(12))
.isExactlyInstanceOf(IOException.class);
}
}
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;

class AnyThrowingSupplierTest {
@Test
void get() throws Throwable {
var value = 12;
AnyThrowingSupplier<Integer> supplier = () -> value;
assertThat(supplier.get()).isEqualTo(value);
}

@Test
void getThrowsException() {
AnyThrowingSupplier<Integer> supplier = () -> {
throw new IOException();
};
assertThatThrownBy(supplier::get)
.isExactlyInstanceOf(IOException.class);
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer, RuntimeException> consumer = output::set;
consumer.accept(value);
assertThat(output).hasValue(value);
}

@Test
void acceptCheckedException() throws IOException {
var value = 12;
var output = new AtomicInteger(-1);
ThrowingConsumer<Integer, IOException> consumer = output::set;
consumer.accept(value);
assertThat(output).hasValue(value);
}

@Test
void acceptThrowsException() {
ThrowingConsumer<Integer, IOException> consumer = v -> {
throw new IOException();
};
assertThatThrownBy(() -> consumer.accept(12))
.isExactlyInstanceOf(IOException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ class ThrowingSupplierTest {
@Test
void get() {
var value = 12;
ThrowingSupplier<RuntimeException, Integer> supplier = () -> value;
ThrowingSupplier<Integer, RuntimeException> supplier = () -> value;
assertThat(supplier.get()).isEqualTo(value);
}

@Test
void getCheckedException() throws IOException {
var value = 12;
ThrowingSupplier<IOException, Integer> supplier = () -> value;
ThrowingSupplier<Integer, IOException> supplier = () -> value;
assertThat(supplier.get()).isEqualTo(value);
}

@Test
void getThrowsException() {
ThrowingSupplier<IOException, Integer> supplier = () -> {
ThrowingSupplier<Integer, IOException> supplier = () -> {
throw new IOException();
};
assertThatThrownBy(supplier::get)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void success() {
var value = 12;
var other = new Success<Integer, Integer>(value);

var res = Result.<IOException, Integer, Integer>of(() -> {
var res = Result.<Integer, Integer>of(() -> {
throw new IOException();
}, 21)
.map(v -> v * 2)
Expand Down

0 comments on commit 91d8b58

Please sign in to comment.