Interface SneakyIntSupplier<X extends Exception>

  • Type Parameters:
    X - the type of exception that might be thrown during execution of the functional method of this interface
    Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @FunctionalInterface
    public interface SneakyIntSupplier<X extends Exception>
    Represents a supplier of int-valued results. This is the int-producing primitive specialization of SneakySupplier.

    There is no requirement that a distinct result be returned each time the supplier is invoked.

    This is a functional interface whose functional method is getAsInt().

    • Method Detail

      • getAsInt

        int getAsInt()
              throws X extends Exception
        Gets a result.
        Returns:
        a result
        Throws:
        X - if an exception during execution of this method was thrown
        X extends Exception
      • sneaky

        static <X extends ExceptionIntSupplier sneaky​(SneakyIntSupplier<X> intSupplier)
        Wraps the passed sneaky functional interface (sneaky interface) into the analogous functional interface from the java.util.function package (original interface).

        1. Both the wrapped sneaky interface and the wrapping original interface have unary functional methods with identical method signatures and return types (if any). The only relevant difference between the unary functional methods of those interfaces is that the functional method of the sneaky interface has a throws clause with a denoted exception, while the functional method of the original interface doesn't have it.

          For example, compare these two functional method declarations:

             // Functional method of the original interface `Function`:
                R apply(T t);
          
             // Functional method of the sneaky interface `SneakyFunction`:
                R apply(T input) throws X;
             
        2. A throws clause in the unary functional method of the wrapped sneaky interface allows to develop elegant lambda expressions via disabling enforcement of checked exceptions handling. To achieve that, simply wrap implementation of a lambda expression into a static sneaky(...) method declared in the respective sneaky interface, as shown in the examples below.
        3. Example - Outside Streams
          1. Classical & unsightly approach:
               Function toURI = input -> {
                 try {
                     return new URI(input);
                 } catch (URISyntaxException exception) {
                     log.error("Unable to create a URI", exception);
                     return null;
                 }
               };
               URI uri = toURI.apply("google.com");
               
          2. Sneaky & elegant approach:
               SneakyFunction toURI = URI::new;
               Function toURIAdapter = sneaky(toURI);
               URI uri = toURIAdapter.apply("google.com");
               
        4. Example - In Streams
          1. Classical & unsightly approach:
               List rawURIs = List.of("google.com", "ciechanowiec.eu");
               List pureURIs = rawURIs.stream()
                                      .map(rawURI -> {
                                          try {
                                              return new URI(rawURI);
                                          } catch (URISyntaxException exception) {
                                              log.error("Unable to create a URI", exception);
                                              return null;
                                          }
                                      })
                                      .toList();
               
          2. Sneaky & elegant approach:
               List rawURIs = List.of("google.com", "ciechanowiec.eu");
               List pureURIs = rawURIs.stream()
                                      .map(sneaky(URI::new))
                                      .toList();
               
        5. For details see documentation: https://github.com/ciechanowiec/sneakyfun.
        Type Parameters:
        X - the type of exception that might be thrown during execution of the functional method of this interface
        Parameters:
        intSupplier - sneaky functional interface that will be wrapped into the analogous original functional interface from the java.util.function package
        Returns:
        original functional interface from the java.util.function package wrapping the passed sneaky functional interface