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

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

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

Either.isRight介绍

[英]Returns whether this Either is a Right.
[中]返回这两者是否为右。

代码示例

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

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

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

@SuppressWarnings("unchecked")
default Either<L, R> orElse(Either<? extends L, ? extends R> other) {
  Objects.requireNonNull(other, "other is null");
  return isRight() ? this : (Either<L, R>) other;
}

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

@SuppressWarnings("unchecked")
public RightProjection<L, R> orElse(RightProjection<? extends L, ? extends R> other) {
  Objects.requireNonNull(other, "other is null");
  return either.isRight() ? this : (RightProjection<L, R>) other;
}

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

@SuppressWarnings("unchecked")
public RightProjection<L, R> orElse(Supplier<? extends RightProjection<? extends L, ? extends R>> supplier) {
  Objects.requireNonNull(supplier, "supplier is null");
  return either.isRight() ? this : (RightProjection<L, R>) supplier.get();
}

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

@SuppressWarnings("unchecked")
default Either<L, R> orElse(Supplier<? extends Either<? extends L, ? extends R>> supplier) {
  Objects.requireNonNull(supplier, "supplier is null");
  return isRight() ? this : (Either<L, R>) supplier.get();
}

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

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

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

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

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

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

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

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

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

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

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

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

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

/**
 * 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());
  }
}

相关文章