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

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

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

Either.left介绍

[英]Returns a LeftProjection of this Either.
[中]返回此函数的左投影。

代码示例

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

/**
 * Alias for {@link Either#left(Object)}
 *
 * @param <L>  Type of left value.
 * @param <R>  Type of right value.
 * @param left The value.
 * @return A new {@link Either.Left} instance.
 */
@SuppressWarnings("unchecked")
public static <L, R> Either.Left<L, R> Left(L left) {
  return (Either.Left<L, R>) Either.left(left);
}

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

public <L2, R2> LeftProjection<L2, R2> bimap(Function<? super L, ? extends L2> leftMapper, Function<? super R, ? extends R2> rightMapper) {
  return either.<L2, R2> bimap(leftMapper, rightMapper).left();
}

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

/**
 * Converts this to a {@link Either}.
 *
 * @param <R>   right type
 * @param right A supplier of a right value
 * @return A new {@link Either.Right} containing the result of {@code right} if this is empty, otherwise
 * a new {@link Either.Left} containing this value.
 * @throws NullPointerException if {@code right} is null
 * @deprecated Use {@link #toEither(Supplier)} instead.
 */
@Deprecated
default <R> Either<T, R> toLeft(Supplier<? extends R> right) {
  Objects.requireNonNull(right, "right is null");
  return isEmpty() ? Either.right(right.get()) : Either.left(get());
}

代码示例来源: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 a {@link Either}.
 *
 * @param <L>  left type
 * @param left A supplier of a left value
 * @return A new {@link Either.Left} containing the result of {@code left} if this is empty, otherwise
 * a new {@link Either.Right} containing this value.
 * @throws NullPointerException if {@code left} is null
 * @deprecated Use {@link #toEither(Supplier)} instead.
 */
@Deprecated
default <L> Either<L, T> toRight(Supplier<? extends L> left) {
  Objects.requireNonNull(left, "left is null");
  return isEmpty() ? Either.left(left.get()) : Either.right(get());
}

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

/**
 * Converts this to a {@link Either}.
 *
 * @param <R>   right type
 * @param right An instance of a right value
 * @return A new {@link Either.Right} containing the value of {@code right} if this is empty, otherwise
 * a new {@link Either.Left} containing this value.
 * @deprecated Use {@link #toEither(Object)} instead.
 */
@Deprecated
default <R> Either<T, R> toLeft(R right) {
  return isEmpty() ? Either.right(right) : Either.left(get());
}

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

/**
 * Converts this to a {@link Either}.
 *
 * @param <L>  left type
 * @param left An instance of a left value
 * @return A new {@link Either.Left} containing the value of {@code left} if this is empty, otherwise
 * a new {@link Either.Right} containing this value.
 * @deprecated Use {@link #toEither(Object)} instead.
 */
@Deprecated
default <L> Either<L, T> toRight(L left) {
  return isEmpty() ? Either.left(left) : Either.right(get());
}

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

/**
 * Maps the value of this Either if it is a Left, performs no operation if this is a Right.
 *
 * <pre>{@code
 * import static io.vavr.API.*;
 *
 * // = Left(2)
 * Left(1).mapLeft(i -> i + 1);
 *
 * // = Right("a")
 * Right("a").mapLeft(i -> i + 1);
 * }</pre>
 *
 * @param leftMapper A mapper
 * @param <U>        Component type of the mapped right value
 * @return a mapped {@code Monad}
 * @throws NullPointerException if {@code mapper} is null
 */
@SuppressWarnings("unchecked")
default <U> Either<U, R> mapLeft(Function<? super L, ? extends U> leftMapper) {
  Objects.requireNonNull(leftMapper, "leftMapper is null");
  if (isLeft()) {
    return Either.left(leftMapper.apply(getLeft()));
  } else {
    return (Either<U, R>) this;
  }
}

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

/**
 * Filters this right-biased {@code Either} by testing a predicate.
 * If the {@code Either} is a {@code Right} and the predicate doesn't match, the
 * {@code Either} will be turned into a {@code Left} with contents computed by applying
 * the filterVal function to the {@code Either} value.
 *
 * <pre>{@code
 * import static io.vavr.API.*;
 *
 * // = Left("bad: a")
 * Right("a").filterOrElse(i -> false, val -> "bad: " + val);
 *
 * // = Right("a")
 * Right("a").filterOrElse(i -> true, val -> "bad: " + val);
 * }</pre>
 *
 * @param predicate A predicate
 * @param zero      A function that turns a right value into a left value if the right value does not make it through the filter.
 * @return an {@code Either} instance
 * @throws NullPointerException if {@code predicate} is null
 */
default Either<L,R> filterOrElse(Predicate<? super R> predicate, Function<? super R, ? extends L> zero) {
  Objects.requireNonNull(predicate, "predicate is null");
  Objects.requireNonNull(zero, "zero is null");
  if (isLeft() || predicate.test(get())) {
    return this;
  } else {
    return Either.left(zero.apply(get()));
  }
}

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

/**
 * Converts this {@code Try} to an {@link Either}.
 *
 * @return A new {@code Either}
 */
default Either<Throwable, T> toEither() {
  if (isFailure()) {
    return Either.left(getCause());
  } else {
    return Either.right(get());
  }
}

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

/**
 * Converts this Validation to an {@link Either}.
 *
 * @return {@code Either.right(get())} if this is valid, otherwise {@code Either.left(getError())}.
 */
default Either<E, T> toEither() {
  return isValid() ? Either.right(get()) : Either.left(getError());
}

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

rightValues = rightValues.append(either.get());
} else {
  return Either.left(either.getLeft());

代码示例来源:origin: jdbi/jdbi

@Test
public void testGetLeftEitherArgumentShouldNotBeEmpty() {
  Optional<Argument> arg = unit.build(new GenericType<Either<String, Integer>>() {}.getType(),
      Either.left("error"), null);
  assertThat(arg).isNotEmpty();
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testGetEitherLeftShouldReturnAllRows() {
  List<Something> result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME)
      .bind("name", Either.left("eric"))
      .mapToBean(Something.class)
      .list();
  assertThat(result).hasSize(2);
}

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

.partition(Either::isLeft)
.apply((leftPartition, rightPartition) -> leftPartition.hasNext()
  ? Either.left(leftPartition.map(Either::getLeft).toVector())
  : Either.right(rightPartition.map(Either::get).toVector())
);

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

private static <O> Either<Throwable, O> execute(final Supplier<O> operation) {
  LOGGER.trace("Will execute {}.", operation);
  return Try.ofSupplier(operation).map(Either::<Throwable, O>right)
      .recover(t -> {
        LOGGER.debug("Operation failed.", t);
        return Either.left(t);
      }).get();
}

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

private static <O> Either<Throwable, O> execute(final Supplier<O> operation) {
  LOGGER.trace("Will execute {}.", operation);
  return Try.ofSupplier(operation).map(Either::<Throwable, O>right)
      .recover(t -> {
        LOGGER.debug("Operation failed.", t);
        return Either.left(t);
      }).get();
}

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

/**
 * Converts this {@code Try} to an {@link Either}.
 *
 * @return A new {@code Either}
 */
default Either<Throwable, T> toEither() {
  if (isFailure()) {
    return Either.left(getCause());
  } else {
    return Either.right(get());
  }
}

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

@SuppressWarnings("unchecked")
@Test
public void test7() throws IOException {
  Either<List<Integer>, Set<Double>> either = Either.left(List.of(42));
  String json = mapper().writer().writeValueAsString(either);
  Either<List<Integer>, Set<Double>> restored = mapper().readValue(json, new TypeReference<Either<List<Integer>, Set<Double>>>() {});
  Assert.assertEquals(either, restored);
}

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

@Test
public void testWithOption() throws IOException {
  TypeReference<Either<Option<String>, Option<String>>> typeReference = new TypeReference<Either<Option<String>, Option<String>>>() {};
  verifySerialization(typeReference, List.of(
      Tuple.of(Either.left(none()), genJsonList("left", null)),
      Tuple.of(Either.right(none()), genJsonList("right", null)),
      Tuple.of(Either.left(some("value")), genJsonList("left", "value")),
      Tuple.of(Either.right(some("value")), genJsonList("right", "value"))
  ));
}

相关文章