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

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

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

Option.of介绍

[英]Creates a new Option of a given value.
[中]创建给定值的新选项。

代码示例

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

/**
 * Alias for {@link Option#of(Object)}
 *
 * @param <T>   type of the value
 * @param value A value
 * @return {@link Option.Some} if value is not {@code null}, {@link Option.None} otherwise
 */
public static <T> Option<T> Option(T value) {
  return Option.of(value);
}

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

/**
   * {@inheritDoc}
   */
  @Override
  public Option<T> take() {
    return Option.of(queue.poll());
  }
}

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

@Override
default Option<T> findLast(Predicate<? super T> predicate) {
  Objects.requireNonNull(predicate, "predicate is null");
  T last = null;
  while (hasNext()) {
    final T elem = next();
    if (predicate.test(elem)) {
      last = elem;
    }
  }
  return Option.of(last);
}

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

Option<String> getConfiguredColumnName(int tupleIndex, ConfigRegistry config) {
  return Option.of(config.get(TupleMappers.class)
      .getColumn(tupleIndex));
}

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

@SuppressWarnings("unchecked")
@Override
public Option<T> map(ResultSet r, int columnNumber, StatementContext ctx) throws SQLException {
  final ColumnMapper<?> mapper = ctx.findColumnMapperFor(nestedType)
      .orElseThrow(() -> new NoSuchMapperException("No mapper for type " + nestedType + " nested in Option"));
  return (Option<T>) Option.of(mapper.map(r, columnNumber, ctx));
}

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

/**
 * Creates a RateLimiter.
 *
 * @param name              the name of the RateLimiter
 * @param rateLimiterConfig The RateLimiter configuration.
 * @param scheduler         executor that will refresh permissions
 */
public SemaphoreBasedRateLimiter(String name, RateLimiterConfig rateLimiterConfig,
                 ScheduledExecutorService scheduler) {
  this.name = requireNonNull(name, NAME_MUST_NOT_BE_NULL);
  this.rateLimiterConfig = new AtomicReference<>(requireNonNull(rateLimiterConfig, CONFIG_MUST_NOT_BE_NULL));
  this.scheduler = Option.of(scheduler).getOrElse(this::configureScheduler);
  this.semaphore = new Semaphore(this.rateLimiterConfig.get().getLimitForPeriod(), true);
  this.metrics = this.new SemaphoreBasedRateLimiterMetrics();
  this.eventProcessor = new RateLimiterEventProcessor();
  scheduleLimitRefresh();
}

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

Array<Tuple3<Type, Integer, Option<String>>> resolveKeyValueColumns(ConfigRegistry config, Array<Tuple2<Type, Integer>> tupleTypes) {
  Array<Tuple3<Type, Integer, Option<String>>> withConfiguredColumnName;
  Tuple2<Type, Integer> keyType = tupleTypes.get(0);
  Tuple2<Type, Integer> valueType = tupleTypes.get(1);
  withConfiguredColumnName = Array.of(
      Tuple.of(keyType._1, keyType._2, Option.of(config.get(TupleMappers.class).getKeyColumn())),
      Tuple.of(valueType._1, valueType._2, Option.of(config.get(TupleMappers.class).getValueColumn()))
  );
  return withConfiguredColumnName;
}

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

private Option<V> getValueFromCache(K cacheKey){
  try {
    Option<V> result = Option.of(cache.get(cacheKey));
    if (result.isDefined()) {
      onCacheHit(cacheKey);
      return result;
    } else {
      onCacheMiss(cacheKey);
      return result;
    }
  }catch (Exception exception){
    LOG.warn("Failed to get a value from Cache {}", getName(), exception);
    onError(exception);
    return Option.none();
  }
}

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

@Test
public void testGetNonValueArgumentShouldNotBeEmpty() {
  Optional<Argument> arg = unit.build(new GenericType<Option<Integer>>() {}.getType(),
      Option.of(1), null);
  assertThat(arg).isNotEmpty();
}

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

@Test
public void testGetOptionShouldReturnCorrectRow() {
  Something result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME)
      .bind("name", Option.of("eric"))
      .mapToBean(Something.class)
      .findOnly();
  assertThat(result).isEqualTo(ERICSOMETHING);
}

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

@Test
public void testOptionMappedShouldSucceed() {
  final Set<Option<String>> result = dbRule.getSharedHandle()
      .createQuery("select name from something")
      .collectInto(new GenericType<Set<Option<String>>>() {});
  assertThat(result).hasSize(2);
  assertThat(result).contains(Option.none(), Option.of("eric"));
}

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

@Test
public void testOptionMappedWithinObjectIfPresentShouldContainValue() {
  final SomethingWithOption result = dbRule.getSharedHandle()
      .registerRowMapper(ConstructorMapper.factory(SomethingWithOption.class))
      .createQuery("select id, name from something where id = 1")
      .mapTo(SomethingWithOption.class)
      .findOnly();
  assertThat(result.getName()).isInstanceOf(Option.class);
  assertThat(result).isEqualTo(new SomethingWithOption(1, Option.of("eric")));
}

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

/**
 * Alias for {@link Option#of(Object)}
 *
 * @param <T>   type of the value
 * @param value A value
 * @return {@link Option.Some} if value is not {@code null}, {@link Option.None} otherwise
 */
public static <T> Option<T> Option(T value) {
  return Option.of(value);
}

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

@Override
default Option<T> findLast(Predicate<? super T> predicate) {
  Objects.requireNonNull(predicate, "predicate is null");
  T last = null;
  while (hasNext()) {
    final T elem = next();
    if (predicate.test(elem)) {
      last = elem;
    }
  }
  return Option.of(last);
}

代码示例来源:origin: org.janusgraph/janusgraph-cql

Map<String, String> getCompressionOptions(final String name) throws BackendException {
  final KeyspaceMetadata keyspaceMetadata = Option.of(this.cluster.getMetadata().getKeyspace(this.keyspace))
      .getOrElseThrow(() -> new PermanentBackendException(String.format("Unknown keyspace '%s'", this.keyspace)));
  return Option.of(keyspaceMetadata.getTable(name))
      .map(tableMetadata -> tableMetadata.getOptions().getCompression())
      .getOrElseThrow(() -> new PermanentBackendException(String.format("Unknown table '%s'", name)));
}

代码示例来源:origin: com.mercateo.eventstore/client-common

public Option<EventStream> getEventStream(EventStreamId eventStreamId) {
  return Option.of(eventstreams.computeIfAbsent(eventStreamId, definition -> getEventStore(eventStreamId
    .eventStoreName()).map(eventStore -> new EventStream(eventStore, eventStreamId)).getOrNull()));
}

代码示例来源:origin: com.pragmaticobjects.oo.atom/atom-basis

@Override
  public final boolean matches(final TypeDescription target) {
    boolean truth = matcher.matches(target);
    if(!truth) {
      truth = Option.of(target.getSuperClass())
        .map(TypeDescription.Generic::asErasure)
        .map(this::matches)
        .getOrElse(false);
    }
    return truth;
  }
}

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

相关文章