@@ -44,13 +44,38 @@ public class SafeFunction {
4444
4545 private SafeFunction () {}
4646
47+ /**
48+ * <p>This Exception consumer just log the exception at warn log level. (no Exception is re-thrown).</p>
49+ */
4750 public static final Consumer <Exception > EX_CONSUMER_LOG_WARN = e -> log .warn ( "Exception suppressed : {}" , e .toString () );
4851
52+ /**
53+ * <p>This Exception consumer log and print the stack trace of the exception at warn log level. (no Exception is re-thrown).</p>
54+ */
4955 public static final Consumer <Exception > EX_CONSUMER_TRACE_WARN = e -> log .warn ( "Exception suppressed : " +e , e );
5056
57+ /**
58+ * <p>This Exception consumer wraps any Exception around a {@link ConfigRuntimeException}.</p>
59+ */
5160 public static final Consumer <Exception > EX_CONSUMER_THROW_CONFIG_RUNTIME = e -> { throw new ConfigRuntimeException ( e ); };
5261
53- private static final Consumer <Exception > DEFAULT_EX_CONSUMER = EX_CONSUMER_THROW_CONFIG_RUNTIME ;
62+ /**
63+ * <p>This Exception consumer wraps checked Exception around a {@link ConfigRuntimeException}.</p>
64+ *
65+ * <p>RuntimeException are just re-thrown.</p>
66+ */
67+ public static final Consumer <Exception > EX_CONSUMER_RETHROW_RTE_OR_CONVERT_CHECKED_TO_CRE = e -> {
68+ if ( e instanceof RuntimeException ) {
69+ throw (RuntimeException )e ;
70+ } else {
71+ throw new ConfigRuntimeException ( "Convert exception to ConfigRuntimeException : " +e , e );
72+ }
73+ };
74+
75+ /**
76+ * Default behavior is {@link EX_CONSUMER_RETHROW_RTE_OR_CONVERT_CHECKED_TO_CRE}
77+ */
78+ private static final Consumer <Exception > DEFAULT_EX_CONSUMER = EX_CONSUMER_RETHROW_RTE_OR_CONVERT_CHECKED_TO_CRE ;
5479
5580 /**
5681 * <p>Get a value returned by an UnsafeSupplier, and convert any raised Exception</p>
@@ -141,6 +166,13 @@ public static void apply( UnsafeVoid<Exception> fun, Consumer<Exception> exHandl
141166 }
142167 }
143168
169+ /**
170+ * <p>This method will apply a function only if a condition is met.</p>
171+ *
172+ * @param condition the condition to check
173+ * @param fun the function to apply if the condition returned <code>true</code>
174+ * @return the value returned by the condition
175+ */
144176 public static boolean applyOnCondition ( UnsafeSupplier <Boolean , Exception > condition , UnsafeVoid <Exception > fun ) {
145177 boolean cond = get ( condition );
146178 if ( cond ) {
@@ -149,10 +181,26 @@ public static boolean applyOnCondition( UnsafeSupplier<Boolean, Exception> condi
149181 return cond ;
150182 }
151183
184+ /**
185+ * <p>This method will apply a function only if a object is not null.</p>
186+ *
187+ * @param <T> the type of the object to check
188+ * @param v the object to check for <code>null</code>
189+ * @param fun the function to apply
190+ * @return <code>true</code> if the object is not null.
191+ */
152192 public static <T > boolean applyIfNotNull ( T v , UnsafeVoid <Exception > fun ) {
153193 return applyOnCondition ( () -> v != null , fun );
154194 }
155195
196+ /**
197+ * <p>This method return a value, provided by a supplier function, only if a condition is met.</p>
198+ *
199+ * @param <R> the return type
200+ * @param condition the condition to check
201+ * @param supplier the supplier function
202+ * @return the value returned by the supplier if the condition returns <code>true</code> or <code>null</code> otherwise.
203+ */
156204 public static <R > R getOnCondition ( UnsafeSupplier <Boolean , Exception > condition , UnsafeSupplier <R , Exception > supplier ) {
157205 return get ( () -> {
158206 R res = null ;
@@ -165,6 +213,15 @@ public static <R> R getOnCondition( UnsafeSupplier<Boolean, Exception> condition
165213
166214 }
167215
216+ /**
217+ * <p>This method return a value, provided by a supplier function, only if an object is not <code>null</code>.</p>
218+ *
219+ * @param <T> the type of the object to check for <code>null</code>
220+ * @param <R> the return type
221+ * @param v the object to check for <code>null</code>
222+ * @param supplier the supplier function
223+ * @return the value returned by the supplier if the object is not <code>null</code> or <code>null</code> otherwise.
224+ */
168225 public static <T , R > R getIfNotNull ( T v , UnsafeSupplier <R , Exception > supplier ) {
169226 return getOnCondition ( () -> v != null , supplier );
170227 }
0 commit comments