Open
Description
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:
- DoubleBinaryOperator
- DoubleConsumer
- DoubleFunction
- DoublePredicate
- DoubleSupplier
- DoubleToIntFunction
- DoubleToLongFunction
- DoubleUnaryOperator
- IntBinaryOperator
- IntConsumer
- IntFunction
- IntPredicate
- IntSupplier
- IntToDoubleFunction
- IntToLongFunction
- IntUnaryOperator
- LongBinaryOperator
- LongConsumer
- LongFunction
- LongPredicate
- LongSupplier
- LongToDoubleFunction
- LongToIntFunction
- LongUnaryOperator
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);
}
};
}
}
Metadata
Metadata
Assignees
Labels
No labels