Package com.ontology2.pidove.util
Class DuctTape
java.lang.Object
com.ontology2.pidove.util.DuctTape
DuctTape methods address disagreements some people could have with Java.
Maybe the most controversial feature of Java is checked exceptions.
Sometimes an Exception happens inside a method which isn't allowed to throw an
Exception. What do you do? It's not impossible an Exception could be thrown in an
class that implements (say) Runnable#run because RuntimeExceptions are common in Java.
The runtimize methods execute a functional interface that throws an Exception that
convert the Exception into a RuntimeException, undoing some of the damage that
checked exceptions do to Java.
Sometimes people find it annoying that Java enforces the types of data values at
compile time. You can always cast a value to a specific type with (Type) but
doing that require that you know and specify the type. The cast function, as
in
SpecificObject x = cast(anyObject);
automatically determines the type. You might find that the lambda() function is
more responsible. Unfortunately you aren't allowed to use var with lambda definitions,
instead you have to write
Function<String,Integer> x = s -> s.length();
The lambda function tricks the compiler into choosing a specific functional interface,
which lets you write
var x = lambda((String) s -> s.length());
which is more concise.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <X> X
static <A,
B, C> Consumer3<A, B, C> static <A,
B, C, D> Function3<A, B, C, D> static <A,
B> BiConsumer<A, B> lambda
(BiConsumer<A, B> x) static <A,
B, C> BiFunction<A, B, C> lambda
(BiFunction<A, B, C> x) static <A> Consumer<A>
static <A,
B> Function<A, B> The following method tricks out the type inference system so you can write var fma = lambda((Integer a, Integer b, Integer c) -> a*b+c); that is, use var to save the a lambda function while defining the input types of the lambda function explictly in the definitionstatic void
uncheck
(ExceptionalRunnable that) Executes a method that could throw an Exception but wraps any non-RuntimeExceptions in RuntimeExceptions.static <X> X
uncheck
(ExceptionalSupplier<X> that) Executes a method that could throw an Exception but wraps any non-RuntimeExceptions in RuntimeExceptions.static Runnable
unchecked
(ExceptionalRunnable that) Converts a method that could throw a non-runtime Exception into one that doesn't, using the method in the uncheck method.static <X> Supplier<X>
unchecked
(ExceptionalSupplier<X> that)
-
Constructor Details
-
DuctTape
public DuctTape()
-
-
Method Details
-
cast
-
uncheck
Executes a method that could throw an Exception but wraps any non-RuntimeExceptions in RuntimeExceptions. If the exception is an InterruptedException it also interrupts the thread.- Parameters:
that
- a void method that might throw an exception
-
uncheck
Executes a method that could throw an Exception but wraps any non-RuntimeExceptions in RuntimeExceptions. If the exception is an InterruptedException it also interrupts the thread.- Parameters:
that
- a non-void method that might throw an exception
-
unchecked
Converts a method that could throw a non-runtime Exception into one that doesn't, using the method in the uncheck method. (It would probably be worthwhile to create methods with signatures such as Function since these would be useful to pass into other methods in pidove)- Parameters:
that
- a Runnable- Returns:
- nothing
-
unchecked
-
lambda
The following method tricks out the type inference system so you can write var fma = lambda((Integer a, Integer b, Integer c) -> a*b+c); that is, use var to save the a lambda function while defining the input types of the lambda function explictly in the definition- Type Parameters:
A
- input typeB
- output type- Parameters:
x
- a function- Returns:
- a Function
-
lambda
-
lambda
-
lambda
-
lambda
-
lambda
-