pivovarit / throwing-function

Checked Exceptions-enabled Java 8+ functional interfaces + adapters
Apache License 2.0
368 stars 60 forks source link

Improvement: Support for common primitive functions (int, long, double) #34

Open TobiasRoland opened 5 years ago

TobiasRoland commented 5 years ago

For better or worse,java.util.function package has specific classes for primitives. I think it would make sense to have "feature parity" with the java provided functions, but wanted to hear if this would be something you're willing to look at a PR for before I spend a bit of time writing it.

So a sort of simple example of what this change would allow:

IntStream.of(1,2,3) 
  .map(unchecked(this::takesIntThrowsChecked)));

DoubleStream.of(1.0, 2.0, 3.0) 
   .filter(unchecked(d -> {
       if (d > 2.0) throw Exception("Oh no!");
       return true;
   }));

LongStream.of(1,2,3)
.mapToObj(unchecked(x -> {
   if (x > 2) throw Exception("Oh no!")
   return "" + x;
}); 

Specifically I'd be looking to implement Throwing versions of:

I imagined doing something like this for each of the above:

interface ThrowingIntFunction<T extends Integer, R, E extends Exception> extends ThrowingFunction<T, R, E> {

    R apply(T arg) throws E;

    static <T, R> Function<T, Optional<R>> lifted(final ThrowingFunction<T, R, ?> f) {
        return requireNonNull(f).lift();
    }

    static <T, R> Function<T, R> unchecked(final ThrowingFunction<T, R, ?> f) {
        return requireNonNull(f).uncheck();
    }

    static <T1, R> Function<T1, R> sneaky(ThrowingFunction<? super T1, ? extends R, ?> function) {
        requireNonNull(function);
        return t -> {
            try {
                return function.apply(t);
            } catch (final Exception ex) {
                return SneakyThrowUtil.sneakyThrow(ex);
            }
        };
    }
}
pivovarit commented 5 years ago

That's a very good idea - targeting 2.0.0

pivovarit commented 4 years ago

...just trying to figure out how to come up with some generator for them - maintaining all of these by hand is not realistic

rwperrott commented 4 years ago

Indeed, that is plenty more work, which I'm trying for my own fork; not just refactor copy class and some interface renaming, but including subtle stuff like using Optional{primitive-name}, and some functional interfaces for primitives not having a super interface.

rwperrott commented 4 years ago

For me, https://kevinbirch.github.io/string-template-maven-plugin/ looks promising for Maven driven code generation; the plugin looks old, but it maybe possible to cause it to use a newer https://www.stringtemplate.org/ version, if required.

rwperrott commented 4 years ago

I implemented all of the primitive using functions in StringTemplates (*.st) files and many pom template reference entries at https://github.com/rwperrott/throwing-function using my fork of string-template-maven-plug at https://github.com/rwperrott/string-template-maven-plugin.

The original string-template-maven-plugin somehow works from NetBeans 12, but fails with an interface incompatibility from IntelliJ IDEA 2020.1, so I enhanced it and upgraded the POM.