io.vavr.control.Either类的使用及代码示例

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

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

Either介绍

[英]Either represents a value of two possible types. An Either is either a Left or a Right.

If the given Either is a Right and projected to a Left, the Left operations have no effect on the Right value.
If the given Either is a Left and projected to a Right, the Right operations have no effect on the Left value.
If a Left is projected to a Left or a Right is projected to a Right, the operations have an effect.

Example: A compute() function, which results either in an Integer value (in the case of success) or in an error message of type String (in the case of failure). By convention the success case is Right and the failure is Left.

Either<String,Integer> value = compute().right().map(i -> i * 2).toEither();

If the result of compute() is Right(1), the value is Right(2).
If the result of compute() is Left("error"), the value is Left("error").
[中]表示两种可能类型的值。非此即彼是左或右。
如果给定的任意一个为右并投影到左,则左操作对右值没有影响。
如果给定的任意一个为左并投影到右,则右操作对左值没有影响。
如果将左侧投影到左侧或将右侧投影到右侧,则操作会产生效果。
示例:一个compute()函数,其结果要么是一个整数值(如果成功),要么是一条字符串类型的错误消息(如果失败)。按照惯例,成功的案例是对的,失败的案例是左的。

Either<String,Integer> value = compute().right().map(i -> i * 2).toEither();

如果compute()的结果正确(1),则该值正确(2)。
如果compute()的结果为Left(“error”),则该值为Left(“error”)。

代码示例

代码示例来源: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

/**
 * Gets the Right value or an alternate value, if the projected Either is a Left.
 *
 * @param other a function which converts a Left value to an alternative Right value
 * @return the right value, if the underlying Either is a Right or else the alternative Right value provided by
 * {@code other} by applying the Left value.
 */
default R getOrElseGet(Function<? super L, ? extends R> other) {
  Objects.requireNonNull(other, "other is null");
  if (isRight()) {
    return get();
  } else {
    return other.apply(getLeft());
  }
}

代码示例来源: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

/**
 * Gets the Left value or an alternate value, if the projected Either is a Right.
 *
 * @param other an alternative value
 * @return the left value, if the underlying Either is a Left or else {@code other}
 * @throws NoSuchElementException if the underlying either of this LeftProjection is a Right
 */
@Override
public L getOrElse(L other) {
  return either.isLeft() ? either.getLeft() : other;
}

代码示例来源: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

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

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

Vector<R> rightValues = Vector.empty();
for (Either<? extends L, ? extends R> either : eithers) {
  if (either.isRight()) {
    rightValues = rightValues.append(either.get());
  } else {
    return Either.left(either.getLeft());
return Either.right(rightValues);

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

@Override
default Either<L, R> peek(Consumer<? super R> action) {
  Objects.requireNonNull(action, "action is null");
  if (isRight()) {
    action.accept(get());
  }
  return this;
}

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

/**
 * Maps the value of this Either if it is a Right, performs no operation if this is a Left.
 *
 * <pre><code>
 * import static io.vavr.API.*;
 *
 * // = Right("A")
 * Right("a").map(String::toUpperCase);
 *
 * // = Left(1)
 * Left(1).map(String::toUpperCase);
 * </code></pre>
 *
 * @param mapper 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")
@Override
default <U> Either<L, U> map(Function<? super R, ? extends U> mapper) {
  Objects.requireNonNull(mapper, "mapper is null");
  if (isRight()) {
    return Either.right(mapper.apply(get()));
  } else {
    return (Either<L, U>) this;
  }
}

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

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

代码示例来源: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

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

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

/**
 * Gets the Left value or an alternate value, if the projected Either is a Right.
 *
 * @param other a function which converts a Right value to an alternative Left value
 * @return the left value, if the underlying Either is a Left or else the alternative Left value provided by
 * {@code other} by applying the Right value.
 */
public L getOrElseGet(Function<? super R, ? extends L> other) {
  Objects.requireNonNull(other, "other is null");
  if (either.isLeft()) {
    return either.getLeft();
  } else {
    return other.apply(either.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

/**
 * 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

/**
 * Filters this right-biased {@code Either} by testing a predicate.
 * <p>
 *
 * @param predicate A predicate
 * @return a new {@code Option} instance
 * @throws NullPointerException if {@code predicate} is null
 */
default Option<Either<L, R>> filter(Predicate<? super R> predicate) {
  Objects.requireNonNull(predicate, "predicate is null");
  return isLeft() || predicate.test(get()) ? Option.some(this) : Option.none();
}

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

@Test
public void testEitherClass() throws Exception {
  EitherClass src = new EitherClass(Either.right(new ImplementedClass()));
  String json = MAPPER.writeValueAsString(src);
  EitherClass restored = MAPPER.readValue(json, EitherClass.class);
  Assert.assertEquals(restored.value.get().getClass(), ImplementedClass.class);
}

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

/**
 * Returns {@code Some} value of type L if this is a left projection of a Left value and the predicate
 * applies to the underlying value.
 *
 * @param predicate A predicate
 * @return A new Option
 */
public Option<LeftProjection<L, R>> filter(Predicate<? super L> predicate) {
  Objects.requireNonNull(predicate, "predicate is null");
  return either.isRight() || predicate.test(either.getLeft()) ? Option.some(this) : Option.none();
}

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

/**
 * Gets the Right value or throws, if the projected Either is a Left.
 *
 * @param <X>               a throwable type
 * @param exceptionFunction a function which creates an exception based on a Left value
 * @return the right value, if the underlying Either is a Right or else throws the exception provided by
 * {@code exceptionFunction} by applying the Left value.
 * @throws X if the projected Either is a Left
 */
public <X extends Throwable> R getOrElseThrow(Function<? super L, X> exceptionFunction) throws X {
  Objects.requireNonNull(exceptionFunction, "exceptionFunction is null");
  return either.getOrElseThrow(exceptionFunction);
}

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

@Override
public boolean isEmpty() {
  return either.isRight();
}

相关文章