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

Mutations

50

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

52

1.1
Location : lambda$unchecked$0
Killed by : com.pivovarit.function.ThrowingToLongFunctionTest.[engine:junit-jupiter]/[class:com.pivovarit.function.ThrowingToLongFunctionTest]/[method:mapToLongTest()]
replaced long return with 0 for com/pivovarit/function/ThrowingToLongFunction::lambda$unchecked$0 → KILLED

68

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

70

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

72

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

Active mutators

Tests examined


Report generated by PIT 1.30.0 support