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

x33g5p2x  于2022-01-19 转载在 其他  
字(4.1k)|赞(0)|评价(0)|浏览(144)

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

Either.mapLeft介绍

[英]Maps the value of this Either if it is a Left, performs no operation if this is a Right.

import static io.vavr.API.*;

[中]映射此值(如果为左),如果为右,则不执行任何操作

import static io.vavr.API.*;

代码示例

代码示例来源:origin: vavr-io/vavr

/**
 * Maps the left value if the projected Either is a Left.
 *
 * @param mapper A mapper which takes a left value and returns a value of type U
 * @param <U>    The new type of a Left value
 * @return A new LeftProjection
 */
@SuppressWarnings("unchecked")
@Override
public <U> LeftProjection<U, R> map(Function<? super L, ? extends U> mapper) {
  Objects.requireNonNull(mapper, "mapper is null");
  if (either.isLeft()) {
    return either.mapLeft((Function<L, U>) mapper).left();
  } else {
    return (LeftProjection<U, R>) this;
  }
}

代码示例来源:origin: vavr-io/vavr

/**
 * Converts this to an {@link Either}.
 *
 * @param leftSupplier A {@link Supplier} for the left value for the {@link Either}
 * @param <L>          Validation error component type
 * @return A new {@link Either}.
 */
default <L> Either<L, T> toEither(Supplier<? extends L> leftSupplier) {
  Objects.requireNonNull(leftSupplier, "leftSupplier is null");
  if (this instanceof Either) {
    return ((Either<?, T>) this).mapLeft(ignored -> leftSupplier.get());
  } else {
    return isEmpty() ? Left(leftSupplier.get()) : Right(get());
  }
}

代码示例来源:origin: vavr-io/vavr

/**
 * Converts this to an {@link Either}.
 *
 * @param left A left value for the {@link Either}
 * @param <L>  Either left component type
 * @return A new {@link Either}.
 */
default <L> Either<L, T> toEither(L left) {
  if (this instanceof Either) {
    return ((Either<?, T>) this).mapLeft(ignored -> left);
  } else {
    return isEmpty() ? Left(left) : Right(get());
  }
}

代码示例来源: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: io.vavr/vavr

/**
 * Maps the left value if the projected Either is a Left.
 *
 * @param mapper A mapper which takes a left value and returns a value of type U
 * @param <U>    The new type of a Left value
 * @return A new LeftProjection
 */
@SuppressWarnings("unchecked")
@Override
public <U> LeftProjection<U, R> map(Function<? super L, ? extends U> mapper) {
  Objects.requireNonNull(mapper, "mapper is null");
  if (either.isLeft()) {
    return either.mapLeft((Function<L, U>) mapper).left();
  } else {
    return (LeftProjection<U, R>) this;
  }
}

代码示例来源:origin: io.vavr/vavr

/**
 * Converts this to an {@link Either}.
 *
 * @param leftSupplier A {@link Supplier} for the left value for the {@link Either}
 * @param <L>          Validation error component type
 * @return A new {@link Either}.
 */
default <L> Either<L, T> toEither(Supplier<? extends L> leftSupplier) {
  Objects.requireNonNull(leftSupplier, "leftSupplier is null");
  if (this instanceof Either) {
    return ((Either<?, T>) this).mapLeft(ignored -> leftSupplier.get());
  } else {
    return isEmpty() ? Left(leftSupplier.get()) : Right(get());
  }
}

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

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

代码示例来源:origin: io.vavr/vavr

/**
 * Converts this to an {@link Either}.
 *
 * @param left A left value for the {@link Either}
 * @param <L>  Either left component type
 * @return A new {@link Either}.
 */
default <L> Either<L, T> toEither(L left) {
  if (this instanceof Either) {
    return ((Either<?, T>) this).mapLeft(ignored -> left);
  } else {
    return isEmpty() ? Left(left) : Right(get());
  }
}

相关文章