| 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.BiPredicate; | |
| 20 | ||
| 21 | import static java.util.Objects.requireNonNull; | |
| 22 | ||
| 23 | /** | |
| 24 | * Represents a predicate (boolean-valued function) of two arguments. This is | |
| 25 | * the two-arity specialization of {@link ThrowingPredicate}. | |
| 26 | * Function may throw a checked exception. | |
| 27 | * | |
| 28 | * @param <T> the type of the first argument to the predicate | |
| 29 | * @param <U> the type of the second argument to the predicate | |
| 30 | * @param <E> the type of the thrown checked exception | |
| 31 | * | |
| 32 | * @author Grzegorz Piwowarek | |
| 33 | */ | |
| 34 | @FunctionalInterface | |
| 35 | public interface ThrowingBiPredicate<T, U, E extends Exception> { | |
| 36 | /** | |
| 37 | * Evaluates this predicate on the given arguments. | |
| 38 | * | |
| 39 | * @param t the first input argument | |
| 40 | * @param u the second input argument | |
| 41 | * @return {@code true} if the input arguments match the predicate, otherwise {@code false} | |
| 42 | * @throws E the checked exception type | |
| 43 | */ | |
| 44 | boolean test(T t, U u) throws E; | |
| 45 | ||
| 46 | /** | |
| 47 | * Returns a new BiPredicate instance which wraps the thrown checked exception instance into a {@link CheckedException} | |
| 48 | * | |
| 49 | * @param <T> the type of the first argument to the predicate | |
| 50 | * @param <U> the type of the second argument to the predicate | |
| 51 | * @param predicate the ThrowingBiPredicate to wrap | |
| 52 | * @return BiPredicate instance that wraps the checked exception into a {@link CheckedException} | |
| 53 | */ | |
| 54 | static <T, U> BiPredicate<T, U> unchecked(ThrowingBiPredicate<? super T, ? super U, ?> predicate) { | |
| 55 | requireNonNull(predicate); | |
| 56 |
1
1. unchecked : replaced return value with null for com/pivovarit/function/ThrowingBiPredicate::unchecked → KILLED |
return (arg1, arg2) -> { |
| 57 | try { | |
| 58 |
2
1. lambda$unchecked$0 : replaced boolean return with true for com/pivovarit/function/ThrowingBiPredicate::lambda$unchecked$0 → SURVIVED 2. lambda$unchecked$0 : replaced boolean return with false for com/pivovarit/function/ThrowingBiPredicate::lambda$unchecked$0 → KILLED |
return predicate.test(arg1, arg2); |
| 59 | } catch (final Exception e) { | |
| 60 | throw new CheckedException(e); | |
| 61 | } | |
| 62 | }; | |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Returns a new BiPredicate instance which rethrows the checked exception using the Sneaky Throws pattern | |
| 67 | * | |
| 68 | * @param <T> the type of the first argument to the predicate | |
| 69 | * @param <U> the type of the second argument to the predicate | |
| 70 | * @param predicate the ThrowingBiPredicate to wrap | |
| 71 | * @return BiPredicate instance that rethrows the checked exception using the Sneaky Throws pattern | |
| 72 | */ | |
| 73 | static <T, U> BiPredicate<T, U> sneaky(ThrowingBiPredicate<? super T, ? super U, ?> predicate) { | |
| 74 | Objects.requireNonNull(predicate); | |
| 75 |
1
1. sneaky : replaced return value with null for com/pivovarit/function/ThrowingBiPredicate::sneaky → KILLED |
return (t, u) -> { |
| 76 | try { | |
| 77 |
2
1. lambda$sneaky$0 : replaced boolean return with false for com/pivovarit/function/ThrowingBiPredicate::lambda$sneaky$0 → NO_COVERAGE 2. lambda$sneaky$0 : replaced boolean return with true for com/pivovarit/function/ThrowingBiPredicate::lambda$sneaky$0 → NO_COVERAGE |
return predicate.test(t, u); |
| 78 | } catch (Exception e) { | |
| 79 |
2
1. lambda$sneaky$0 : replaced boolean return with false for com/pivovarit/function/ThrowingBiPredicate::lambda$sneaky$0 → NO_COVERAGE 2. lambda$sneaky$0 : replaced boolean return with true for com/pivovarit/function/ThrowingBiPredicate::lambda$sneaky$0 → NO_COVERAGE |
return SneakyThrowUtil.sneakyThrow(e); |
| 80 | } | |
| 81 | }; | |
| 82 | } | |
| 83 | } | |
Mutations | ||
| 56 |
1.1 |
|
| 58 |
1.1 2.2 |
|
| 75 |
1.1 |
|
| 77 |
1.1 2.2 |
|
| 79 |
1.1 2.2 |