Skip to content

Commit

Permalink
Add AnyThrowingFunction & ThrowingFunction
Browse files Browse the repository at this point in the history
Signed-off-by: David Greven <fixed-term.David.Greven@de.bosch.com>
  • Loading branch information
grevend-bosch committed Dec 9, 2021
1 parent 8167216 commit e01b399
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
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 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
* @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,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 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
* @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;

public 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);
}
}

0 comments on commit e01b399

Please sign in to comment.