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

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

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

Either.getLeft介绍

[英]Returns the left value.
[中]返回左值。

代码示例

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

/**
 * Runs an action in the case this is a projection on a Left value.
 *
 * @param action an action which consumes a Left value
 */
default void orElseRun(Consumer<? super L> action) {
  Objects.requireNonNull(action, "action is null");
  if (isLeft()) {
    action.accept(getLeft());
  }
}

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

default Either<L, R> peekLeft(Consumer<? super L> action) {
  Objects.requireNonNull(action, "action is null");
  if (isLeft()) {
    action.accept(getLeft());
  }
  return 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

/**
 * Applies the given action to the value if the projected either is a Left. Otherwise nothing happens.
 *
 * @param action An action which takes a left value
 * @return this LeftProjection
 */
@Override
public LeftProjection<L, R> peek(Consumer<? super L> action) {
  Objects.requireNonNull(action, "action is null");
  if (either.isLeft()) {
    action.accept(either.getLeft());
  }
  return this;
}

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

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

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

/**
 * FlatMaps this LeftProjection.
 *
 * @param mapper A mapper
 * @param <U>    Component type of the mapped left value
 * @return this as {@code LeftProjection<L, U>} if a Right is underlying, otherwise a the mapping result of the left value.
 * @throws NullPointerException if {@code mapper} is null
 */
@SuppressWarnings("unchecked")
public <U> LeftProjection<U, R> flatMap(Function<? super L, ? extends LeftProjection<? extends U, R>> mapper) {
  Objects.requireNonNull(mapper, "mapper is null");
  if (either.isLeft()) {
    return (LeftProjection<U, R>) mapper.apply(either.getLeft());
  } else {
    return (LeftProjection<U, R>) 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 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

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

/**
 * 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
public Iterator<L> iterator() {
  if (either.isLeft()) {
    return Iterator.of(either.getLeft());
  } 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());
  }
}

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

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

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

相关文章