Module io.jooby
Package io.jooby

Class SneakyThrows

java.lang.Object
io.jooby.SneakyThrows

public final class SneakyThrows extends Object
Collection of throwable interfaces to simplify exception handling on lambdas.

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
  • Method Details

    • throwingPredicate

      public static <V> SneakyThrows.Predicate<V> throwingPredicate(SneakyThrows.Predicate<V> predicate)
      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

      public static SneakyThrows.Runnable throwingRunnable(SneakyThrows.Runnable action)
      Factory method for SneakyThrows.Runnable.
      Parameters:
      action - Runnable.
      Returns:
      Same runnable.
    • throwingSupplier

      public static <V> SneakyThrows.Supplier<V> throwingSupplier(SneakyThrows.Supplier<V> fn)
      Factory method for SneakyThrows.Supplier.
      Type Parameters:
      V - Resulting value.
      Parameters:
      fn - Supplier.
      Returns:
      Same supplier.
    • throwingFunction

      public static <V, R> SneakyThrows.Function<V,R> throwingFunction(SneakyThrows.Function<V,R> fn)
      Factory method for SneakyThrows.Function and Function.
      Type Parameters:
      V - Input value.
      R - Result value.
      Parameters:
      fn - Function.
      Returns:
      Same supplier.
    • throwingFunction

      public static <V1, V2, R> SneakyThrows.Function2<V1,V2,R> throwingFunction(SneakyThrows.Function2<V1,V2,R> fn)
      Factory method for SneakyThrows.Function2 and BiFunction.
      Type Parameters:
      V1 - Input value.
      V2 - Input value.
      R - Result value.
      Parameters:
      fn - Function.
      Returns:
      Same supplier.
    • throwingFunction

      public static <V1, V2, V3, R> SneakyThrows.Function3<V1,V2,V3,R> throwingFunction(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, V3, V4, R> SneakyThrows.Function4<V1,V2,V3,V4,R> throwingFunction(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, 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.
      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, 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.
      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, 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.
      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, 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.
      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

      public static <V> SneakyThrows.Consumer<V> throwingConsumer(SneakyThrows.Consumer<V> action)
      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, V3> SneakyThrows.Consumer3<V1,V2,V3> throwingConsumer(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, V3, V4> SneakyThrows.Consumer4<V1,V2,V3,V4> throwingConsumer(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, V3, V4, V5> SneakyThrows.Consumer5<V1,V2,V3,V4,V5> throwingConsumer(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, 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.
      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, 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.
      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, 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.
      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

      @NonNull public static RuntimeException propagate(Throwable x)
      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 the throw statement to avoid compiler errors regarding no return statement and similar problems. This method won't of course return an actual RuntimeException - 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

      public static boolean isFatal(Throwable x)
      True if the given exception is one of InterruptedException, LinkageError, ThreadDeath, VirtualMachineError.
      Parameters:
      x - Exception to test.
      Returns:
      True if the given exception is one of InterruptedException, LinkageError, ThreadDeath, VirtualMachineError.