Skip to content

Commit c628e8b

Browse files
committed
Removed invokeAndUnwrap methods
These would require code to be wrapped in a method or lambda, where try-catch combined with UncheckedException.throwCauseAs is usually just as easy
1 parent 3507918 commit c628e8b

88 files changed

Lines changed: 440 additions & 4211 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/com/github/robtimus/function/throwing/ThrowingBiConsumer.java

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -292,34 +292,16 @@ static <T, U, X extends Throwable> ThrowingBiConsumer<T, U, X> checked(BiConsume
292292
static <T, U, X extends Throwable> ThrowingBiConsumer<T, U, X> checked(BiConsumer<? super T, ? super U> operation, Class<X> errorType) {
293293
Objects.requireNonNull(operation);
294294
Objects.requireNonNull(errorType);
295-
return (t, u) -> invokeAndUnwrap(operation, t, u, errorType);
296-
}
297-
298-
/**
299-
* Invokes an operation, unwrapping any {@link UncheckedException} that is thrown if its cause if an instance of {@code errorType}.
300-
*
301-
* @param <T> The type of the first argument to the operation.
302-
* @param <U> The type of the second argument to the operation.
303-
* @param <X> The type of checked exception that can be thrown.
304-
* @param operation The operation to invoke.
305-
* @param input1 The first input to the operation.
306-
* @param input2 The second input to the operation.
307-
* @param errorType The type of checked exception that can be thrown.
308-
* @throws NullPointerException If {@code operation} or {@code errorType} is {@code null}.
309-
* @throws X If {@code operation} throws an {@link UncheckedException} that wraps an instance of {@code errorType}.
310-
*/
311-
static <T, U, X extends Throwable> void invokeAndUnwrap(BiConsumer<? super T, ? super U> operation, T input1, U input2, Class<X> errorType)
312-
throws X {
313-
314-
Objects.requireNonNull(errorType);
315-
try {
316-
operation.accept(input1, input2);
317-
} catch (UncheckedException e) {
318-
Throwable cause = e.getCause();
319-
if (errorType.isInstance(cause)) {
320-
throw errorType.cast(cause);
295+
return (t, u) -> {
296+
try {
297+
operation.accept(t, u);
298+
} catch (UncheckedException e) {
299+
Throwable cause = e.getCause();
300+
if (errorType.isInstance(cause)) {
301+
throw errorType.cast(cause);
302+
}
303+
throw e;
321304
}
322-
throw e;
323-
}
305+
};
324306
}
325307
}

src/main/java/com/github/robtimus/function/throwing/ThrowingBiFunction.java

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -344,35 +344,16 @@ static <T, U, R, X extends Throwable> ThrowingBiFunction<T, U, R, X> checked(BiF
344344
Class<X> errorType) {
345345
Objects.requireNonNull(function);
346346
Objects.requireNonNull(errorType);
347-
return (t, u) -> invokeAndUnwrap(function, t, u, errorType);
348-
}
349-
350-
/**
351-
* Invokes a function, unwrapping any {@link UncheckedException} that is thrown if its cause if an instance of {@code errorType}.
352-
*
353-
* @param <T> The type of the first argument to the function.
354-
* @param <U> The type of the second argument to the function.
355-
* @param <R> The type of the result of the function.
356-
* @param <X> The type of checked exception that can be thrown.
357-
* @param function The function to invoke.
358-
* @param input1 The first input to the function.
359-
* @param input2 The second input to the function.
360-
* @param errorType The type of checked exception that can be thrown.
361-
* @return The result of invoking {@code function}.
362-
* @throws NullPointerException If {@code function} or {@code errorType} is {@code null}.
363-
* @throws X If {@code function} throws an {@link UncheckedException} that wraps an instance of {@code errorType}.
364-
*/
365-
static <T, U, R, X extends Throwable> R invokeAndUnwrap(BiFunction<? super T, ? super U, ? extends R> function, T input1, U input2,
366-
Class<X> errorType) throws X {
367-
Objects.requireNonNull(errorType);
368-
try {
369-
return function.apply(input1, input2);
370-
} catch (UncheckedException e) {
371-
Throwable cause = e.getCause();
372-
if (errorType.isInstance(cause)) {
373-
throw errorType.cast(cause);
347+
return (t, u) -> {
348+
try {
349+
return function.apply(t, u);
350+
} catch (UncheckedException e) {
351+
Throwable cause = e.getCause();
352+
if (errorType.isInstance(cause)) {
353+
throw errorType.cast(cause);
354+
}
355+
throw e;
374356
}
375-
throw e;
376-
}
357+
};
377358
}
378359
}

src/main/java/com/github/robtimus/function/throwing/ThrowingBiPredicate.java

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -379,35 +379,16 @@ static <T, U, X extends Throwable> ThrowingBiPredicate<T, U, X> checked(BiPredic
379379
static <T, U, X extends Throwable> ThrowingBiPredicate<T, U, X> checked(BiPredicate<? super T, ? super U> predicate, Class<X> errorType) {
380380
Objects.requireNonNull(predicate);
381381
Objects.requireNonNull(errorType);
382-
return (t, u) -> invokeAndUnwrap(predicate, t, u, errorType);
383-
}
384-
385-
/**
386-
* Invokes a predicate, unwrapping any {@link UncheckedException} that is thrown if its cause if an instance of {@code errorType}.
387-
*
388-
* @param <T> The type of the first argument to the predicate.
389-
* @param <U> The type of the second argument the predicate.
390-
* @param <X> The type of checked exception that can be thrown.
391-
* @param predicate The predicate to invoke.
392-
* @param input1 The first input to the predicate.
393-
* @param input2 The second input to the predicate.
394-
* @param errorType The type of checked exception that can be thrown.
395-
* @return The result of invoking {@code predicate}.
396-
* @throws NullPointerException If {@code predicate} or {@code errorType} is {@code null}.
397-
* @throws X If {@code predicate} throws an {@link UncheckedException} that wraps an instance of {@code errorType}.
398-
*/
399-
static <T, U, X extends Throwable> boolean invokeAndUnwrap(BiPredicate<? super T, ? super U> predicate, T input1, U input2, Class<X> errorType)
400-
throws X {
401-
402-
Objects.requireNonNull(errorType);
403-
try {
404-
return predicate.test(input1, input2);
405-
} catch (UncheckedException e) {
406-
Throwable cause = e.getCause();
407-
if (errorType.isInstance(cause)) {
408-
throw errorType.cast(cause);
382+
return (t, u) -> {
383+
try {
384+
return predicate.test(t, u);
385+
} catch (UncheckedException e) {
386+
Throwable cause = e.getCause();
387+
if (errorType.isInstance(cause)) {
388+
throw errorType.cast(cause);
389+
}
390+
throw e;
409391
}
410-
throw e;
411-
}
392+
};
412393
}
413394
}

src/main/java/com/github/robtimus/function/throwing/ThrowingBinaryOperator.java

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -238,32 +238,16 @@ static <T, X extends Throwable> ThrowingBinaryOperator<T, X> checked(BinaryOpera
238238
static <T, X extends Throwable> ThrowingBinaryOperator<T, X> checked(BinaryOperator<T> operator, Class<X> errorType) {
239239
Objects.requireNonNull(operator);
240240
Objects.requireNonNull(errorType);
241-
return (t, u) -> invokeAndUnwrap(operator, t, u, errorType);
242-
}
243-
244-
/**
245-
* Invokes a binary operator, unwrapping any {@link UncheckedException} that is thrown if its cause if an instance of {@code errorType}.
246-
*
247-
* @param <T> The type of the operands and result of the operator.
248-
* @param <X> The type of checked exception that can be thrown.
249-
* @param operator The binary operator to invoke.
250-
* @param input1 The first input to the binary operator.
251-
* @param input2 The second input to the binary operator.
252-
* @param errorType The type of checked exception that can be thrown.
253-
* @return The result of invoking {@code operator}.
254-
* @throws NullPointerException If {@code operator} or {@code errorType} is {@code null}.
255-
* @throws X If {@code operator} throws an {@link UncheckedException} that wraps an instance of {@code errorType}.
256-
*/
257-
static <T, X extends Throwable> T invokeAndUnwrap(BinaryOperator<T> operator, T input1, T input2, Class<X> errorType) throws X {
258-
Objects.requireNonNull(errorType);
259-
try {
260-
return operator.apply(input1, input2);
261-
} catch (UncheckedException e) {
262-
Throwable cause = e.getCause();
263-
if (errorType.isInstance(cause)) {
264-
throw errorType.cast(cause);
241+
return (t, u) -> {
242+
try {
243+
return operator.apply(t, u);
244+
} catch (UncheckedException e) {
245+
Throwable cause = e.getCause();
246+
if (errorType.isInstance(cause)) {
247+
throw errorType.cast(cause);
248+
}
249+
throw e;
265250
}
266-
throw e;
267-
}
251+
};
268252
}
269253
}

src/main/java/com/github/robtimus/function/throwing/ThrowingBooleanSupplier.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -264,29 +264,16 @@ static <X extends Throwable> ThrowingBooleanSupplier<X> checked(BooleanSupplier
264264
static <X extends Throwable> ThrowingBooleanSupplier<X> checked(BooleanSupplier supplier, Class<X> errorType) {
265265
Objects.requireNonNull(supplier);
266266
Objects.requireNonNull(errorType);
267-
return () -> invokeAndUnwrap(supplier, errorType);
268-
}
269-
270-
/**
271-
* Invokes a supplier, unwrapping any {@link UncheckedException} that is thrown if its cause if an instance of {@code errorType}.
272-
*
273-
* @param <X> The type of checked exception that can be thrown.
274-
* @param supplier The supplier to invoke.
275-
* @param errorType The type of checked exception that can be thrown.
276-
* @return The result of invoking {@code supplier}.
277-
* @throws NullPointerException If {@code supplier} or {@code errorType} is {@code null}.
278-
* @throws X If {@code supplier} throws an {@link UncheckedException} that wraps an instance of {@code errorType}.
279-
*/
280-
static <X extends Throwable> boolean invokeAndUnwrap(BooleanSupplier supplier, Class<X> errorType) throws X {
281-
Objects.requireNonNull(errorType);
282-
try {
283-
return supplier.getAsBoolean();
284-
} catch (UncheckedException e) {
285-
Throwable cause = e.getCause();
286-
if (errorType.isInstance(cause)) {
287-
throw errorType.cast(cause);
267+
return () -> {
268+
try {
269+
return supplier.getAsBoolean();
270+
} catch (UncheckedException e) {
271+
Throwable cause = e.getCause();
272+
if (errorType.isInstance(cause)) {
273+
throw errorType.cast(cause);
274+
}
275+
throw e;
288276
}
289-
throw e;
290-
}
277+
};
291278
}
292279
}

src/main/java/com/github/robtimus/function/throwing/ThrowingConsumer.java

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -285,30 +285,16 @@ static <T, X extends Throwable> ThrowingConsumer<T, X> checked(Consumer<? super
285285
static <T, X extends Throwable> ThrowingConsumer<T, X> checked(Consumer<? super T> operation, Class<X> errorType) {
286286
Objects.requireNonNull(operation);
287287
Objects.requireNonNull(errorType);
288-
return t -> invokeAndUnwrap(operation, t, errorType);
289-
}
290-
291-
/**
292-
* Invokes an operation, unwrapping any {@link UncheckedException} that is thrown if its cause if an instance of {@code errorType}.
293-
*
294-
* @param <T> The type of the input to the operation.
295-
* @param <X> The type of checked exception that can be thrown.
296-
* @param operation The operation to invoke.
297-
* @param input The input to the operation.
298-
* @param errorType The type of checked exception that can be thrown.
299-
* @throws NullPointerException If {@code operation} or {@code errorType} is {@code null}.
300-
* @throws X If {@code operation} throws an {@link UncheckedException} that wraps an instance of {@code errorType}.
301-
*/
302-
static <T, X extends Throwable> void invokeAndUnwrap(Consumer<? super T> operation, T input, Class<X> errorType) throws X {
303-
Objects.requireNonNull(errorType);
304-
try {
305-
operation.accept(input);
306-
} catch (UncheckedException e) {
307-
Throwable cause = e.getCause();
308-
if (errorType.isInstance(cause)) {
309-
throw errorType.cast(cause);
288+
return t -> {
289+
try {
290+
operation.accept(t);
291+
} catch (UncheckedException e) {
292+
Throwable cause = e.getCause();
293+
if (errorType.isInstance(cause)) {
294+
throw errorType.cast(cause);
295+
}
296+
throw e;
310297
}
311-
throw e;
312-
}
298+
};
313299
}
314300
}

src/main/java/com/github/robtimus/function/throwing/ThrowingDoubleBinaryOperator.java

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -313,31 +313,16 @@ static <X extends Throwable> ThrowingDoubleBinaryOperator<X> checked(DoubleBinar
313313
static <X extends Throwable> ThrowingDoubleBinaryOperator<X> checked(DoubleBinaryOperator operator, Class<X> errorType) {
314314
Objects.requireNonNull(operator);
315315
Objects.requireNonNull(errorType);
316-
return (t, u) -> invokeAndUnwrap(operator, t, u, errorType);
317-
}
318-
319-
/**
320-
* Invokes a binary operator, unwrapping any {@link UncheckedException} that is thrown if its cause if an instance of {@code errorType}.
321-
*
322-
* @param <X> The type of checked exception that can be thrown.
323-
* @param operator The operator to invoke.
324-
* @param input1 The first input to the operator.
325-
* @param input2 The second input to the operator.
326-
* @param errorType The type of checked exception that can be thrown.
327-
* @return The result of invoking {@code operator}.
328-
* @throws NullPointerException If {@code operator} or {@code errorType} is {@code null}.
329-
* @throws X If {@code operator} throws an {@link UncheckedException} that wraps an instance of {@code errorType}.
330-
*/
331-
static <X extends Throwable> double invokeAndUnwrap(DoubleBinaryOperator operator, double input1, double input2, Class<X> errorType) throws X {
332-
Objects.requireNonNull(errorType);
333-
try {
334-
return operator.applyAsDouble(input1, input2);
335-
} catch (UncheckedException e) {
336-
Throwable cause = e.getCause();
337-
if (errorType.isInstance(cause)) {
338-
throw errorType.cast(cause);
316+
return (t, u) -> {
317+
try {
318+
return operator.applyAsDouble(t, u);
319+
} catch (UncheckedException e) {
320+
Throwable cause = e.getCause();
321+
if (errorType.isInstance(cause)) {
322+
throw errorType.cast(cause);
323+
}
324+
throw e;
339325
}
340-
throw e;
341-
}
326+
};
342327
}
343328
}

src/main/java/com/github/robtimus/function/throwing/ThrowingDoubleConsumer.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -280,29 +280,16 @@ static <X extends Throwable> ThrowingDoubleConsumer<X> checked(DoubleConsumer op
280280
static <X extends Throwable> ThrowingDoubleConsumer<X> checked(DoubleConsumer operation, Class<X> errorType) {
281281
Objects.requireNonNull(operation);
282282
Objects.requireNonNull(errorType);
283-
return t -> invokeAndUnwrap(operation, t, errorType);
284-
}
285-
286-
/**
287-
* Invokes an operation, unwrapping any {@link UncheckedException} that is thrown if its cause if an instance of {@code errorType}.
288-
*
289-
* @param <X> The type of checked exception that can be thrown.
290-
* @param operation The operation to invoke.
291-
* @param input The input to the operation.
292-
* @param errorType The type of checked exception that can be thrown.
293-
* @throws NullPointerException If {@code operation} or {@code errorType} is {@code null}.
294-
* @throws X If {@code operation} throws an {@link UncheckedException} that wraps an instance of {@code errorType}.
295-
*/
296-
static <X extends Throwable> void invokeAndUnwrap(DoubleConsumer operation, double input, Class<X> errorType) throws X {
297-
Objects.requireNonNull(errorType);
298-
try {
299-
operation.accept(input);
300-
} catch (UncheckedException e) {
301-
Throwable cause = e.getCause();
302-
if (errorType.isInstance(cause)) {
303-
throw errorType.cast(cause);
283+
return t -> {
284+
try {
285+
operation.accept(t);
286+
} catch (UncheckedException e) {
287+
Throwable cause = e.getCause();
288+
if (errorType.isInstance(cause)) {
289+
throw errorType.cast(cause);
290+
}
291+
throw e;
304292
}
305-
throw e;
306-
}
293+
};
307294
}
308295
}

src/main/java/com/github/robtimus/function/throwing/ThrowingDoubleFunction.java

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -317,31 +317,16 @@ static <R, X extends Throwable> ThrowingDoubleFunction<R, X> checked(DoubleFunct
317317
static <R, X extends Throwable> ThrowingDoubleFunction<R, X> checked(DoubleFunction<? extends R> function, Class<X> errorType) {
318318
Objects.requireNonNull(function);
319319
Objects.requireNonNull(errorType);
320-
return t -> invokeAndUnwrap(function, t, errorType);
321-
}
322-
323-
/**
324-
* Invokes a function, unwrapping any {@link UncheckedException} that is thrown if its cause if an instance of {@code errorType}.
325-
*
326-
* @param <R> The type of the result of the function.
327-
* @param <X> The type of checked exception that can be thrown.
328-
* @param function The function to invoke.
329-
* @param input The input to the function.
330-
* @param errorType The type of checked exception that can be thrown.
331-
* @return The result of invoking {@code function}.
332-
* @throws NullPointerException If {@code function} or {@code errorType} is {@code null}.
333-
* @throws X If {@code function} throws an {@link UncheckedException} that wraps an instance of {@code errorType}.
334-
*/
335-
static <R, X extends Throwable> R invokeAndUnwrap(DoubleFunction<? extends R> function, double input, Class<X> errorType) throws X {
336-
Objects.requireNonNull(errorType);
337-
try {
338-
return function.apply(input);
339-
} catch (UncheckedException e) {
340-
Throwable cause = e.getCause();
341-
if (errorType.isInstance(cause)) {
342-
throw errorType.cast(cause);
320+
return t -> {
321+
try {
322+
return function.apply(t);
323+
} catch (UncheckedException e) {
324+
Throwable cause = e.getCause();
325+
if (errorType.isInstance(cause)) {
326+
throw errorType.cast(cause);
327+
}
328+
throw e;
343329
}
344-
throw e;
345-
}
330+
};
346331
}
347332
}

0 commit comments

Comments
 (0)