@@ -44,13 +44,38 @@ public class SafeFunction {
44
44
45
45
private SafeFunction () {}
46
46
47
+ /**
48
+ * <p>This Exception consumer just log the exception at warn log level. (no Exception is re-thrown).</p>
49
+ */
47
50
public static final Consumer <Exception > EX_CONSUMER_LOG_WARN = e -> log .warn ( "Exception suppressed : {}" , e .toString () );
48
51
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
+ */
49
55
public static final Consumer <Exception > EX_CONSUMER_TRACE_WARN = e -> log .warn ( "Exception suppressed : " +e , e );
50
56
57
+ /**
58
+ * <p>This Exception consumer wraps any Exception around a {@link ConfigRuntimeException}.</p>
59
+ */
51
60
public static final Consumer <Exception > EX_CONSUMER_THROW_CONFIG_RUNTIME = e -> { throw new ConfigRuntimeException ( e ); };
52
61
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 ;
54
79
55
80
/**
56
81
* <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
141
166
}
142
167
}
143
168
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
+ */
144
176
public static boolean applyOnCondition ( UnsafeSupplier <Boolean , Exception > condition , UnsafeVoid <Exception > fun ) {
145
177
boolean cond = get ( condition );
146
178
if ( cond ) {
@@ -149,10 +181,26 @@ public static boolean applyOnCondition( UnsafeSupplier<Boolean, Exception> condi
149
181
return cond ;
150
182
}
151
183
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
+ */
152
192
public static <T > boolean applyIfNotNull ( T v , UnsafeVoid <Exception > fun ) {
153
193
return applyOnCondition ( () -> v != null , fun );
154
194
}
155
195
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
+ */
156
204
public static <R > R getOnCondition ( UnsafeSupplier <Boolean , Exception > condition , UnsafeSupplier <R , Exception > supplier ) {
157
205
return get ( () -> {
158
206
R res = null ;
@@ -165,6 +213,15 @@ public static <R> R getOnCondition( UnsafeSupplier<Boolean, Exception> condition
165
213
166
214
}
167
215
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
+ */
168
225
public static <T , R > R getIfNotNull ( T v , UnsafeSupplier <R , Exception > supplier ) {
169
226
return getOnCondition ( () -> v != null , supplier );
170
227
}
0 commit comments