We do provide throwable and 100% compatible implementation of Function
, Consumer
, Runnable
,
Supplier
, Predicate
and BiPredicate
.
Examples:
interface Query {
Item findById(String id) throws IOException;
}
Query query = ...
List<Item> items = Arrays.asList("1", "2", "3")
.stream()
.map(throwingFunction(query::findById))
.collect(Collectors.toList());
- Author:
- edgar
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Throwable version ofConsumer
.static interface
Two argument version ofSneakyThrows.Consumer
.static interface
Three argument version ofSneakyThrows.Consumer
.static interface
Four argument version ofSneakyThrows.Consumer
.static interface
Five argument version ofSneakyThrows.Consumer
.static interface
Six argument version ofSneakyThrows.Consumer
.static interface
Seven argument version ofSneakyThrows.Consumer
.static interface
Seven argument version ofSneakyThrows.Consumer
.static interface
Throwable version ofFunction
.static interface
Throwable version ofBiFunction
.static interface
Function with three arguments.static interface
Function with four arguments.static interface
Function with five arguments.static interface
Function with six arguments.static interface
Function with seven arguments.static interface
Function with seven arguments.static interface
Throwable version ofSneakyThrows.Predicate
.static interface
Throwable version ofSneakyThrows.Predicate
.static interface
Throwable version ofRunnable
.static interface
Throwable version ofSupplier
. -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
True if the given exception is one ofInterruptedException
,LinkageError
,ThreadDeath
,VirtualMachineError
.static RuntimeException
Throws any throwable 'sneakily' - you don't need to catch it, nor declare that you throw it onwards.static <V> SneakyThrows.Consumer<V>
throwingConsumer
(SneakyThrows.Consumer<V> action) Factory method for throwing consumer.static <V1,
V2> SneakyThrows.Consumer2<V1, V2> throwingConsumer
(SneakyThrows.Consumer2<V1, V2> action) Factory method for throwing consumer.static <V1,
V2, V3> SneakyThrows.Consumer3<V1, V2, V3> throwingConsumer
(SneakyThrows.Consumer3<V1, V2, V3> action) Factory method for throwing consumer.static <V1,
V2, V3, V4>
SneakyThrows.Consumer4<V1,V2, V3, V4> throwingConsumer
(SneakyThrows.Consumer4<V1, V2, V3, V4> action) Factory method for throwing consumer.static <V1,
V2, V3, V4, V5>
SneakyThrows.Consumer5<V1,V2, V3, V4, V5> throwingConsumer
(SneakyThrows.Consumer5<V1, V2, V3, V4, V5> action) Factory method for throwing consumer.static <V1,
V2, V3, V4, V5, V6>
SneakyThrows.Consumer6<V1,V2, V3, V4, V5, V6> throwingConsumer
(SneakyThrows.Consumer6<V1, V2, V3, V4, V5, V6> action) Factory method for throwing consumer.static <V1,
V2, V3, V4, V5, V6, V7>
SneakyThrows.Consumer7<V1,V2, V3, V4, V5, V6, V7> throwingConsumer
(SneakyThrows.Consumer7<V1, V2, V3, V4, V5, V6, V7> action) Factory method for throwing consumer.static <V1,
V2, V3, V4, V5, V6, V7, V8>
SneakyThrows.Consumer8<V1,V2, V3, V4, V5, V6, V7, V8> throwingConsumer
(SneakyThrows.Consumer8<V1, V2, V3, V4, V5, V6, V7, V8> action) Factory method for throwing consumer.static <V,
R> SneakyThrows.Function<V, R> throwingFunction
(SneakyThrows.Function<V, R> fn) Factory method forSneakyThrows.Function
andFunction
.static <V1,
V2, R> SneakyThrows.Function2<V1, V2, R> throwingFunction
(SneakyThrows.Function2<V1, V2, R> fn) Factory method forSneakyThrows.Function2
andBiFunction
.static <V1,
V2, V3, R>
SneakyThrows.Function3<V1,V2, V3, R> throwingFunction
(SneakyThrows.Function3<V1, V2, V3, R> fn) Factory method for throwing function.static <V1,
V2, V3, V4, R>
SneakyThrows.Function4<V1,V2, V3, V4, R> throwingFunction
(SneakyThrows.Function4<V1, V2, V3, V4, R> fn) Factory method for throwing function.static <V1,
V2, V3, V4, V5, R>
SneakyThrows.Function5<V1,V2, V3, V4, V5, R> throwingFunction
(SneakyThrows.Function5<V1, V2, V3, V4, V5, R> fn) Factory method for throwing function.static <V1,
V2, V3, V4, V5, V6, R>
SneakyThrows.Function6<V1,V2, V3, V4, V5, V6, R> throwingFunction
(SneakyThrows.Function6<V1, V2, V3, V4, V5, V6, R> fn) Factory method for throwing function.static <V1,
V2, V3, V4, V5, V6, V7, R>
SneakyThrows.Function7<V1,V2, V3, V4, V5, V6, V7, R> throwingFunction
(SneakyThrows.Function7<V1, V2, V3, V4, V5, V6, V7, R> fn) Factory method for throwing function.static <V1,
V2, V3, V4, V5, V6, V7, V8, R>
SneakyThrows.Function8<V1,V2, V3, V4, V5, V6, V7, V8, R> throwingFunction
(SneakyThrows.Function8<V1, V2, V3, V4, V5, V6, V7, V8, R> fn) Factory method for throwing function.static <V> SneakyThrows.Predicate<V>
throwingPredicate
(SneakyThrows.Predicate<V> predicate) Factory method for predicate.static <V1,
V2> SneakyThrows.Predicate2<V1, V2> throwingPredicate
(SneakyThrows.Predicate2<V1, V2> predicate) Factory method for predicate.static SneakyThrows.Runnable
Factory method forSneakyThrows.Runnable
.static <V> SneakyThrows.Supplier<V>
Factory method forSneakyThrows.Supplier
.
-
Method Details
-
throwingPredicate
Factory method for predicate.- Type Parameters:
V
- Type 1.- Parameters:
predicate
- Predicate.- Returns:
- A throwing predicate.
-
throwingPredicate
public static <V1,V2> SneakyThrows.Predicate2<V1,V2> throwingPredicate(SneakyThrows.Predicate2<V1, V2> predicate) Factory method for predicate.- Type Parameters:
V1
- Type 1.V2
- Type 2.- Parameters:
predicate
- Predicate.- Returns:
- A throwing predicate.
-
throwingRunnable
Factory method forSneakyThrows.Runnable
.- Parameters:
action
- Runnable.- Returns:
- Same runnable.
-
throwingSupplier
Factory method forSneakyThrows.Supplier
.- Type Parameters:
V
- Resulting value.- Parameters:
fn
- Supplier.- Returns:
- Same supplier.
-
throwingFunction
Factory method forSneakyThrows.Function
andFunction
.- Type Parameters:
V
- Input value.R
- Result value.- Parameters:
fn
- Function.- Returns:
- Same supplier.
-
throwingFunction
public static <V1,V2, SneakyThrows.Function2<V1,R> V2, throwingFunctionR> (SneakyThrows.Function2<V1, V2, R> fn) Factory method forSneakyThrows.Function2
andBiFunction
.- Type Parameters:
V1
- Input value.V2
- Input value.R
- Result value.- Parameters:
fn
- Function.- Returns:
- Same supplier.
-
throwingFunction
public static <V1,V2, SneakyThrows.Function3<V1,V3, R> V2, throwingFunctionV3, R> (SneakyThrows.Function3<V1, V2, V3, R> fn) Factory method for throwing function.- Type Parameters:
V1
- Type 1.V2
- Type 2.V3
- Type 3.R
- Return type.- Parameters:
fn
- SneakyThrows function.- Returns:
- SneakyThrows function.
-
throwingFunction
public static <V1,V2, SneakyThrows.Function4<V1,V3, V4, R> V2, throwingFunctionV3, V4, R> (SneakyThrows.Function4<V1, V2, V3, V4, R> fn) Factory method for throwing function.- Type Parameters:
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.R
- Return type.- Parameters:
fn
- SneakyThrows function.- Returns:
- SneakyThrows function.
-
throwingFunction
public static <V1,V2, SneakyThrows.Function5<V1,V3, V4, V5, R> V2, throwingFunctionV3, V4, V5, R> (SneakyThrows.Function5<V1, V2, V3, V4, V5, R> fn) Factory method for throwing function.- Type Parameters:
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.V5
- Type 5.R
- Return type.- Parameters:
fn
- SneakyThrows function.- Returns:
- SneakyThrows function.
-
throwingFunction
public static <V1,V2, SneakyThrows.Function6<V1,V3, V4, V5, V6, R> V2, throwingFunctionV3, V4, V5, V6, R> (SneakyThrows.Function6<V1, V2, V3, V4, V5, V6, R> fn) Factory method for throwing function.- Type Parameters:
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.V5
- Type 5.V6
- Type 6.R
- Return type.- Parameters:
fn
- SneakyThrows function.- Returns:
- SneakyThrows function.
-
throwingFunction
public static <V1,V2, SneakyThrows.Function7<V1,V3, V4, V5, V6, V7, R> V2, throwingFunctionV3, V4, V5, V6, V7, R> (SneakyThrows.Function7<V1, V2, V3, V4, V5, V6, V7, R> fn) Factory method for throwing function.- Type Parameters:
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.V5
- Type 5.V6
- Type 6.V7
- Type 7.R
- Return type.- Parameters:
fn
- SneakyThrows function.- Returns:
- SneakyThrows function.
-
throwingFunction
public static <V1,V2, SneakyThrows.Function8<V1,V3, V4, V5, V6, V7, V8, R> V2, throwingFunctionV3, V4, V5, V6, V7, V8, R> (SneakyThrows.Function8<V1, V2, V3, V4, V5, V6, V7, V8, R> fn) Factory method for throwing function.- Type Parameters:
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.V5
- Type 5.V6
- Type 6.V7
- Type 7.V8
- Type 8.R
- Return type.- Parameters:
fn
- SneakyThrows function.- Returns:
- SneakyThrows function.
-
throwingConsumer
Factory method for throwing consumer.- Type Parameters:
V
- Type 1.- Parameters:
action
- SneakyThrows consumer.- Returns:
- SneakyThrows consumer.
-
throwingConsumer
public static <V1,V2> SneakyThrows.Consumer2<V1,V2> throwingConsumer(SneakyThrows.Consumer2<V1, V2> action) Factory method for throwing consumer.- Type Parameters:
V1
- Type 1.V2
- Type 2.- Parameters:
action
- SneakyThrows consumer.- Returns:
- SneakyThrows consumer.
-
throwingConsumer
public static <V1,V2, SneakyThrows.Consumer3<V1,V3> V2, throwingConsumerV3> (SneakyThrows.Consumer3<V1, V2, V3> action) Factory method for throwing consumer.- Type Parameters:
V1
- Type 1.V2
- Type 2.V3
- Type 3.- Parameters:
action
- SneakyThrows consumer.- Returns:
- SneakyThrows consumer.
-
throwingConsumer
public static <V1,V2, SneakyThrows.Consumer4<V1,V3, V4> V2, throwingConsumerV3, V4> (SneakyThrows.Consumer4<V1, V2, V3, V4> action) Factory method for throwing consumer.- Type Parameters:
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.- Parameters:
action
- SneakyThrows consumer.- Returns:
- SneakyThrows consumer.
-
throwingConsumer
public static <V1,V2, SneakyThrows.Consumer5<V1,V3, V4, V5> V2, throwingConsumerV3, V4, V5> (SneakyThrows.Consumer5<V1, V2, V3, V4, V5> action) Factory method for throwing consumer.- Type Parameters:
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.V5
- Type 5.- Parameters:
action
- SneakyThrows consumer.- Returns:
- SneakyThrows consumer.
-
throwingConsumer
public static <V1,V2, SneakyThrows.Consumer6<V1,V3, V4, V5, V6> V2, throwingConsumerV3, V4, V5, V6> (SneakyThrows.Consumer6<V1, V2, V3, V4, V5, V6> action) Factory method for throwing consumer.- Type Parameters:
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.V5
- Type 5.V6
- Type 6.- Parameters:
action
- SneakyThrows consumer.- Returns:
- SneakyThrows consumer.
-
throwingConsumer
public static <V1,V2, SneakyThrows.Consumer7<V1,V3, V4, V5, V6, V7> V2, throwingConsumerV3, V4, V5, V6, V7> (SneakyThrows.Consumer7<V1, V2, V3, V4, V5, V6, V7> action) Factory method for throwing consumer.- Type Parameters:
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.V5
- Type 5.V6
- Type 6.V7
- Type 7.- Parameters:
action
- SneakyThrows consumer.- Returns:
- SneakyThrows consumer.
-
throwingConsumer
public static <V1,V2, SneakyThrows.Consumer8<V1,V3, V4, V5, V6, V7, V8> V2, throwingConsumerV3, V4, V5, V6, V7, V8> (SneakyThrows.Consumer8<V1, V2, V3, V4, V5, V6, V7, V8> action) Factory method for throwing consumer.- Type Parameters:
V1
- Type 1.V2
- Type 2.V3
- Type 3.V4
- Type 4.V5
- Type 5.V6
- Type 6.V7
- Type 7.V8
- Type 8.- Parameters:
action
- SneakyThrows consumer.- Returns:
- SneakyThrows consumer.
-
propagate
Throws any throwable 'sneakily' - you don't need to catch it, nor declare that you throw it onwards. The exception is still thrown - javac will just stop whining about it.Example usage:
public void run() { throw sneakyThrow(new IOException("You don't need to catch me!")); }
NB: The exception is not wrapped, ignored, swallowed, or redefined. The JVM actually does not know or care about the concept of a 'checked exception'. All this method does is hide the act of throwing a checked exception from the java compiler.
Note that this method has a return type of
RuntimeException
; it is advised you always call this method as argument to thethrow
statement to avoid compiler errors regarding no return statement and similar problems. This method won't of course return an actualRuntimeException
- it never returns, it always throws the provided exception.- Parameters:
x
- The throwable to throw without requiring you to catch its type.- Returns:
- A dummy RuntimeException; this method never returns normally, it always throws an exception!
-
isFatal
True if the given exception is one ofInterruptedException
,LinkageError
,ThreadDeath
,VirtualMachineError
.- Parameters:
x
- Exception to test.- Returns:
- True if the given exception is one of
InterruptedException
,LinkageError
,ThreadDeath
,VirtualMachineError
.
-