| 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.UnaryOperator; | |
| 19 | ||
| 20 | import static java.util.Objects.requireNonNull; | |
| 21 | ||
| 22 | /** | |
| 23 | * Represents an operation on a single operand that produces a result of the | |
| 24 | * same type as its operand. This is a specialization of {@code Function} for | |
| 25 | * the case where the operand and result are of the same type. | |
| 26 | * Function may throw a checked exception. | |
| 27 | * | |
| 28 | * @param <T> the type of the operand and result of the operator | |
| 29 | * @param <E> the type of the thrown checked exception | |
| 30 | * | |
| 31 | * @see ThrowingFunction | |
| 32 | * | |
| 33 | * @author Grzegorz Piwowarek | |
| 34 | */ | |
| 35 | @FunctionalInterface | |
| 36 | public interface ThrowingUnaryOperator<T, E extends Exception> extends ThrowingFunction<T, T, E> { | |
| 37 | ||
| 38 | /** | |
| 39 | * Returns a new UnaryOperator instance which wraps the thrown checked exception instance into a {@link CheckedException} | |
| 40 | * | |
| 41 | * @param <T> the type of the operand and result of the operator | |
| 42 | * @param operator the ThrowingUnaryOperator to wrap | |
| 43 | * @return UnaryOperator instance that wraps the checked exception into a {@link CheckedException} | |
| 44 | */ | |
| 45 | static <T> UnaryOperator<T> unchecked(ThrowingUnaryOperator<T, ?> operator) { | |
| 46 | requireNonNull(operator); | |
| 47 |
1
1. unchecked : replaced return value with null for com/pivovarit/function/ThrowingUnaryOperator::unchecked → KILLED |
return t -> { |
| 48 | try { | |
| 49 |
1
1. lambda$unchecked$0 : replaced return value with null for com/pivovarit/function/ThrowingUnaryOperator::lambda$unchecked$0 → SURVIVED |
return operator.apply(t); |
| 50 | } catch (final Exception e) { | |
| 51 | throw new CheckedException(e); | |
| 52 | } | |
| 53 | }; | |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * Returns a new UnaryOperator instance which rethrows the checked exception using the Sneaky Throws pattern | |
| 58 | * | |
| 59 | * @param <T> the type of the operand and result of the operator | |
| 60 | * @param operator the ThrowingUnaryOperator to wrap | |
| 61 | * @return UnaryOperator instance that rethrows the checked exception using the Sneaky Throws pattern | |
| 62 | */ | |
| 63 | static <T> UnaryOperator<T> sneaky(ThrowingUnaryOperator<T, ?> operator) { | |
| 64 | requireNonNull(operator); | |
| 65 |
1
1. sneaky : replaced return value with null for com/pivovarit/function/ThrowingUnaryOperator::sneaky → KILLED |
return t -> { |
| 66 | try { | |
| 67 |
1
1. lambda$sneaky$0 : replaced return value with null for com/pivovarit/function/ThrowingUnaryOperator::lambda$sneaky$0 → NO_COVERAGE |
return operator.apply(t); |
| 68 | } catch (Exception e) { | |
| 69 |
1
1. lambda$sneaky$0 : replaced return value with null for com/pivovarit/function/ThrowingUnaryOperator::lambda$sneaky$0 → NO_COVERAGE |
return SneakyThrowUtil.sneakyThrow(e); |
| 70 | } | |
| 71 | }; | |
| 72 | } | |
| 73 | } | |
Mutations | ||
| 47 |
1.1 |
|
| 49 |
1.1 |
|
| 65 |
1.1 |
|
| 67 |
1.1 |
|
| 69 |
1.1 |