@@ -341,13 +341,14 @@ public <X extends Throwable> EffectHandler<X> runEffect(// NOSONAR
341
341
}
342
342
343
343
/**
344
- * If the value is present, map it to another value through the {@code mapper}
345
- * function. If the error is present, the {@code mapper} is never applied and
346
- * the next handler will still contain the error.
347
- *
348
- * @param <U> the type the value will be mapped to
349
- * @param mapper a function that receives the solved value and produces another
350
- * @return a new handler with either the mapped value, or the previous error
344
+ * If the value is present, map it to another value using the {@code mapper}
345
+ * function. If an error is present, the {@code mapper} function is never
346
+ * applied and the next handler will still contain the error.
347
+ *
348
+ * @param <U> the type the value is mapped to
349
+ * @param mapper a function which takes the value as argument and returns
350
+ * another value
351
+ * @return a new handler with either the mapped value or an error
351
352
*/
352
353
public <U > SolveHandler <U , E > map (final Function <? super T , ? extends U > mapper ) {
353
354
return this .value
@@ -358,6 +359,29 @@ public <U> SolveHandler<U, E> map(final Function<? super T, ? extends U> mapper)
358
359
);
359
360
}
360
361
362
+ /**
363
+ * If the value is present, map it to another value using the {@code mapper}
364
+ * function. If an error is present, the {@code mapper} function is never
365
+ * applied and the next handler will still contain the error.
366
+ *
367
+ * This method is similar to {@link #map(Function)}, but the mapping function is
368
+ * one whose result is a {@code Maybe}, and if invoked, flatMap does not wrap
369
+ * it within an additional {@code Maybe}.
370
+ *
371
+ * @param <U> the type the value is mapped to
372
+ * @param mapper a function which takes the value as argument and returns a
373
+ * {@code Maybe<U>} with another value
374
+ * @return a new handler with either the mapped value or an error
375
+ */
376
+ public <U > SolveHandler <U , E > flatMap (final Function <? super T , Maybe <? extends U >> mapper ) {
377
+ return this .value
378
+ .mapRight (mapper )
379
+ .unwrap (
380
+ SolveHandler ::failure ,
381
+ maybe -> maybe .solve (ThrowingFunction .identity ())
382
+ );
383
+ }
384
+
361
385
/**
362
386
* If the value is present, cast the value to anoter type. If the cast fails
363
387
* or if the error is present, it returns a new handler which contains a
0 commit comments