ThrowingFunction.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.Function;
20
21
import static java.util.Objects.requireNonNull;
22
23
/**
24
 * Represents a function that accepts one argument and returns a value;
25
 * Function might throw a checked exception instance.
26
 *
27
 * @param <T> the type of the input to the function
28
 * @param <R> the type of the result of the function
29
 * @param <E> the type of the thrown checked exception
30
 * @author Grzegorz Piwowarek
31
 */
32
@FunctionalInterface
33
public interface ThrowingFunction<T, R, E extends Exception> {
34
    /**
35
     * Applies this function to the given argument.
36
     *
37
     * @param arg the function argument
38
     * @return the function result
39
     * @throws E the checked exception type
40
     */
41
    R apply(T arg) throws E;
42
43
    /**
44
     * Returns a new Function instance which returns the result as an Optional, or an empty Optional in case of a thrown exception
45
     *
46
     * @param <T>      the type of the input to the function
47
     * @param <R>      the type of the result of the function
48
     * @param function the ThrowingFunction to wrap
49
     * @return a Function that returns the result of the given function as an Optional instance.
50
     * In case of a failure, empty Optional is returned
51
     */
52
    static <T, R> Function<T, Optional<R>> lifted(final ThrowingFunction<? super T, ? extends R, ?> function) {
53
        requireNonNull(function);
54
55 1 1. lifted : replaced return value with null for com/pivovarit/function/ThrowingFunction::lifted → KILLED
        return t -> {
56
            try {
57 1 1. lambda$lifted$0 : replaced return value with Optional.empty for com/pivovarit/function/ThrowingFunction::lambda$lifted$0 → NO_COVERAGE
                return Optional.ofNullable(function.apply(t));
58
            } catch (final Exception e) {
59
                return Optional.empty();
60
            }
61
        };
62
    }
63
64
    /**
65
     * Returns a new Function instance which wraps the thrown checked exception instance into a {@link CheckedException}
66
     *
67
     * @param <T>      the type of the input to the function
68
     * @param <R>      the type of the result of the function
69
     * @param function the ThrowingFunction to wrap
70
     * @return Function instance that wraps the checked exception into a {@link CheckedException}
71
     */
72
    static <T, R> Function<T, R> unchecked(final ThrowingFunction<? super T, ? extends R, ?> function) {
73
        requireNonNull(function);
74 1 1. unchecked : replaced return value with null for com/pivovarit/function/ThrowingFunction::unchecked → KILLED
        return t -> {
75
            try {
76 1 1. lambda$unchecked$0 : replaced return value with null for com/pivovarit/function/ThrowingFunction::lambda$unchecked$0 → KILLED
                return function.apply(t);
77
            } catch (final Exception e) {
78
                throw new CheckedException(e);
79
            }
80
        };
81
    }
82
83
    /**
84
     * Returns a new Function instance which rethrows the checked exception using the Sneaky Throws pattern
85
     *
86
     * @param <T1>     the type of the input to the function
87
     * @param <R>      the type of the result of the function
88
     * @param function the ThrowingFunction to wrap
89
     * @return Function instance that rethrows the checked exception using the Sneaky Throws pattern
90
     */
91
    static <T1, R> Function<T1, R> sneaky(ThrowingFunction<? super T1, ? extends R, ?> function) {
92
        requireNonNull(function);
93 1 1. sneaky : replaced return value with null for com/pivovarit/function/ThrowingFunction::sneaky → KILLED
        return t -> {
94
            try {
95 1 1. lambda$sneaky$0 : replaced return value with null for com/pivovarit/function/ThrowingFunction::lambda$sneaky$0 → NO_COVERAGE
                return function.apply(t);
96
            } catch (final Exception ex) {
97 1 1. lambda$sneaky$0 : replaced return value with null for com/pivovarit/function/ThrowingFunction::lambda$sneaky$0 → NO_COVERAGE
                return SneakyThrowUtil.sneakyThrow(ex);
98
            }
99
        };
100
    }
101
102
    /**
103
     * Returns a new Function instance which returns the result as an Optional, or an empty Optional in case of a thrown exception
104
     *
105
     * @return a Function that returns the result as an Optional instance, or an empty Optional in case of a failure
106
     */
107
    default Function<T, Optional<R>> lift() {
108 1 1. lift : replaced return value with null for com/pivovarit/function/ThrowingFunction::lift → KILLED
        return t -> {
109
            try {
110 1 1. lambda$lift$0 : replaced return value with Optional.empty for com/pivovarit/function/ThrowingFunction::lambda$lift$0 → KILLED
                return Optional.ofNullable(apply(t));
111
            } catch (final Exception e) {
112
                return Optional.empty();
113
            }
114
        };
115
    }
116
}

Mutations

55

1.1
Location : lifted
Killed by : com.pivovarit.function.ThrowingFunctionTest.[engine:junit-jupiter]/[class:com.pivovarit.function.ThrowingFunctionTest]/[method:shouldWrapInOptionalWhenUsingStandardUtilsFunctions()]
replaced return value with null for com/pivovarit/function/ThrowingFunction::lifted → KILLED

57

1.1
Location : lambda$lifted$0
Killed by : none
replaced return value with Optional.empty for com/pivovarit/function/ThrowingFunction::lambda$lifted$0 → NO_COVERAGE

74

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

76

1.1
Location : lambda$unchecked$0
Killed by : com.pivovarit.function.ThrowingFunctionTest.[engine:junit-jupiter]/[class:com.pivovarit.function.ThrowingFunctionTest]/[method:shouldApplyWhenNoExceptionThrownWithUnchecked()]
replaced return value with null for com/pivovarit/function/ThrowingFunction::lambda$unchecked$0 → KILLED

93

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

95

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

97

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

108

1.1
Location : lift
Killed by : com.pivovarit.function.ThrowingFunctionTest.[engine:junit-jupiter]/[class:com.pivovarit.function.ThrowingFunctionTest]/[method:shouldReturnOptional()]
replaced return value with null for com/pivovarit/function/ThrowingFunction::lift → KILLED

110

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

Active mutators

Tests examined


Report generated by PIT 1.30.0 support