io.vavr.control.Try.toEither()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(3.8k)|赞(0)|评价(0)|浏览(408)

本文整理了Java中io.vavr.control.Try.toEither()方法的一些代码示例,展示了Try.toEither()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Try.toEither()方法的具体详情如下:
包路径:io.vavr.control.Try
类名称:Try
方法名:toEither

Try.toEither介绍

[英]Converts this Try to an Either.
[中]将此尝试转换为“或”。

代码示例

代码示例来源:origin: RoboZonky/robozonky

  1. private static Either<Throwable, ObjectName> assembleObjectName(final Class<?> clz) {
  2. return Try.of(() -> {
  3. final String packageName = clz.getPackage().getName();
  4. final String className = clz.getSimpleName();
  5. return new ObjectName(DOMAIN + ":type=" + packageName + ",name=" + className);
  6. }).toEither();
  7. }

代码示例来源:origin: com.mercateo.eventstore/client-common

  1. public Either<EventStoreFailure, String> toJsonString(Object data) {
  2. return Try //
  3. .of(() -> objectMapper.writeValueAsString(data))
  4. .onFailure(e -> log.warn("could not deserialize {}", data != null ? data.getClass().getSimpleName() : null,
  5. e))
  6. .toEither()
  7. .mapLeft(this::mapInternalError);
  8. }

代码示例来源:origin: com.mercateo.eventstore/client-common

  1. public <E> Either<EventStoreFailure, E> readValue(byte[] rawData, Class<E> dataClass) {
  2. return Try //
  3. .of(() -> objectMapper.readValue(rawData, dataClass))
  4. .toEither()
  5. .peekLeft(e -> log.warn("could not deserialize {}", dataClass.getSimpleName(), e))
  6. .mapLeft(this::mapInternalError);
  7. }

代码示例来源:origin: com.github.robozonky/robozonky-common

  1. @Override
  2. public synchronized Either<Throwable, T> get() {
  3. if (!needsReload()) {
  4. logger.trace("Not reloading {}.", this);
  5. return Either.right(value.get());
  6. }
  7. logger.trace("Reloading {}.", this);
  8. return Try.ofSupplier(getOperation())
  9. .peek(v -> processRetrievedValue(v, value::set))
  10. .toEither();
  11. }
  12. }

代码示例来源:origin: com.mercateo.eventstore/client-common

  1. public Either<EventStoreFailure, EventStore> createEventStore(EventStoreProperties properties) {
  2. return Try.of(() -> createEventStoreInternal(properties)).toEither().mapLeft(this::mapError);
  3. }

代码示例来源:origin: RoboZonky/robozonky

  1. @Override
  2. public Either<Throwable, T> get() {
  3. if (needsReload()) { // double-checked locking to make sure the reload only happens once
  4. synchronized (this) {
  5. if (needsReload()) {
  6. logger.trace("Reloading {}.", this);
  7. return Try.ofSupplier(() -> getOperation().apply(value.get()))
  8. .peek(v -> processRetrievedValue(v, value::set))
  9. .toEither();
  10. }
  11. // otherwise fall through to retrieve the value
  12. }
  13. }
  14. logger.trace("Not reloading {}.", this);
  15. return Either.right(value.get());
  16. }

代码示例来源:origin: com.github.robozonky/robozonky-common

  1. @Override
  2. public synchronized Either<Throwable, T> get() {
  3. if (value.get() == null) { // force value retrieval and wait for it
  4. logger.debug("Fetching initial value synchronously on {}.", this);
  5. return Try.ofSupplier(getOperation())
  6. .peek(v -> processRetrievedValue(v, value::set))
  7. .toEither();
  8. }
  9. if (!needsReload()) { // return old value
  10. logger.trace("Not reloading {}.", this);
  11. return Either.right(value.get());
  12. }
  13. // trigger retrieval but return existing value
  14. final CompletableFuture<Void> currentFuture = future.getAndUpdate(this::refresh);
  15. logger.debug("Retrieved potentially stale value on {}, while {}.", this, currentFuture);
  16. return Either.right(value.get());
  17. }
  18. }

代码示例来源:origin: RoboZonky/robozonky

  1. @Override
  2. public Either<Throwable, T> get() {
  3. if (!hasValue()) { // force value retrieval and wait for it
  4. synchronized (this) {
  5. if (!hasValue()) { // double-checked locking to make sure the value is only ever loaded once
  6. logger.debug("Fetching initial value synchronously on {}.", this);
  7. return Try.ofSupplier(() -> getOperation().apply(null))
  8. .peek(v -> processRetrievedValue(v, value::set))
  9. .toEither();
  10. }
  11. // otherwise fall through to retrieve the current value
  12. }
  13. }
  14. if (needsReload()) { // trigger value retrieval on the background
  15. synchronized (this) {
  16. final CompletableFuture<Void> currentFuture = future.getAndUpdate(this::refreshIfNotAlreadyRefreshing);
  17. logger.debug("Retrieved potentially stale value on {}, while {}.", this, currentFuture);
  18. }
  19. }
  20. // return the current value
  21. return Either.right(value.get());
  22. }

相关文章