-
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 ThrowingConsumer, AnyThrowingSupplier & AnyThrowingConsumer (#8)
Signed-off-by: David Greven <[email protected]> Signed-off-by: Sebastian Becker <[email protected]>
- Loading branch information
1 parent
f60395b
commit 91d8b58
Showing
12 changed files
with
218 additions
and
23 deletions.
There are no files selected for viewing
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
27 changes: 27 additions & 0 deletions
27
common-types/src/main/java/io/carbynestack/common/function/AnyThrowingConsumer.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 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; | ||
} |
27 changes: 27 additions & 0 deletions
27
common-types/src/main/java/io/carbynestack/common/function/AnyThrowingSupplier.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 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; | ||
} |
29 changes: 29 additions & 0 deletions
29
common-types/src/main/java/io/carbynestack/common/function/ThrowingConsumer.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,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; | ||
} |
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
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
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
35 changes: 35 additions & 0 deletions
35
common-types/src/test/java/io/carbynestack/common/function/AnyThrowingConsumerTest.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,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); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
common-types/src/test/java/io/carbynestack/common/function/AnyThrowingSupplierTest.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; | ||
|
||
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); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
common-types/src/test/java/io/carbynestack/common/function/ThrowingConsumerTest.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,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); | ||
} | ||
} |
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
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