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

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

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

Option.get介绍

[英]Gets the value if this is a Some or throws if this is a None.
[中]如果这是Some,则获取该值;如果这是None,则抛出该值。

代码示例

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

@Override
  public T getNext() {
    final T next =  nextOption.get();
    nextOption = null;
    return next;
  }
};

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

@Override
  public U getNext() {
    final U result = nextVal.get()._1;
    nextVal = f.apply(nextVal.get()._2);
    return result;
  }
};

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

@Override
public T head() {
  if (isEmpty()) {
    throw new NoSuchElementException("head of empty TreeSet");
  } else {
    return tree.min().get();
  }
}

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

@Override
public T last() {
  if (isEmpty()) {
    throw new NoSuchElementException("last of empty TreeSet");
  } else {
    return tree.max().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

@Override
public TreeMap<K, V> init() {
  if (isEmpty()) {
    throw new UnsupportedOperationException("init of empty TreeMap");
  } else {
    final Tuple2<K, V> max = entries.max().get();
    return new TreeMap<>(entries.delete(max));
  }
}

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

@SuppressWarnings("unchecked")
static <K, V, M extends Map<K, V>> Tuple2<V, M> computeIfAbsent(M map, K key, Function<? super K, ? extends V> mappingFunction) {
  Objects.requireNonNull(mappingFunction, "mappingFunction is null");
  final Option<V> value = map.get(key);
  if (value.isDefined()) {
    return Tuple.of(value.get(), map);
  } else {
    final V newValue = mappingFunction.apply(key);
    final M newMap = (M) map.put(key, newValue);
    return Tuple.of(newValue, newMap);
  }
}

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

/**
 * Gets the value if the computation result is a {@code Success} or throws if it was a {@code Failure}.
 * Waits for the result if necessary by blocking the current thread.
 * <p>
 * <strong>IMPORTANT! If the computation result is a {@link Try.Failure}, the underlying {@code cause} of type {@link Throwable} is thrown.</strong>
 *
 * @return The value of this {@code Future}.
 */
@Override
default T get() {
  return await().getValue().get().get();
}

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

/**
 * Checks, if this future has a value.
 *
 * @return true, if this future succeeded with a value, false otherwise.
 */
@Override
default boolean isEmpty() {
  return await().getValue().get().isEmpty();
}

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

/**
 * Checks if this Future completed with a success.
 *
 * @return true, if this Future completed and is a Success, false otherwise.
 */
default boolean isSuccess() {
  return isCompleted() && getValue().get().isSuccess();
}

代码示例来源: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
public TreeSet<T> init() {
  if (isEmpty()) {
    throw new UnsupportedOperationException("init of empty TreeSet");
  } else {
    return new TreeSet<>(tree.delete(tree.max().get()));
  }
}

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

@Override
public TreeSet<T> tail() {
  if (isEmpty()) {
    throw new UnsupportedOperationException("tail of empty TreeSet");
  } else {
    return new TreeSet<>(tree.delete(tree.min().get()));
  }
}

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

/**
 * Checks if this Future completed with a failure.
 *
 * @return true, if this Future completed and is a Failure, false otherwise.
 */
default boolean isFailure() {
  return isCompleted() && getValue().get().isFailure();
}

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

@Override
public TreeMap<K, V> tail() {
  if (isEmpty()) {
    throw new UnsupportedOperationException("tail of empty TreeMap");
  } else {
    final Tuple2<K, V> min = entries.min().get();
    return new TreeMap<>(entries.delete(min));
  }
}

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

@SuppressWarnings("unchecked")
static <K, V, M extends Map<K, V>> Tuple2<Option<V>, M> computeIfPresent(M map, K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
  final Option<V> value = map.get(key);
  if (value.isDefined()) {
    final V newValue = remappingFunction.apply(key, value.get());
    final M newMap = (M) map.put(key, newValue);
    return Tuple.of(Option.of(newValue), newMap);
  } else {
    return Tuple.of(Option.none(), map);
  }
}

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

@SuppressWarnings("unchecked")
private <K2, V2> Multimap<K2, V2> createFromEntries(Iterable<? extends Tuple2<? extends K2, ? extends V2>> entries) {
  Map<K2, Traversable<V2>> back = emptyMapSupplier();
  for (Tuple2<? extends K2, ? extends V2> entry : entries) {
    if (back.containsKey(entry._1)) {
      back = back.put(entry._1, containerType.add(back.get(entry._1).get(), entry._2));
    } else {
      back = back.put(entry._1, containerType.add(emptyContainer.get(), entry._2));
    }
  }
  return createFromMap(back);
}

相关文章