ThrowingSupplier.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.Supplier;
20
21
import static java.util.Objects.requireNonNull;
22
23
/**
24
 * Represents a function that accepts zero arguments and returns some value.
25
 * Function might throw a checked exception instance.
26
 *
27
 * @param <T> the type of the output to the function
28
 * @param <E> the type of the thrown checked exception
29
 * @author Grzegorz Piwowarek
30
 */
31
@FunctionalInterface
32
public interface ThrowingSupplier<T, E extends Exception> {
33
    /**
34
     * Gets a result.
35
     *
36
     * @return a result
37
     * @throws E the checked exception type
38
     */
39
    T get() throws E;
40
41
    /**
42
     * Returns a new Supplier instance which wraps the thrown checked exception instance into a {@link CheckedException}
43
     *
44
     * @param <T>      the type of results supplied by this supplier
45
     * @param supplier the ThrowingSupplier to wrap
46
     * @return Supplier instance that wraps the checked exception into a {@link CheckedException}
47
     */
48
    static <T> Supplier<T> unchecked(ThrowingSupplier<? extends T, ?> supplier) {
49
        requireNonNull(supplier);
50 1 1. unchecked : replaced return value with null for com/pivovarit/function/ThrowingSupplier::unchecked → KILLED
        return () -> {
51
            try {
52 1 1. lambda$unchecked$0 : replaced return value with null for com/pivovarit/function/ThrowingSupplier::lambda$unchecked$0 → NO_COVERAGE
                return supplier.get();
53
            } catch (final Exception e) {
54
                throw new CheckedException(e);
55
            }
56
        };
57
    }
58
59
    /**
60
     * Returns a new Supplier instance which returns the result as an Optional, or an empty Optional in case of a thrown exception
61
     *
62
     * @param <T>      the type of results supplied by this supplier
63
     * @param supplier the ThrowingSupplier to wrap
64
     * @return Supplier instance that returns the result as an Optional, or empty Optional in case of a thrown exception
65
     */
66
    static <T> Supplier<Optional<T>> optional(ThrowingSupplier<? extends T, ?> supplier) {
67
        requireNonNull(supplier);
68 1 1. optional : replaced return value with null for com/pivovarit/function/ThrowingSupplier::optional → KILLED
        return () -> {
69
            try {
70 1 1. lambda$optional$0 : replaced return value with Optional.empty for com/pivovarit/function/ThrowingSupplier::lambda$optional$0 → KILLED
                return Optional.ofNullable(supplier.get());
71
            } catch (final Exception e) {
72
                return Optional.empty();
73
            }
74
        };
75
    }
76
77
    /**
78
     * Returns a new Supplier instance which rethrows the checked exception using the Sneaky Throws pattern
79
     *
80
     * @param <T>      the type of results supplied by this supplier
81
     * @param supplier the ThrowingSupplier to wrap
82
     * @return Supplier instance that rethrows the checked exception using the Sneaky Throws pattern
83
     */
84
    static <T> Supplier<T> sneaky(ThrowingSupplier<? extends T, ?> supplier) {
85
        requireNonNull(supplier);
86 1 1. sneaky : replaced return value with null for com/pivovarit/function/ThrowingSupplier::sneaky → KILLED
        return () -> {
87
            try {
88 1 1. lambda$sneaky$0 : replaced return value with null for com/pivovarit/function/ThrowingSupplier::lambda$sneaky$0 → NO_COVERAGE
                return supplier.get();
89
            } catch (final Exception ex) {
90 1 1. lambda$sneaky$0 : replaced return value with null for com/pivovarit/function/ThrowingSupplier::lambda$sneaky$0 → NO_COVERAGE
                return SneakyThrowUtil.sneakyThrow(ex);
91
            }
92
        };
93
    }
94
}

Mutations

50

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

52

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

68

1.1
Location : optional
Killed by : com.pivovarit.function.ThrowingSupplierTest.[engine:junit-jupiter]/[class:com.pivovarit.function.ThrowingSupplierTest]/[method:shouldReturnEmptyOptionalWhenUsingOptionalAndExceptionThrown()]
replaced return value with null for com/pivovarit/function/ThrowingSupplier::optional → KILLED

70

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

86

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

88

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

90

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

Active mutators

Tests examined


Report generated by PIT 1.30.0 support