本文整理了Java中io.vavr.control.Try.getOrElseGet()
方法的一些代码示例,展示了Try.getOrElseGet()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Try.getOrElseGet()
方法的具体详情如下:
包路径:io.vavr.control.Try
类名称:Try
方法名:getOrElseGet
暂无
代码示例来源:origin: RoboZonky/robozonky
@Override
protected String getLatestSource() {
logger.debug("Reading notification configuration from '{}'.", source);
return Try.withResources(() -> new BufferedReader(new InputStreamReader(source.openStream(), Defaults.CHARSET)))
.of(r -> r.lines().collect(Collectors.joining(System.lineSeparator())))
.getOrElseGet(t -> {
logger.warn("Failed reading notification configuration from '{}' due to '{}'.", source,
t.getMessage());
return null;
});
}
}
代码示例来源:origin: com.github.robozonky/robozonky-notifications
@Override
protected String getLatestSource() {
LOGGER.debug("Reading notification configuration from '{}'.", source);
return Try.withResources(() -> new BufferedReader(new InputStreamReader(source.openStream(), Defaults.CHARSET)))
.of(r -> r.lines().collect(Collectors.joining(System.lineSeparator())))
.getOrElseGet(t -> {
LOGGER.warn("Failed reading notification configuration from '{}' due to '{}'.", source,
t.getMessage());
return null;
});
}
}
代码示例来源:origin: com.github.robozonky/robozonky-integration-stonky
static Optional<File> download(final URL url) {
return Try.withResources(url::openStream)
.of(Util::download)
.getOrElseGet(t -> {
LOGGER.warn("Failed downloading file.", t);
return Optional.empty();
});
}
代码示例来源:origin: com.github.robozonky/robozonky-app
@Override
protected Optional<VersionIdentifier> transform(final String source) {
return Try.withResources(() -> new ByteArrayInputStream(source.getBytes(Defaults.CHARSET)))
.of(s -> Optional.of(UpdateMonitor.parseVersionString(s)))
.getOrElseGet(ex -> {
LOGGER.debug("Failed parsing source.", ex);
return Optional.empty();
});
}
}
代码示例来源:origin: com.github.robozonky/robozonky-common
public static Optional<File> findFolder(final String folderName) {
final Path root = new File(System.getProperty("user.dir")).toPath();
return Try.withResources(() -> Files.find(root, 1, (path, attr) -> attr.isDirectory()))
.of(s -> s.map(Path::toFile)
.filter(f -> Objects.equals(f.getName(), folderName))
.findFirst())
.getOrElseGet(ex -> {
FileUtil.LOGGER.warn("Exception while walking file tree.", ex);
return Optional.empty();
});
}
代码示例来源:origin: RoboZonky/robozonky
@Override
protected Optional<ConfigStorage> transform(final String source) {
return Try.withResources(() -> new ByteArrayInputStream(source.getBytes(Defaults.CHARSET)))
.of(baos -> Optional.of(ConfigStorage.create(baos)))
.getOrElseGet(ex -> {
logger.warn("Failed transforming source.", ex);
return Optional.empty();
});
}
代码示例来源:origin: RoboZonky/robozonky
@Override
protected Optional<VersionIdentifier> transform(final String source) {
return Try.withResources(() -> new ByteArrayInputStream(source.getBytes(Defaults.CHARSET)))
.of(s -> Optional.of(UpdateMonitor.parseVersionString(s)))
.getOrElseGet(ex -> {
logger.debug("Failed parsing source.", ex);
return Optional.empty();
});
}
}
代码示例来源:origin: com.github.robozonky/robozonky-app
ReturnCode execute(final InvestmentMode mode) {
return Try.withResources(() -> mode)
.of(this::executeSafe)
.getOrElseGet(t -> {
LOGGER.error("Caught unexpected exception, terminating daemon.", t);
return ReturnCode.ERROR_UNEXPECTED;
});
}
代码示例来源:origin: com.github.robozonky/robozonky-notifications
@Override
protected Optional<ConfigStorage> transform(final String source) {
return Try.withResources(() -> new ByteArrayInputStream(source.getBytes(Defaults.CHARSET)))
.of(baos -> Optional.of(ConfigStorage.create(baos)))
.getOrElseGet(ex -> {
LOGGER.warn("Failed transforming source.", ex);
return Optional.empty();
});
}
代码示例来源:origin: RoboZonky/robozonky
ReturnCode execute(final InvestmentMode mode) {
return Try.withResources(() -> mode)
.of(this::executeSafe)
.getOrElseGet(t -> {
LOGGER.error("Caught unexpected exception, terminating daemon.", t);
return ReturnCode.ERROR_UNEXPECTED;
});
}
代码示例来源:origin: RoboZonky/robozonky
public static Optional<File> findFolder(final String folderName) {
final Path root = new File(System.getProperty("user.dir")).toPath();
return Try.withResources(() -> Files.find(root, 1, (path, attr) -> attr.isDirectory()))
.of(s -> s.map(Path::toFile)
.filter(f -> Objects.equals(f.getName(), folderName))
.findFirst())
.getOrElseGet(ex -> {
LOGGER.warn("Exception while walking file tree.", ex);
return Optional.empty();
});
}
代码示例来源:origin: RoboZonky/robozonky
static Optional<File> download(final URL url) {
return Try.withResources(url::openStream)
.of(Util::download)
.getOrElseGet(t -> {
LOGGER.warn("Failed downloading file.", t);
return Optional.empty();
});
}
代码示例来源:origin: RoboZonky/robozonky
/**
* Remove an entry from the key store.
* @param alias The alias to locate the entry.
* @return True if there is now no entry with a given key.
*/
public boolean delete(final String alias) {
return Try.of(() -> {
this.keyStore.deleteEntry(alias);
this.dirty.set(true);
return true;
}).getOrElseGet(t -> {
LOGGER.debug("Entry '{}' not deleted.", alias, t);
return false;
});
}
代码示例来源:origin: com.github.robozonky/robozonky-common
/**
* Remove an entry from the key store.
* @param alias The alias to locate the entry.
* @return True if there is now no entry with a given key.
*/
public boolean delete(final String alias) {
return Try.of(() -> {
this.keyStore.deleteEntry(alias);
this.dirty.set(true);
return true;
}).getOrElseGet(t -> {
KeyStoreHandler.LOGGER.debug("Entry '{}' not deleted.", alias, t);
return false;
});
}
代码示例来源:origin: Tristan971/Lyrebird
private Pane loadComponentSynchronous(final FxComponent fxComponent) {
return this.easyFxml
.loadNode(fxComponent)
.getNode()
.getOrElseGet(ExceptionHandler::fromThrowable);
}
代码示例来源:origin: RoboZonky/robozonky
CompletableFuture<Void> refreshIfNotAlreadyRefreshing(final CompletableFuture<Void> old) {
if (old == null || old.isDone()) {
logger.trace("Starting async reload.");
final Runnable asyncOperation = () -> Try.ofSupplier(() -> getOperation().apply(value.get()))
.peek(v -> processRetrievedValue(v, value::set)) // set the value on success
.getOrElseGet(t -> {
logger.warn("Async reload failed, operating with stale value.", t);
return null;
});
return CompletableFuture.runAsync(asyncOperation, Scheduler.inBackground().getExecutor());
} else {
logger.trace("Reload already in progress on {} with {}.", this, old);
return old;
}
}
代码示例来源:origin: com.github.robozonky/robozonky-integration-zonkoid
private static boolean requestConfirmation(final RequestId requestId, final int loanId, final int amount,
final String rootUrl, final String protocol) {
return Try.withResources(HttpClients::createDefault)
.of(httpClient -> {
LOGGER.debug("Requesting notification of {} CZK for loan #{}.", amount, loanId);
final HttpPost post = getRequest(requestId, loanId, amount, protocol, rootUrl);
return httpClient.execute(post, ZonkoidConfirmationProvider::respond);
})
.getOrElseGet(t -> handleError(requestId, loanId, amount, rootUrl, protocol, t));
}
代码示例来源:origin: RoboZonky/robozonky
private static boolean requestConfirmation(final RequestId requestId, final int loanId, final int amount,
final String rootUrl, final String protocol) {
return Try.withResources(HttpClients::createDefault)
.of(httpClient -> {
LOGGER.debug("Requesting notification of {} CZK for loan #{}.", amount, loanId);
final HttpPost post = getRequest(requestId, loanId, amount, protocol, rootUrl);
return httpClient.execute(post, ZonkoidConfirmationProvider::respond);
})
.getOrElseGet(t -> handleError(requestId, loanId, amount, rootUrl, protocol, t));
}
代码示例来源:origin: com.github.robozonky/robozonky-common
CompletableFuture<Void> refresh(final CompletableFuture<Void> old) {
if (old == null || old.isDone()) {
logger.trace("Starting async reload.");
final Runnable asyncOperation = () -> Try.ofSupplier(getOperation())
.peek(v -> processRetrievedValue(v, value::set)) // set the value on success
.getOrElseGet(t -> {
logger.warn("Async reload failed, operating with stale value.", t);
return null;
});
return CompletableFuture.runAsync(asyncOperation, Scheduler.inBackground().getExecutor());
} else {
logger.trace("Reload already in progress on {} with {}.", this, old);
return old;
}
}
代码示例来源:origin: Tristan971/Lyrebird
/**
* The {@link #update} box only show up when an update is detected as available. Then if it is the case, this method
* is called on click to open the update information screen.
*
* @see Screen#UPDATE_VIEW
*/
private void openUpdatesScreen() {
final FxmlLoadResult<Pane, FxmlController> updateScreenLoadResult = easyFxml.loadNode(Screen.UPDATE_VIEW);
final Pane updatePane = updateScreenLoadResult.getNode().getOrElseGet(ExceptionHandler::fromThrowable);
Stages.stageOf("Updates", updatePane).thenAcceptAsync(Stages::scheduleDisplaying);
}
内容来源于网络,如有侵权,请联系作者删除!