Java Code Examples for java.util.Optional#or()
The following examples show how to use
java.util.Optional#or() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InSyncTracker.java From besu with Apache License 2.0 | 6 votes |
private Optional<Boolean> currentSyncStatus( final ChainHead localChain, final Optional<ChainHeadEstimate> syncTargetChain, final Optional<ChainHeadEstimate> bestPeerChain) { final Optional<Boolean> inSyncWithSyncTarget = syncTargetChain.map(remote -> isInSync(localChain, remote)); final Optional<Boolean> inSyncWithBestPeer = bestPeerChain.map(remote -> isInSync(localChain, remote)); // If we're out of sync with either peer, we're out of sync if (inSyncWithSyncTarget.isPresent() && !inSyncWithSyncTarget.get()) { return Optional.of(false); } if (inSyncWithBestPeer.isPresent() && !inSyncWithBestPeer.get()) { return Optional.of(false); } // Otherwise, if either peer is in sync, we're in sync return inSyncWithSyncTarget.or(() -> inSyncWithBestPeer); }
Example 2
Source File: TestSolution2OptionalConditionalFetching.java From java-katas with MIT License | 6 votes |
@Test @DisplayName("return an Optional of either a non-null value or from a Supplier") @Tag("PASSING") @Order(1) public void orReturnOptional() { Optional<String> defaultOptional = Optional.of("supplied"); Optional<String> anOptional = Optional.empty(); /* * DONE: * Replace the empty optional to either return the anOptional, if it has a value * or return the defaultOptional (use a Supplier) * Check API: java.util.Optional.or(?) */ Optional<String> nonNullOptional = anOptional.or(() -> defaultOptional); assertTrue(nonNullOptional instanceof Optional, "The nonNullOptional should be an instance of Optional"); assertFalse(nonNullOptional.isEmpty(), "The nonNullOptional should not be empty"); }
Example 3
Source File: ScreenshotTaker.java From vividus with Apache License 2.0 | 5 votes |
@Override public ru.yandex.qatools.ashot.Screenshot takeAshotScreenshot(SearchContext searchContext, Optional<ScreenshotConfiguration> screenshotConfiguration) { Optional<ScreenshotConfiguration> configuration = screenshotConfiguration.or(this::getConfiguration); AShot aShot = createAShot(false, configuration); return takeScreenshot(searchContext, aShot, configuration.map(ScreenshotConfiguration::getScrollableElement).flatMap(Supplier::get)); }
Example 4
Source File: ICALToJsonAttribute.java From james-project with Apache License 2.0 | 5 votes |
private Optional<MailAddress> retrieveSender(Mail mail) throws MessagingException { Optional<MailAddress> fromMime = StreamUtils.ofOptional( Optional.ofNullable(mail.getMessage()) .map(Throwing.function(MimeMessage::getFrom).orReturn(new Address[]{}))) .map(address -> (InternetAddress) address) .map(InternetAddress::getAddress) .map(MaybeSender::getMailSender) .flatMap(MaybeSender::asStream) .findFirst(); return fromMime.or(() -> mail.getMaybeSender().asOptional()); }
Example 5
Source File: ContentSearch.java From milkman with MIT License | 4 votes |
private Optional<MatchResult> getPrevMatchFrom(int caretPosition, List<MatchResult> matches) { Optional<MatchResult> prev = matches.stream().filter(m -> m.end() < caretPosition).reduce((a,b) -> b); // find last return prev.or(() -> matches.stream().filter(m -> m.start() > caretPosition).reduce((a,b) -> b)); }
Example 6
Source File: ContentSearch.java From milkman with MIT License | 4 votes |
private Optional<MatchResult> getNextMatchFrom(int caretPosition, List<MatchResult> matches) { Optional<MatchResult> first = matches.stream().filter(m -> m.start() > caretPosition).findFirst(); return first.or(() -> matches.stream().filter(m -> m.end() < caretPosition).findFirst()); }
Example 7
Source File: Book.java From Java-Coding-Problems with MIT License | 3 votes |
public Optional<String> findStatusPrefer() { // fetch an Optional prone to be empty Optional<String> status = Optional.empty(); return status.or(() -> Optional.of(BOOK_STATUS)); }