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

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

本文整理了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

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

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

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

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

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

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

@Override
  public synchronized Either<Throwable, T> get() {
    if (!needsReload()) {
      logger.trace("Not reloading {}.", this);
      return Either.right(value.get());
    }
    logger.trace("Reloading {}.", this);
    return Try.ofSupplier(getOperation())
        .peek(v -> processRetrievedValue(v, value::set))
        .toEither();
  }
}

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

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

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

@Override
public Either<Throwable, T> get() {
  if (needsReload()) { // double-checked locking to make sure the reload only happens once
    synchronized (this) {
      if (needsReload()) {
        logger.trace("Reloading {}.", this);
        return Try.ofSupplier(() -> getOperation().apply(value.get()))
            .peek(v -> processRetrievedValue(v, value::set))
            .toEither();
      }
      // otherwise fall through to retrieve the value
    }
  }
  logger.trace("Not reloading {}.", this);
  return Either.right(value.get());
}

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

@Override
  public synchronized Either<Throwable, T> get() {
    if (value.get() == null) { // force value retrieval and wait for it
      logger.debug("Fetching initial value synchronously on {}.", this);
      return Try.ofSupplier(getOperation())
          .peek(v -> processRetrievedValue(v, value::set))
          .toEither();
    }
    if (!needsReload()) { // return old value
      logger.trace("Not reloading {}.", this);
      return Either.right(value.get());
    }
    // trigger retrieval but return existing value
    final CompletableFuture<Void> currentFuture = future.getAndUpdate(this::refresh);
    logger.debug("Retrieved potentially stale value on {}, while {}.", this, currentFuture);
    return Either.right(value.get());
  }
}

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

@Override
public Either<Throwable, T> get() {
  if (!hasValue()) { // force value retrieval and wait for it
    synchronized (this) {
      if (!hasValue()) { // double-checked locking to make sure the value is only ever loaded once
        logger.debug("Fetching initial value synchronously on {}.", this);
        return Try.ofSupplier(() -> getOperation().apply(null))
            .peek(v -> processRetrievedValue(v, value::set))
            .toEither();
      }
      // otherwise fall through to retrieve the current value
    }
  }
  if (needsReload()) { // trigger value retrieval on the background
    synchronized (this) {
      final CompletableFuture<Void> currentFuture = future.getAndUpdate(this::refreshIfNotAlreadyRefreshing);
      logger.debug("Retrieved potentially stale value on {}, while {}.", this, currentFuture);
    }
  }
  // return the current value
  return Either.right(value.get());
}

相关文章