ThrowingPredicate.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.Predicate;
20
21
import static java.util.Objects.requireNonNull;
22
23
/**
24
 * Represents a function that accepts one argument and returns a boolean 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 ThrowingPredicate<T, E extends Exception> {
34
    /**
35
     * Evaluates this predicate on the given argument.
36
     *
37
     * @param t the input argument
38
     * @return {@code true} if the input argument matches the predicate, otherwise {@code false}
39
     * @throws E the checked exception type
40
     */
41
    boolean test(T t) throws E;
42
43
    /**
44
     * Returns a new Predicate instance which wraps the thrown checked exception instance into a {@link CheckedException}
45
     *
46
     * @param <T>       the type of the input to the predicate
47
     * @param predicate the ThrowingPredicate to wrap
48
     * @return Predicate instance that wraps the checked exception into a {@link CheckedException}
49
     */
50
    static <T> Predicate<T> unchecked(ThrowingPredicate<? super T, ?> predicate) {
51
        requireNonNull(predicate);
52 1 1. unchecked : replaced return value with null for com/pivovarit/function/ThrowingPredicate::unchecked → KILLED
        return t -> {
53
            try {
54 2 1. lambda$unchecked$0 : replaced boolean return with true for com/pivovarit/function/ThrowingPredicate::lambda$unchecked$0 → SURVIVED
2. lambda$unchecked$0 : replaced boolean return with false for com/pivovarit/function/ThrowingPredicate::lambda$unchecked$0 → KILLED
                return predicate.test(t);
55
            } catch (final Exception e) {
56
                throw new CheckedException(e);
57
            }
58
        };
59
    }
60
61
    /**
62
     * Returns a new Predicate instance which rethrows the checked exception using the Sneaky Throws pattern
63
     *
64
     * @param <T>       the type of the input to the predicate
65
     * @param predicate the ThrowingPredicate to wrap
66
     * @return Predicate instance that rethrows the checked exception using the Sneaky Throws pattern
67
     */
68
    static <T> Predicate<T> sneaky(ThrowingPredicate<? super T, ?> predicate) {
69
        Objects.requireNonNull(predicate);
70 1 1. sneaky : replaced return value with null for com/pivovarit/function/ThrowingPredicate::sneaky → KILLED
        return t -> {
71
            try {
72 2 1. lambda$sneaky$0 : replaced boolean return with false for com/pivovarit/function/ThrowingPredicate::lambda$sneaky$0 → NO_COVERAGE
2. lambda$sneaky$0 : replaced boolean return with true for com/pivovarit/function/ThrowingPredicate::lambda$sneaky$0 → NO_COVERAGE
                return predicate.test(t);
73
            } catch (Exception e) {
74 2 1. lambda$sneaky$0 : replaced boolean return with false for com/pivovarit/function/ThrowingPredicate::lambda$sneaky$0 → NO_COVERAGE
2. lambda$sneaky$0 : replaced boolean return with true for com/pivovarit/function/ThrowingPredicate::lambda$sneaky$0 → NO_COVERAGE
                return SneakyThrowUtil.sneakyThrow(e);
75
            }
76
        };
77
    }
78
}

Mutations

52

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

54

1.1
Location : lambda$unchecked$0
Killed by : none
replaced boolean return with true for com/pivovarit/function/ThrowingPredicate::lambda$unchecked$0 → SURVIVED
Covering tests

2.2
Location : lambda$unchecked$0
Killed by : com.pivovarit.function.ThrowingPredicateTest.[engine:junit-jupiter]/[class:com.pivovarit.function.ThrowingPredicateTest]/[method:shouldApplyWhenNoExceptionThrownWithUnchecked()]
replaced boolean return with false for com/pivovarit/function/ThrowingPredicate::lambda$unchecked$0 → KILLED

70

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

72

1.1
Location : lambda$sneaky$0
Killed by : none
replaced boolean return with false for com/pivovarit/function/ThrowingPredicate::lambda$sneaky$0 → NO_COVERAGE

2.2
Location : lambda$sneaky$0
Killed by : none
replaced boolean return with true for com/pivovarit/function/ThrowingPredicate::lambda$sneaky$0 → NO_COVERAGE

74

1.1
Location : lambda$sneaky$0
Killed by : none
replaced boolean return with false for com/pivovarit/function/ThrowingPredicate::lambda$sneaky$0 → NO_COVERAGE

2.2
Location : lambda$sneaky$0
Killed by : none
replaced boolean return with true for com/pivovarit/function/ThrowingPredicate::lambda$sneaky$0 → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.30.0 support