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

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

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

Either.get介绍

[英]Gets the right value if this is a Right or throws if this is a Left.
[中]如果这是右侧,则获取右侧值;如果这是左侧,则抛出。

代码示例

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

@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

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

/**
 * Runs an action in the case this is a projection on a Right value.
 *
 * @param action an action which consumes a Right value
 */
public void orElseRun(Consumer<? super R> action) {
  Objects.requireNonNull(action, "action is null");
  if (either.isRight()) {
    action.accept(either.get());
  }
}

代码示例来源: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
 */
default <X extends Throwable> R getOrElseThrow(Function<? super L, X> exceptionFunction) throws X {
  Objects.requireNonNull(exceptionFunction, "exceptionFunction is null");
  if (isRight()) {
    return get();
  } else {
    throw exceptionFunction.apply(getLeft());
  }
}

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

/**
 * Applies the given action to the value if the projected either is a Right. Otherwise nothing happens.
 *
 * @param action An action which takes a right value
 * @return this {@code Either} instance
 */
@Override
public RightProjection<L, R> peek(Consumer<? super R> action) {
  Objects.requireNonNull(action, "action is null");
  if (either.isRight()) {
    action.accept(either.get());
  }
  return this;
}

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

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

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

/**
 * Gets the {@code Right} value or throws.
 *
 * @return the right value, if the underlying {@code Either} is a {@code Right}
 * @throws NoSuchElementException if the underlying {@code Either} of this {@code RightProjection} is a {@code Left}
 */
@Override
public R get() {
  if (either.isRight()) {
    return either.get();
  } else {
    throw new NoSuchElementException("RightProjection.get() on Left");
  }
}

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

/**
 * Folds either the left or the right side of this disjunction.
 *
 * @param leftMapper  maps the left value if this is a Left
 * @param rightMapper maps the right value if this is a Right
 * @param <U>         type of the folded value
 * @return A value of type U
 */
default <U> U fold(Function<? super L, ? extends U> leftMapper, Function<? super R, ? extends U> rightMapper) {
  Objects.requireNonNull(leftMapper, "leftMapper is null");
  Objects.requireNonNull(rightMapper, "rightMapper is null");
  if (isRight()) {
    return rightMapper.apply(get());
  } else {
    return leftMapper.apply(getLeft());
  }
}

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

/**
 * FlatMaps this right-biased Either.
 *
 * @param mapper A mapper
 * @param <U>    Component type of the mapped right value
 * @return this as {@code Either<L, U>} if this is a Left, otherwise the right mapping result
 * @throws NullPointerException if {@code mapper} is null
 */
@SuppressWarnings("unchecked")
default <U> Either<L, U> flatMap(Function<? super R, ? extends Either<L, ? extends U>> mapper) {
  Objects.requireNonNull(mapper, "mapper is null");
  if (isRight()) {
    return (Either<L, U>) mapper.apply(get());
  } else {
    return (Either<L, U>) this;
  }
}

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

/**
 * Creates a {@code Validation} of an {@code Either}.
 *
 * @param either An {@code Either}
 * @param <E>    error type
 * @param <T>    value type
 * @return A {@code Valid(either.get())} if either is a Right, otherwise {@code Invalid(either.getLeft())}.
 * @throws NullPointerException if either is null
 */
static <E, T> Validation<E, T> fromEither(Either<E, T> either) {
  Objects.requireNonNull(either, "either is null");
  return either.isRight() ? valid(either.get()) : invalid(either.getLeft());
}

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

/**
 * FlatMaps this RightProjection.
 *
 * @param mapper A mapper
 * @param <U>    Component type of the mapped right value
 * @return this as {@code RightProjection<L, U>} if a Left is underlying, otherwise a the mapping result of the right value.
 * @throws NullPointerException if {@code mapper} is null
 */
@SuppressWarnings("unchecked")
public <U> RightProjection<L, U> flatMap(Function<? super R, ? extends RightProjection<L, ? extends U>> mapper) {
  Objects.requireNonNull(mapper, "mapper is null");
  if (either.isRight()) {
    return (RightProjection<L, U>) mapper.apply(either.get());
  } else {
    return (RightProjection<L, U>) this;
  }
}

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

/**
 * Converts a {@code Left} to a {@code Right} vice versa by wrapping the value in a new type.
 *
 * @return a new {@code Either}
 */
default Either<R, L> swap() {
  if (isRight()) {
    return new Left<>(get());
  } else {
    return new Right<>(getLeft());
  }
}

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

/**
 * Returns this as {@code Validation}.
 *
 * @return {@code Validation.valid(get())} if this is right, otherwise {@code Validation.invalid(getLeft())}.
 */
default Validation<L, R> toValidation() {
  return isRight() ? Validation.valid(get()) : Validation.invalid(getLeft());
}

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

/**
 * Maps either the left or the right side of this disjunction.
 *
 * @param leftMapper  maps the left value if this is a Left
 * @param rightMapper maps the right value if this is a Right
 * @param <X>         The new left type of the resulting Either
 * @param <Y>         The new right type of the resulting Either
 * @return A new Either instance
 */
default <X, Y> Either<X, Y> bimap(Function<? super L, ? extends X> leftMapper, Function<? super R, ? extends Y> rightMapper) {
  Objects.requireNonNull(leftMapper, "leftMapper is null");
  Objects.requireNonNull(rightMapper, "rightMapper is null");
  if (isRight()) {
    return new Right<>(rightMapper.apply(get()));
  } else {
    return new Left<>(leftMapper.apply(getLeft()));
  }
}

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

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

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

@Override
default Iterator<R> iterator() {
  if (isRight()) {
    return Iterator.of(get());
  } else {
    return Iterator.empty();
  }
}

相关文章