public class Functional extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
Functional.ConsumerThrows<T,E extends Exception>
|
static interface |
Functional.RunnableThrows<E extends Exception>
|
static interface |
Functional.SupplierThrows<T,E extends Exception>
|
static class |
Functional.UncheckingExcpetion
Wrapper class that encapsulates a checked exception in an unchecked exception
|
Constructor and Description |
---|
Functional() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
chain(T t,
Consumer<T>... fs)
This method allows for the chaining of calls that don't normally chain.
|
static <T,E extends Exception> |
chainThrows(T t,
Functional.ConsumerThrows<T,E>... fs)
This method allows for the chaining of calls that don't normally chain but that also may throw.
|
static <Ein extends Exception,Eout extends Exception> |
mapChecked(Functional.RunnableThrows<Ein> f,
Function<Ein,Eout> mapException)
This method allows for the automapping mapping of lambda's that throw checked exceptions to other (checked or unchecked) exceptions.
|
static <T,Ein extends Exception,Eout extends Exception> |
mapChecked(Functional.SupplierThrows<T,Ein> f,
Function<Ein,Eout> mapException)
This method allows for the automapping mapping of lambda's that throw checked exceptions to other (checked or unchecked) exceptions.
|
static <E extends Exception> |
recheck(Runnable f)
This assumes there's a
uncheck(SupplierThrows) call nested within the provided supplier. |
static <E extends Exception> |
recheck(Runnable f,
Class<E> exceptionClass)
Alternate form of
recheck(Runnable) that doesn't require the explicit generic and allows for the use of a static import. |
static <T,E extends Exception> |
recheck(Supplier<T> f)
This assumes there's a
uncheck(SupplierThrows) call nested within the provided supplier. |
static <T,E extends Exception> |
recheck(Supplier<T> f,
Class<E> exceptionClass)
Alternate form of
recheck(Supplier) that doesn't require the explicit generic and allows for the use of a static import. |
static IntStream |
reverseRange(int from,
int to)
Create's an IntStream that runs in reverse.
|
static <E extends Exception> |
uncheck(Functional.RunnableThrows<E> f)
This method allows the use of lambda's that throw exceptions in standard java 8
Stream operations by converting those exceptions to an unchecked exception. |
static <T,E extends Exception> |
uncheck(Functional.SupplierThrows<T,E> f)
This method allows the use of lambda's that throw exceptions in standard java 8
Stream operations by converting those exceptions to an unchecked exception. |
public static IntStream reverseRange(int from, int to)
@SafeVarargs public static <T> T chain(T t, Consumer<T>... fs)
This method allows for the chaining of calls that don't normally chain. For example, typical setter methods. A simple example would be:
properties = chain(new Properties(), p -> p.setProperty("name1", "value1"), p -> p.setProperty("name2", "value2"));
@SafeVarargs public static <T,E extends Exception> T chainThrows(T t, Functional.ConsumerThrows<T,E>... fs) throws E extends Exception
This method allows for the chaining of calls that don't normally chain but that also may throw. A simple example would be:
properties = chain(new Properties(), p -> p.setProperty("name1", "value1"), p -> p.setProperty("name2", "value2"));
E extends Exception
public static <T,E extends Exception> T uncheck(Functional.SupplierThrows<T,E> f)
This method allows the use of lambda's that throw exceptions in standard java 8 Stream
operations by converting those exceptions to an unchecked exception. For example:
Suppose I wanted to process a Stream
of classnames:
classes = classnames.stream().map(Class::forName).collect(Collectors.toList());
This wont work since Class.forName throws a checked ClassNotFoundException
. Therefore we can wrap it.
classes = classnames.stream().map(cn -> uncheck(() -> Class.forName(cn))).collect(Collectors.toList());
T
- is the type that is supplied by the function, f.E
- is the exception type that's thrown by f.f
- is a Functional.SupplierThrows
that throws an exceptionFunctional.UncheckingExcpetion
- which is a specific RuntimeException
that wraps the underlying exception.public static <E extends Exception> void uncheck(Functional.RunnableThrows<E> f)
This method allows the use of lambda's that throw exceptions in standard java 8 Stream
operations by converting those exceptions to an unchecked exception. For example:
Suppose I wanted to process a Stream
of classnames:
strings.stream().forEach(objectOutputStream::writeObject);
This wont work since ObjectOutputStream.writeObject(Object)
throws a checked IOException
. Therefore we can wrap it.
strings.stream().forEach(s -> uncheck(() -> objectOutputStream.writeObject(s));
E
- is the exception type that's thrown by f.f
- is a Functional.RunnableThrows
that throws an exceptionFunctional.UncheckingExcpetion
- which is a specific RuntimeException
that wraps the underlying exception.public static <T,E extends Exception> T recheck(Supplier<T> f) throws E extends Exception
This assumes there's a uncheck(SupplierThrows)
call nested within the provided supplier. An example of the usage would be:
Suppose I wanted to process a Stream
of classnames:
classes = classnames.stream().map(Class::forName).collect(Collectors.toList());
This wont work since Class.forName throws a checked ClassNotFoundException
. Therefore we can wrap it.
classes = classnames.stream().map(cn -> uncheck(() -> Class.forName(cn))).collect(Collectors.toList());
But this buries the checked exception. If we want the checked exception to be thrown from the stream operation we can recheck(Supplier)
it. Unfortunately we need to be explicit about the
exception type thrown since there's no way for the compiler to infer it. This also means we can't use a static import.
try {
classes = Functional.<ClassNotFoundException> recheck(() -> classnames.stream().map(cn -> uncheck(() -> Class.forName(cn))));
} catch (final ClassNotFoundException cnf) {
...
}
T
- is the type that is supplied by the function, f.E
- is the exception type that's wrapped inside of 'f' having been passed to uncheck(SupplierThrows)
.E extends Exception
public static <E extends Exception> void recheck(Runnable f) throws E extends Exception
This assumes there's a uncheck(SupplierThrows)
call nested within the provided supplier. An example of the usage would be:
Suppose I wanted to process a Stream
of classnames:
strings.stream().forEach(objectOutputStream::writeObject);
This wont work since ObjectOutputStream.writeObject(Object)
throws a checked IOException
. Therefore we can wrap it.
strings.stream().forEach(s -> uncheck(() -> objectOutputStream.writeObject(s));
But this buries the checked exception. If we want the checked exception to be thrown from the stream operation we can recheck(Runnable)
it. Unfortunately we need to be explicit about the
exception type thrown since there's no way for the compiler to infer it. This also means we can't use a static import.
try {
Functional.<ClassNotFoundException> recheck(() -> strings.stream().forEach(s -> uncheck(() -> objectOutputStream.writeObject(s))));
} catch (final ClassNotFoundException cnf) {
....
}
E extends Exception
public static <T,E extends Exception> T recheck(Supplier<T> f, Class<E> exceptionClass) throws E extends Exception
recheck(Supplier)
that doesn't require the explicit generic and allows for the use of a static import.E extends Exception
public static <E extends Exception> void recheck(Runnable f, Class<E> exceptionClass) throws E extends Exception
recheck(Runnable)
that doesn't require the explicit generic and allows for the use of a static import.E extends Exception
public static <T,Ein extends Exception,Eout extends Exception> T mapChecked(Functional.SupplierThrows<T,Ein> f, Function<Ein,Eout> mapException) throws Eout extends Exception
f
- is the lambda to remap.mapException
- is the function to do the mappingEout
- when the function maps an exception, it is thrown from the method call.Eout extends Exception
public static <Ein extends Exception,Eout extends Exception> void mapChecked(Functional.RunnableThrows<Ein> f, Function<Ein,Eout> mapException) throws Eout extends Exception
f
- is the lambda to remap.mapException
- is the function to do the mappingEout
- when the function maps an exception, it is thrown from the method call.Eout extends Exception
Copyright © 2018. All rights reserved.