Class 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 Detail

      • EX_CONSUMER_TRACE_WARN

        public static final Consumer<Exception> EX_CONSUMER_TRACE_WARN
      • EX_CONSUMER_THROW_CONFIG_RUNTIME

        public static final Consumer<Exception> EX_CONSUMER_THROW_CONFIG_RUNTIME
    • 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 type
        E - the exception type
        Parameters:
        supplier - the UnsafeSupplier function
        fun - the Exception conversion function
        Returns:
        the value evaluated by the UnsafeSupplier
        Throws:
        E - the type of the raised Exception
        E 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 - the UnsafeSupplier 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 - the UnsafeVoid function
      • 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 - the UnsafeSupplier the return value supplier
        exHandler - 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 - the UnsafeSupplier the return value supplier
        exHandler - 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 - the UnsafeVoid function
        exHandler - the consumer to handle the exception
      • applyIfNotNull

        public static <T> boolean applyIfNotNull​(T v,
                                                 UnsafeVoid<Exception> fun)