Class SafeFunction
- java.lang.Object
-
- org.fugerit.java.core.function.SafeFunction
-
public class SafeFunction extends Object
Collections of utilities for handling exceptions through functions.
For instance, if the goal is to read an input without having the method throw an exception, a typical approach could be :
public String testExampleToOneLineClassic() { StringBuilder builder = new StringBuilder(); try ( BufferedReader reader = new BufferedReader( new StringReader( "test" ) ) ) { reader.lines().forEach( line -> builder.append( line+" " ) ); } catch (IOException e) { throw new ConfigRuntimeException( e ); } return builder.toString(); }
With SafeFunction.get() the same code could be written as follow :
public String testExampleToOneLineSafeFunction() { return SafeFunction.get(() -> { StringBuilder builder = new StringBuilder(); try ( BufferedReader reader = new BufferedReader( new StringReader( "test" ) ) ) { reader.lines().forEach( line -> builder.append( line+" " ) ); } return builder.toString(); }); }
So this way it is possible to separate the exception handling from the actual software logic. (useful to reduce testing coverage too).
-
-
Field Summary
Fields Modifier and Type Field Description static Consumer<Exception>
EX_CONSUMER_LOG_WARN
static Consumer<Exception>
EX_CONSUMER_THROW_CONFIG_RUNTIME
static Consumer<Exception>
EX_CONSUMER_TRACE_WARN
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
apply(UnsafeVoid<Exception> fun)
Apply an UnsafeVoid function, converting any raised Exception to ConfigRuntimeException.static void
apply(UnsafeVoid<Exception> fun, Consumer<Exception> exHandler)
Apply an UnsafeVoid function, using a consumer the handle any Exception raised.static <T> boolean
applyIfNotNull(T v, UnsafeVoid<Exception> fun)
static boolean
applyOnCondition(UnsafeSupplier<Boolean,Exception> condition, UnsafeVoid<Exception> fun)
static void
applySilent(UnsafeVoid<Exception> fun)
static <T> T
get(UnsafeSupplier<T,Exception> supplier)
Return the value of an UnsafeSupplier, converting any raised Exception to ConfigRuntimeException.static <T> T
get(UnsafeSupplier<T,Exception> supplier, Consumer<Exception> exHandler)
Return the value provided by the supplier, handling any exception with aConsumer
function.static <T,E extends Exception>
TgetEx(UnsafeSupplier<T,Exception> supplier, Function<Exception,E> fun)
Get a value returned by an UnsafeSupplier, and convert any raised Exceptionstatic <T,R>
RgetIfNotNUll(T v, UnsafeSupplier<R,Exception> supplier)
static <R> R
getOnCondition(UnsafeSupplier<Boolean,Exception> condition, UnsafeSupplier<R,Exception> supplier)
static <T> T
getWithDefault(UnsafeSupplier<T,Exception> supplier, Function<Exception,T> exHandler)
Return the value provided by the supplier, handling any exception with aConsumer
function.
-
-
-
Method Detail
-
getEx
public static <T,E extends Exception> T getEx(UnsafeSupplier<T,Exception> supplier, Function<Exception,E> fun) throws E extends Exception
Get a value returned by an UnsafeSupplier, and convert any raised Exception
- Type Parameters:
T
- the returned typeE
- the exception type- Parameters:
supplier
- theUnsafeSupplier
functionfun
- the Exception conversion function- Returns:
- the value evaluated by the
UnsafeSupplier
- Throws:
E
- the type of the raised ExceptionE extends Exception
-
get
public static <T> T get(UnsafeSupplier<T,Exception> supplier)
Return the value of an UnsafeSupplier, converting any raised Exception to ConfigRuntimeException.
- Type Parameters:
T
- the returned type- Parameters:
supplier
- theUnsafeSupplier
function- Returns:
- the value evaluated by the
UnsafeSupplier
- Throws:
ConfigRuntimeException
- may throw ConfigRuntimeException in case of exceptions
-
apply
public static void apply(UnsafeVoid<Exception> fun)
Apply an UnsafeVoid function, converting any raised Exception to ConfigRuntimeException.
- Parameters:
fun
- theUnsafeVoid
function
-
applySilent
public static void applySilent(UnsafeVoid<Exception> fun)
-
get
public static <T> T get(UnsafeSupplier<T,Exception> supplier, Consumer<Exception> exHandler)
Return the value provided by the supplier, handling any exception with a
Consumer
function.- Type Parameters:
T
- the return type- Parameters:
supplier
- theUnsafeSupplier
the return value supplierexHandler
- the exception handler- Returns:
- the value evaluated by the supplier
-
getWithDefault
public static <T> T getWithDefault(UnsafeSupplier<T,Exception> supplier, Function<Exception,T> exHandler)
Return the value provided by the supplier, handling any exception with a
Consumer
function.- Type Parameters:
T
- the return type- Parameters:
supplier
- theUnsafeSupplier
the return value supplierexHandler
- the exception handler as a function that can provide a default value- Returns:
- the value evaluated by the supplier
-
apply
public static void apply(UnsafeVoid<Exception> fun, Consumer<Exception> exHandler)
Apply an UnsafeVoid function, using a consumer the handle any Exception raised.
- Parameters:
fun
- theUnsafeVoid
functionexHandler
- the consumer to handle the exception
-
applyOnCondition
public static boolean applyOnCondition(UnsafeSupplier<Boolean,Exception> condition, UnsafeVoid<Exception> fun)
-
applyIfNotNull
public static <T> boolean applyIfNotNull(T v, UnsafeVoid<Exception> fun)
-
getOnCondition
public static <R> R getOnCondition(UnsafeSupplier<Boolean,Exception> condition, UnsafeSupplier<R,Exception> supplier)
-
getIfNotNUll
public static <T,R> R getIfNotNUll(T v, UnsafeSupplier<R,Exception> supplier)
-
-