Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AnyThrowingFunction & ThrowingFunction #24

Merged
merged 3 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {@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
* @version JDK 8
* @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;
}
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;

/**
* 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
* @version JDK 8
* @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;
}
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 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);
}
}
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);
}
}