Skip to content

Commit f5e8908

Browse files
Added Result of optional method
1 parent 2c097d0 commit f5e8908

File tree

2 files changed

+67
-0
lines changed
  • modules/collect/src

2 files changed

+67
-0
lines changed

modules/collect/src/main/java/com/opengamma/strata/collect/result/Result.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,49 @@ public static <R> Result<R> ofNullable(R value) {
460460
return ofNullable(value, FailureReason.MISSING_DATA, "Found null where a value was expected");
461461
}
462462

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+
463506
//-------------------------------------------------------------------------
464507
/**
465508
* Checks if all the results are successful.

modules/collect/src/test/java/com/opengamma/strata/collect/result/ResultTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Arrays;
1919
import java.util.HashSet;
2020
import java.util.List;
21+
import java.util.Optional;
2122
import java.util.Set;
2223
import java.util.function.BiFunction;
2324
import java.util.function.Function;
@@ -424,6 +425,29 @@ public void ofNullable_null() {
424425
}
425426

426427
//-------------------------------------------------------------------------
428+
429+
@Test
430+
public void ofOptional_nonEmpty() {
431+
Result<Integer> test = Result.ofOptional(Optional.of(6));
432+
assertThat(test.isFailure()).isFalse();
433+
assertThat(test.getValue().intValue()).isEqualTo(6);
434+
}
435+
436+
@Test
437+
public void ofOptional_empty() {
438+
Result<Integer> test = Result.ofOptional(Optional.empty());
439+
assertThat(test.isFailure()).isTrue();
440+
assertThat(test.getFailure().getMessage()).isEqualTo("Found empty where a value was expected");
441+
assertThat(test.getFailure().getItems().size()).isEqualTo(1);
442+
FailureItem item = test.getFailure().getItems().iterator().next();
443+
assertThat(item.getReason()).isEqualTo(MISSING_DATA);
444+
assertThat(item.getMessage()).isEqualTo("Found empty where a value was expected");
445+
assertThat(item.getCauseType().isPresent()).isEqualTo(false);
446+
assertThat(item.getStackTrace()).isNotNull();
447+
}
448+
449+
//-------------------------------------------------------------------------
450+
427451
@Test
428452
public void of_with_success() {
429453

0 commit comments

Comments
 (0)