@@ -460,6 +460,49 @@ public static <R> Result<R> ofNullable(R value) {
460
460
return ofNullable (value , FailureReason .MISSING_DATA , "Found null where a value was expected" );
461
461
}
462
462
463
+ /**
464
+ * Returns a success result containing the value is present, else returns a failure result
465
+ * with the specified reason and message.
466
+ * <p>
467
+ * The message is produced using a template that contains zero to many "{}" placeholders.
468
+ * Each placeholder is replaced by the next available argument.
469
+ * If there are too few arguments, then the message will be left with placeholders.
470
+ * If there are too many arguments, then the excess arguments are appended to the
471
+ * end of the message. No attempt is made to format the arguments.
472
+ * See {@link Messages#format(String, Object...)} for more details.
473
+ *
474
+ * @param <R> the expected type of the result
475
+ * @param value the potentially null value
476
+ * @param reason the reason for the failure
477
+ * @param message a message explaining the failure, uses "{}" for inserting {@code messageArgs}
478
+ * @param messageArgs the arguments for the message
479
+ * @return a success result if the value is non-null, else a failure result
480
+ */
481
+ public static <R > Result <R > ofOptional (
482
+ Optional <R > value ,
483
+ FailureReason reason ,
484
+ String message ,
485
+ Object ... messageArgs ) {
486
+
487
+ return value
488
+ .map (Result ::success )
489
+ .orElse (Result .failure (reason , message , messageArgs ));
490
+ }
491
+
492
+ /**
493
+ * Returns a success result containing the value is present, else returns a failure result
494
+ * with a reason of {@link FailureReason#MISSING_DATA} and message to say an unexpected empty value was found.
495
+ *
496
+ * @param <R> the expected type of the result
497
+ * @param value the potentially null value
498
+ * @return a success result if the value is non-null, else a failure result
499
+ */
500
+ public static <R > Result <R > ofOptional (
501
+ Optional <R > value ) {
502
+
503
+ return ofOptional (value , FailureReason .MISSING_DATA , "Found empty where a value was expected" );
504
+ }
505
+
463
506
//-------------------------------------------------------------------------
464
507
/**
465
508
* Checks if all the results are successful.
0 commit comments