GeezerVison is showing github.com — BAEL-3994: Add example of chaining methods returning Optionals (#9102) · OpenBranches/tutorials@56e13fb
File tree
Expand file treeCollapse file tree
Open diff view settings
core-java-modules/core-java-optional/src main/java/com/baeldung/orelseoptional test/java/com/baeldung/orelseoptional
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +package com.baeldung.orelseoptional; |
| 2 | + |
| 3 | +import java.util.Optional; |
| 4 | + |
| 5 | +public class ItemsProvider { |
| 6 | + |
| 7 | + Optional<String> getEmptyItem() { |
| 8 | + System.out.println("Returning an empty item"); |
| 9 | + return Optional.empty(); |
| 10 | + } |
| 11 | + |
| 12 | + Optional<String> getNail() { |
| 13 | + System.out.println("Returning a nail"); |
| 14 | + return Optional.of("nail"); |
| 15 | + } |
| 16 | + |
| 17 | + Optional<String> getHammer() { |
| 18 | + System.out.println("Returning a hammer"); |
| 19 | + return Optional.of("hammer"); |
| 20 | + } |
| 21 | +} |
Collapse file
Copy file name to clipboardExpand all lines: core-java-modules/core-java-optional/src/test/java/com/baeldung/orelseoptional/OrElseOptionalUnitTest.java +24-2Lines changed: 24 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | package com.baeldung.orelseoptional; |
2 | 2 |
|
3 | | -import static org.junit.Assert.assertEquals; |
| 3 | +import org.junit.Test; |
4 | 4 |
|
5 | 5 | import java.util.Optional; |
6 | 6 |
|
7 | | -import org.junit.Test; |
| 7 | +import static org.junit.Assert.assertEquals; |
8 | 8 |
|
9 | 9 | public class OrElseOptionalUnitTest { |
10 | 10 |
|
@@ -25,6 +25,28 @@ public void givenEmptyOptional_whenValue_thenOptionalGeneralMethod() { |
25 | 25 | assertEquals(fallbackOptionalString, OptionalUtils.or(optionalString, fallbackOptionalString)); |
26 | 26 | } |
27 | 27 |
|
| 28 | + @Test |
| 29 | + public void givenTwoOptionalMethods_whenFirstEmpty_thenSecondEvaluated() { |
| 30 | + ItemsProvider itemsProvider = new ItemsProvider(); |
| 31 | + |
| 32 | + Optional<String> item = itemsProvider.getEmptyItem() |
| 33 | + .map(Optional::of) |
| 34 | + .orElseGet(itemsProvider::getNail); |
| 35 | + |
| 36 | + assertEquals(Optional.of("nail"), item); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void givenTwoOptionalMethods_whenFirstNonEmpty_thenSecondNotEvaluated() { |
| 41 | + ItemsProvider itemsProvider = new ItemsProvider(); |
| 42 | + |
| 43 | + Optional<String> item = itemsProvider.getNail() |
| 44 | + .map(Optional::of) |
| 45 | + .orElseGet(itemsProvider::getHammer); |
| 46 | + |
| 47 | + assertEquals(Optional.of("nail"), item); |
| 48 | + } |
| 49 | + |
28 | 50 | // Uncomment code when code base is compatible with Java 9 |
29 | 51 | // @Test |
30 | 52 | // public void givenOptional_whenEmptyValue_thenCustomMessage() { |
|