ThrowingConsumer.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.Objects;
19
import java.util.function.Consumer;
20
21
import static java.util.Objects.requireNonNull;
22
23
/**
24
 * Represents a function that accepts one argument and does not return any value;
25
 * Function might throw a checked exception instance.
26
 *
27
 * @param <T> the type of the input to the function
28
 * @param <E> the type of the thrown checked exception
29
 *
30
 * @author Grzegorz Piwowarek
31
 */
32
@FunctionalInterface
33
public interface ThrowingConsumer<T, E extends Exception> {
34
35
    /**
36
     * Performs this operation on the given argument.
37
     *
38
     * @param t the input argument
39
     * @throws E the checked exception type
40
     */
41
    void accept(T t) throws E;
42
43
    /**
44
     * Returns a new Consumer instance which wraps the thrown checked exception instance into a {@link CheckedException}
45
     *
46
     * @param <T>      the type of the input to the operation
47
     * @param consumer the ThrowingConsumer to wrap
48
     * @return Consumer instance that wraps the checked exception into a {@link CheckedException}
49
     */
50
    static <T> Consumer<T> unchecked(ThrowingConsumer<? super T, ?> consumer) {
51
        requireNonNull(consumer);
52 1 1. unchecked : replaced return value with null for com/pivovarit/function/ThrowingConsumer::unchecked → KILLED
        return t -> {
53
            try {
54 1 1. lambda$unchecked$0 : removed call to com/pivovarit/function/ThrowingConsumer::accept → KILLED
                consumer.accept(t);
55
            } catch (final Exception e) {
56
                throw new CheckedException(e);
57
            }
58
        };
59
    }
60
61
    /**
62
     * Returns a new Consumer instance which rethrows the checked exception using the Sneaky Throws pattern
63
     *
64
     * @param <T>      the type of the input to the operation
65
     * @param consumer the ThrowingConsumer to wrap
66
     * @return Consumer instance that rethrows the checked exception using the Sneaky Throws pattern
67
     */
68
    static <T> Consumer<T> sneaky(ThrowingConsumer<? super T, ?> consumer) {
69
        Objects.requireNonNull(consumer);
70 1 1. sneaky : replaced return value with null for com/pivovarit/function/ThrowingConsumer::sneaky → KILLED
        return t -> {
71
            try {
72 1 1. lambda$sneaky$0 : removed call to com/pivovarit/function/ThrowingConsumer::accept → KILLED
                consumer.accept(t);
73
            } catch (Exception e) {
74
                SneakyThrowUtil.sneakyThrow(e);
75
            }
76
        };
77
    }
78
}

Mutations

52

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

54

1.1
Location : lambda$unchecked$0
Killed by : com.pivovarit.function.ThrowingConsumerTest.[engine:junit-jupiter]/[class:com.pivovarit.function.ThrowingConsumerTest]/[method:shouldConsumeAndThrowUnchecked()]
removed call to com/pivovarit/function/ThrowingConsumer::accept → KILLED

70

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

72

1.1
Location : lambda$sneaky$0
Killed by : com.pivovarit.function.ThrowingConsumerTest.[engine:junit-jupiter]/[class:com.pivovarit.function.ThrowingConsumerTest]/[method:shouldConsumeAndSneakyThrow()]
removed call to com/pivovarit/function/ThrowingConsumer::accept → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support