ThrowingBiFunction.java

1
/*
2
 * Copyright 2014-2016 Grzegorz Piwowarek, https://4comprehension.com/
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.pivovarit.function;
17
18
import java.util.Optional;
19
import java.util.function.BiFunction;
20
21
import static java.util.Objects.requireNonNull;
22
23
/**
24
 * Represents a function that accepts two arguments and produces a result.
25
 * This is the two-arity specialization of {@link ThrowingFunction}.
26
 * Function may throw a checked exception.
27
 *
28
 * @param <T1> the type of the first argument to the function
29
 * @param <T2> the type of the second argument to the function
30
 * @param <R>  the type of the result of the function
31
 * @param <E>  the type of the thrown checked exception
32
 *
33
 * @author Grzegorz Piwowarek
34
 * @see ThrowingFunction
35
 */
36
@FunctionalInterface
37
public interface ThrowingBiFunction<T1, T2, R, E extends Exception> {
38
    /**
39
     * Applies this function to the given arguments.
40
     *
41
     * @param arg1 the first function argument
42
     * @param arg2 the second function argument
43
     * @return the function result
44
     * @throws E the checked exception type
45
     */
46
    R apply(T1 arg1, T2 arg2) throws E;
47
48
    /**
49
     * Returns a new BiFunction instance which wraps the thrown checked exception instance into a {@link CheckedException}
50
     *
51
     * @param <T1>     the type of the first argument to the function
52
     * @param <T2>     the type of the second argument to the function
53
     * @param <R>      the type of the result of the function
54
     * @param function the ThrowingBiFunction to wrap
55
     * @return BiFunction instance that wraps the checked exception into a {@link CheckedException}
56
     */
57
    static <T1, T2, R> BiFunction<T1, T2, R> unchecked(ThrowingBiFunction<? super T1, ? super T2, ? extends R, ?> function) {
58
        requireNonNull(function);
59
60 1 1. unchecked : replaced return value with null for com/pivovarit/function/ThrowingBiFunction::unchecked → KILLED
        return (arg1, arg2) -> {
61
            try {
62 1 1. lambda$unchecked$0 : replaced return value with null for com/pivovarit/function/ThrowingBiFunction::lambda$unchecked$0 → SURVIVED
                return function.apply(arg1, arg2);
63
            } catch (final Exception e) {
64
                throw new CheckedException(e);
65
            }
66
        };
67
    }
68
69
    /**
70
     * Returns a new BiFunction instance which rethrows the checked exception using the Sneaky Throws pattern
71
     *
72
     * @param <T1>     the type of the first argument to the function
73
     * @param <T2>     the type of the second argument to the function
74
     * @param <R>      the type of the result of the function
75
     * @param function the ThrowingBiFunction to wrap
76
     * @return BiFunction instance that rethrows the checked exception using the Sneaky Throws pattern
77
     */
78
    static <T1, T2, R> BiFunction<T1, T2, R> sneaky(ThrowingBiFunction<? super T1, ? super T2, ? extends R, ?> function) {
79
        requireNonNull(function);
80 1 1. sneaky : replaced return value with null for com/pivovarit/function/ThrowingBiFunction::sneaky → KILLED
        return (t1, t2) -> {
81
            try {
82 1 1. lambda$sneaky$0 : replaced return value with null for com/pivovarit/function/ThrowingBiFunction::lambda$sneaky$0 → NO_COVERAGE
                return function.apply(t1, t2);
83
            } catch (final Exception ex) {
84 1 1. lambda$sneaky$0 : replaced return value with null for com/pivovarit/function/ThrowingBiFunction::lambda$sneaky$0 → NO_COVERAGE
                return SneakyThrowUtil.sneakyThrow(ex);
85
            }
86
        };
87
    }
88
89
    /**
90
     * Returns a new BiFunction instance which returns the result as an Optional, or an empty Optional in case of a thrown exception
91
     *
92
     * @param <T1>     the type of the first argument to the function
93
     * @param <T2>     the type of the second argument to the function
94
     * @param <R>      the type of the result of the function
95
     * @param function the ThrowingBiFunction to wrap
96
     * @return BiFunction instance that returns the result as an Optional, or empty Optional in case of a thrown exception
97
     */
98
    static <T1, T2, R> BiFunction<T1, T2, Optional<R>> optional(ThrowingBiFunction<? super T1, ? super T2, ? extends R, ?> function) {
99
        requireNonNull(function);
100
101 1 1. optional : replaced return value with null for com/pivovarit/function/ThrowingBiFunction::optional → KILLED
        return (arg1, arg2) -> {
102
            try {
103 1 1. lambda$optional$0 : replaced return value with Optional.empty for com/pivovarit/function/ThrowingBiFunction::lambda$optional$0 → KILLED
                return Optional.ofNullable(function.apply(arg1, arg2));
104
            } catch (final Exception e) {
105
                return Optional.empty();
106
            }
107
        };
108
    }
109
}

Mutations

60

1.1
Location : unchecked
Killed by : com.pivovarit.function.ThrowingBiFunctionTest.[engine:junit-jupiter]/[class:com.pivovarit.function.ThrowingBiFunctionTest]/[method:shouldApplyWhenNoExceptionThrown()]
replaced return value with null for com/pivovarit/function/ThrowingBiFunction::unchecked → KILLED

62

1.1
Location : lambda$unchecked$0
Killed by : none
replaced return value with null for com/pivovarit/function/ThrowingBiFunction::lambda$unchecked$0 → SURVIVED
Covering tests

80

1.1
Location : sneaky
Killed by : com.pivovarit.function.ThrowingBiFunctionTest.[engine:junit-jupiter]/[class:com.pivovarit.function.ThrowingBiFunctionTest]/[method:shouldSneakyThrow()]
replaced return value with null for com/pivovarit/function/ThrowingBiFunction::sneaky → KILLED

82

1.1
Location : lambda$sneaky$0
Killed by : none
replaced return value with null for com/pivovarit/function/ThrowingBiFunction::lambda$sneaky$0 → NO_COVERAGE

84

1.1
Location : lambda$sneaky$0
Killed by : none
replaced return value with null for com/pivovarit/function/ThrowingBiFunction::lambda$sneaky$0 → NO_COVERAGE

101

1.1
Location : optional
Killed by : com.pivovarit.function.ThrowingBiFunctionTest.[engine:junit-jupiter]/[class:com.pivovarit.function.ThrowingBiFunctionTest]/[method:shouldWrapInOptionalWhenUsingLifted()]
replaced return value with null for com/pivovarit/function/ThrowingBiFunction::optional → KILLED

103

1.1
Location : lambda$optional$0
Killed by : com.pivovarit.function.ThrowingBiFunctionTest.[engine:junit-jupiter]/[class:com.pivovarit.function.ThrowingBiFunctionTest]/[method:shouldWrapInOptionalWhenUsingLifted()]
replaced return value with Optional.empty for com/pivovarit/function/ThrowingBiFunction::lambda$optional$0 → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support