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

x33g5p2x  于2022-01-25 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(120)

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

Option.isEmpty介绍

[英]Returns true, if this is None, otherwise false, if this is Some.
[中]如果没有,则返回true;如果有,则返回false。

代码示例

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

/**
 * Returns true, if this is {@code Some}, otherwise false, if this is {@code None}.
 * <p>
 * Please note that it is possible to create {@code new Some(null)}, which is defined.
 *
 * @return true, if this {@code Option} has a defined value, false otherwise
 */
default boolean isDefined() {
  return !isEmpty();
}

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

/**
 * Returns this {@code Option} if it is nonempty, otherwise return the alternative.
 *
 * @param other An alternative {@code Option}
 * @return this {@code Option} if it is nonempty, otherwise return the alternative.
 */
@SuppressWarnings("unchecked")
default Option<T> orElse(Option<? extends T> other) {
  Objects.requireNonNull(other, "other is null");
  return isEmpty() ? (Option<T>) other : this;
}

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

/**
 * Returns this {@code Option} if it is nonempty, otherwise return the result of evaluating supplier.
 *
 * @param supplier An alternative {@code Option} supplier
 * @return this {@code Option} if it is nonempty, otherwise return the result of evaluating supplier.
 */
@SuppressWarnings("unchecked")
default Option<T> orElse(Supplier<? extends Option<? extends T>> supplier) {
  Objects.requireNonNull(supplier, "supplier is null");
  return isEmpty() ? (Option<T>) supplier.get() : this;
}

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

/**
 * Runs a Java Runnable passed as parameter if this {@code Option} is empty.
 *
 * @param action a given Runnable to be run
 * @return this {@code Option}
 */
default Option<T> onEmpty(Runnable action) {
  Objects.requireNonNull(action, "action is null");
  if (isEmpty()) {
    action.run();
  }
  return this;
}

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

/**
 * Returns the value if this is a {@code Some} or the {@code other} value if this is a {@code None}.
 * <p>
 * Please note, that the other value is eagerly evaluated.
 *
 * @param other An alternative value
 * @return This value, if this Option is defined or the {@code other} value, if this Option is empty.
 */
@Override
default T getOrElse(T other) {
  return isEmpty() ? other : get();
}

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

/**
 * Returns the value if this is a {@code Some}, otherwise the {@code other} value is returned,
 * if this is a {@code None}.
 * <p>
 * Please note, that the other value is lazily evaluated.
 *
 * @param supplier An alternative value supplier
 * @return This value, if this Option is defined or the {@code other} value, if this Option is empty.
 */
@Override
default T getOrElse(Supplier<? extends T> supplier) {
  Objects.requireNonNull(supplier, "supplier is null");
  return isEmpty() ? supplier.get() : get();
}

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

/**
 * Returns the value if this is a {@code Some}, otherwise throws an exception.
 *
 * @param exceptionSupplier An exception supplier
 * @param <X>               A throwable
 * @return This value, if this Option is defined, otherwise throws X
 * @throws X a throwable
 */
@Override
default <X extends Throwable> T getOrElseThrow(Supplier<X> exceptionSupplier) throws X {
  Objects.requireNonNull(exceptionSupplier, "exceptionSupplier is null");
  if (isEmpty()) {
    throw exceptionSupplier.get();
  } else {
    return get();
  }
}

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

/**
 * Returns {@code Some(value)} if this is a {@code Some} and the value satisfies the given predicate.
 * Otherwise {@code None} is returned.
 *
 * @param predicate A predicate which is used to test an optional value
 * @return {@code Some(value)} or {@code None} as specified
 */
default Option<T> filter(Predicate<? super T> predicate) {
  Objects.requireNonNull(predicate, "predicate is null");
  return isEmpty() || predicate.test(get()) ? this : none();
}

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

/**
 * Maps the value to a new {@code Option} if this is a {@code Some}, otherwise returns {@code None}.
 *
 * @param mapper A mapper
 * @param <U>    Component type of the resulting Option
 * @return a new {@code Option}
 */
@SuppressWarnings("unchecked")
default <U> Option<U> flatMap(Function<? super T, ? extends Option<? extends U>> mapper) {
  Objects.requireNonNull(mapper, "mapper is null");
  return isEmpty() ? none() : (Option<U>) mapper.apply(get());
}

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

/**
 * Maps the value and wraps it in a new {@code Some} if this is a {@code Some}, returns {@code None}.
 *
 * @param mapper A value mapper
 * @param <U>    The new value type
 * @return a new {@code Some} containing the mapped value if this Option is defined, otherwise {@code None}, if this is empty.
 */
@Override
default <U> Option<U> map(Function<? super T, ? extends U> mapper) {
  Objects.requireNonNull(mapper, "mapper is null");
  return isEmpty() ? none() : some(mapper.apply(get()));
}

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

@SuppressWarnings("unchecked")
static <K, V, U extends V, M extends Map<K, V>> M put(M map, K key, U value,
    BiFunction<? super V, ? super U, ? extends V> merge) {
  Objects.requireNonNull(merge, "the merge function is null");
  final Option<V> currentValue = map.get(key);
  if (currentValue.isEmpty()) {
    return (M) map.put(key, value);
  } else {
    return (M) map.put(key, merge.apply(currentValue.get(), value));
  }
}

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

static <K, V, U extends V, M extends Map<K, V>> M put(M map, Tuple2<? extends K, U> entry,
    BiFunction<? super V, ? super U, ? extends V> merge) {
  Objects.requireNonNull(merge, "the merge function is null");
  final Option<V> currentValue = map.get(entry._1);
  if (currentValue.isEmpty()) {
    return put(map, entry);
  } else {
    return put(map, entry.map2(value -> merge.apply(currentValue.get(), value)));
  }
}

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

@Override
public boolean hasNext() {
  while (next.isEmpty() && that.hasNext()) {
    final T candidate = that.next();
    if (predicate.test(candidate)) {
      next = Option.some(candidate);
    }
  }
  return next.isDefined();
}

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

@Override
public String toString() {
  final Option<Try<T>> value = this.value;
  final String s = (value == null || value.isEmpty()) ? "?" : value.get().toString();
  return stringPrefix() + "(" + s + ")";
}

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

@Override
default Iterator<T> iterator() {
  return isEmpty() ? Iterator.empty() : Iterator.of(get());
}

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

/**
 * Reduces many {@code Option}s into a single {@code Option} by transforming an
 * {@code Iterable<Option<? extends T>>} into a {@code Option<Seq<T>>}. If any of
 * the Options are {@link Option.None}, then this returns {@link Option.None}.
 *
 * @param values An {@code Iterable} of {@code Option}s
 * @param <T>    type of the Options
 * @return An {@code Option} of a {@link Seq} of results
 * @throws NullPointerException if {@code values} is null
 */
static <T> Option<Seq<T>> sequence(Iterable<? extends Option<? extends T>> values) {
  Objects.requireNonNull(values, "values is null");
  Vector<T> vector = Vector.empty();
  for (Option<? extends T> value : values) {
    if (value.isEmpty()) {
      return Option.none();
    }
    vector = vector.append(value.get());
  }
  return Option.some(vector);
}

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

/**
 * Maps the cause to a new exception if this is a {@code Failure} or returns this instance if this is a {@code Success}.
 * <p>
 * If none of the given cases matches the cause, the same {@code Failure} is returned.
 *
 * @param cases A not necessarily exhaustive sequence of cases that will be matched against a cause.
 * @return A new {@code Try} if this is a {@code Failure}, otherwise this.
 */
@GwtIncompatible
@SuppressWarnings({ "unchecked", "varargs" })
default Try<T> mapFailure(Match.Case<? extends Throwable, ? extends Throwable>... cases) {
  if (isSuccess()) {
    return this;
  } else {
    final Option<Throwable> x = Match(getCause()).option(cases);
    return x.isEmpty() ? this : failure(x.get());
  }
}

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

/**
 * Matches each element with a unique key that you extract from it.
 * If the same key is present twice, the function will return {@code None}.
 *
 * @param getKey A function which extracts a key from elements
 * @param <K>    key class type
 * @return A Map containing the elements arranged by their keys.
 * @throws NullPointerException if {@code getKey} is null.
 * @see #groupBy(Function)
 */
default <K> Option<Map<K, T>> arrangeBy(Function<? super T, ? extends K> getKey) {
  return Option.of(groupBy(getKey).mapValues(Traversable<T>::singleOption))
      .filter(map -> !map.exists(kv -> kv._2.isEmpty()))
      .map(map -> Map.narrow(map.mapValues(Option::get)));
}

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

assertThat(exceptionBuffer.take().get()).isInstanceOf(IOException.class);
assertThat(exceptionBuffer.take().get()).isInstanceOf(IOException.class);
assertThat(exceptionBuffer.take().isEmpty()).isTrue();

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

/**
 * Returns this {@code Option} if it is nonempty, otherwise return the alternative.
 *
 * @param other An alternative {@code Option}
 * @return this {@code Option} if it is nonempty, otherwise return the alternative.
 */
@SuppressWarnings("unchecked")
default Option<T> orElse(Option<? extends T> other) {
  Objects.requireNonNull(other, "other is null");
  return isEmpty() ? (Option<T>) other : this;
}

相关文章