Package eu.ciechanowiec.sneakyfun
Interface SneakyBinaryOperator<T,X extends Exception>
-
- Type Parameters:
T- the type of the operands and result of the operatorX- the type of exception that might be thrown during execution of the functional method of this interface
- All Superinterfaces:
SneakyBiFunction<T,T,T,X>
- 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 SneakyBinaryOperator<T,X extends Exception> extends SneakyBiFunction<T,T,T,X>
Represents an operation upon two operands of the same type, producing a result of the same type as the operands. This is a specialization ofSneakyBiFunctionfor the case where the operands and the result are all of the same type.This is a
functional interfacewhose functional method isSneakyBiFunction.apply(Object, Object).
-
-
Method Summary
Static Methods Modifier and Type Method Description static <T,X extends Exception>
BinaryOperator<T>sneaky(SneakyBinaryOperator<T,X> binaryOperator)Wraps the passed sneaky functional interface (sneaky interface) into the analogous functional interface from thejava.util.functionpackage (original interface).-
Methods inherited from interface eu.ciechanowiec.sneakyfun.SneakyBiFunction
apply
-
-
-
-
Method Detail
-
sneaky
static <T,X extends Exception> BinaryOperator<T> sneaky(SneakyBinaryOperator<T,X> binaryOperator)
Wraps the passed sneaky functional interface (sneaky interface) into the analogous functional interface from thejava.util.functionpackage (original interface).-
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
throwsclause 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; -
A
throwsclause 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 staticsneaky(...)method declared in the respective sneaky interface, as shown in the examples below. -
Example - Outside Streams
- 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"); - Sneaky & elegant approach:
SneakyFunction
toURI = URI::new; Function toURIAdapter = sneaky(toURI); URI uri = toURIAdapter.apply("google.com");
- Classical & unsightly approach:
-
Example - In Streams
- 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(); - Sneaky & elegant approach:
List
rawURIs = List.of("google.com", "ciechanowiec.eu"); List pureURIs = rawURIs.stream() .map(sneaky(URI::new)) .toList();
- Classical & unsightly approach:
- For details see documentation: https://github.com/ciechanowiec/sneakyfun.
- Type Parameters:
T-X-- Parameters:
binaryOperator- sneaky functional interface that will be wrapped into the analogous original functional interface from thejava.util.functionpackage- Returns:
- original functional interface from the
java.util.functionpackage wrapping the passed sneaky functional interface
-
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
-
-