io.trane.future.Future.flatMap()方法的使用及代码示例

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

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

Future.flatMap介绍

[英]Maps the result of this future to another future and flattens the result.
[中]将此未来的结果映射到另一个未来,并将结果展平。

代码示例

代码示例来源:origin: traneio/future

@Override
 final Future<R> apply(final Future<T> result) {
  return result.flatMap(f);
 }
}

代码示例来源:origin: traneio/future

/**
 * Flattens a nested future. The usage of this method indicates a code smell:
 * a map may have been used instead of flatMap. There are genuine scenarios
 * where flatten is required, though.
 *
 * @param fut  the nested future
 * @param <T>  the type of the future result
 * @return     the flat future
 */
public static <T> Future<T> flatten(final Future<Future<T>> fut) {
 return fut.flatMap(f -> f);
}

代码示例来源:origin: traneio/future

@Override
public <U, R> Future<R> biFlatMap(final Future<U> other,
  final BiFunction<? super T, ? super U, ? extends Future<R>> f) {
 return other.flatMap(u -> f.apply(value, u));
}

代码示例来源:origin: traneio/future

@Benchmark
public String flatMapConst() throws CheckedFutureException {
 return constFuture.flatMap(flatMapF).get(Duration.ofMillis(Long.MAX_VALUE));
}

代码示例来源:origin: traneio/ndbc

private final Future<Void> scheduleValidation(final Duration validationInterval,
  final ScheduledExecutorService scheduler) {
 return Future.VOID.delayed(validationInterval, scheduler).flatMap(v1 -> {
  final long start = System.currentTimeMillis();
  return validateN(items.size()).flatMap(v2 -> {
   final long next = validationInterval.toMillis() - System.currentTimeMillis() - start;
   if (next <= 0)
    return scheduleValidation(validationInterval, scheduler);
   else
    return scheduleValidation(Duration.ofMillis(next), scheduler);
  });
 });
}

代码示例来源:origin: traneio/ndbc

private final Future<Void> drain() {
 final T item = items.poll();
 if (item == null)
  return Future.VOID;
 else
  return item.close().flatMap(v -> drain());
}

代码示例来源:origin: traneio/future

@Benchmark
public String flatMapConstN() throws CheckedFutureException {
 Future<String> f = constFuture;
 for (int i = 0; i < N.n; i++)
  f = f.flatMap(flatMapF);
 return f.get(Duration.ofMillis(Long.MAX_VALUE));
}

代码示例来源:origin: traneio/future

/**
 * Executes the supplier function while the condition is valid.
 *
 * @param cond  a supplier that determines if the while should stop or not
 * @param f     the body of the while that is executed on each asynchronous
 *              iteration
 * @return      a void future that is satisfied when the while stops.
 */
public static <T> Future<Void> whileDo(final Supplier<Boolean> cond, final Supplier<Future<T>> f) {
 return Tailrec.apply(() -> {
  if (cond.get())
   return f.get().flatMap(r -> whileDo(cond, f));
  else
   return VOID;
 });
}

代码示例来源:origin: traneio/ndbc

default public <R> Exchange<R> flatMap(final Function<T, Exchange<R>> f) {
 return channel -> run(channel).flatMap(v -> f.apply(v).run(channel));
}

代码示例来源:origin: traneio/ndbc

default public <R> Exchange<R> then(final Exchange<R> ex) {
 return channel -> run(channel).flatMap(v -> ex.run(channel));
}

代码示例来源:origin: traneio/ndbc

private final <T> Future<T> withConnection(final Function<Connection, Future<T>> f) {
  final Optional<Connection> transaction = currentTransaction.get();
  if (transaction.isPresent())
    return f.apply(transaction.get());
  else
    return pool.acquire().flatMap(c -> {
      return Future.flatApply(() -> f.apply(c)).ensure(() -> pool.release(c));
    });
}

代码示例来源:origin: traneio/ndbc

private final Future<Void> validateN(final int n) {
 if (n >= 0) {
  final T item = items.poll();
  if (item == null)
   return Future.VOID;
  else
   return item.isValid().rescue(e -> Future.FALSE).flatMap(valid -> {
    if (!valid)
     return item.close().rescue(e -> Future.VOID).ensure(() -> sizeSemaphore.release());
    else {
     items.offer(item);
     return Future.VOID;
    }
   }).flatMap(v -> validateN(n - 1));
 } else
  return Future.VOID;
}

代码示例来源:origin: traneio/ndbc

default public Exchange<T> onSuccess(final Function<T, Exchange<?>> f) {
 return channel -> run(channel).flatMap(v -> f.apply(v).run(channel).map(ign -> v));
}

代码示例来源:origin: traneio/ndbc

private final <T> InterruptHandler handler(final Promise<T> p) {
 return ex -> backendKeyData.ifPresent(data -> channelSupplier.get()
   .flatMap(channel -> Exchange
     .send(marshallers.cancelRequest, new CancelRequest(data.processId, data.secretKey))
     .then(Exchange.CLOSE).run(channel))
   .onFailure(e -> log.warn("Can't cancel request. Reason: " + e)));
}

代码示例来源:origin: traneio/future

private Future<Integer> loop(int i) {
 return Tailrec.apply(() -> {
  if (i > 0)
   return Future.value(i - 1).flatMap(this::loop);
  else
   return Future.value(0);
 });
}

代码示例来源:origin: traneio/future

@Benchmark
public String flatMapPromiseN() throws CheckedFutureException {
 Promise<String> p = Promise.<String>apply();
 Future<String> f = p;
 for (int i = 0; i < N.n; i++)
  f = f.flatMap(flatMapF);
 p.setValue(string);
 return f.get(Duration.ofMillis(Long.MAX_VALUE));
}

代码示例来源:origin: traneio/ndbc

default public Exchange<T> onFailure(final Function<Throwable, Exchange<?>> e) {
 return channel -> run(channel).rescue(ex -> e.apply(ex).run(channel).flatMap(v -> Future.exception(ex)));
}

代码示例来源:origin: traneio/ndbc

@Override
public final <T> Future<T> transactional(final Supplier<Future<T>> supplier) {
  if (currentTransaction.get().isPresent())
    return Future.flatApply(supplier);
  else
    return pool.acquire().flatMap(c -> {
      currentTransaction.set(Optional.of(c));
      return c.beginTransaction().flatMap(v -> supplier.get()).transformWith(new Transformer<T, Future<T>>() {
        @Override
        public Future<T> onException(final Throwable ex) {
          currentTransaction.set(Optional.empty());
          return c.rollback().flatMap(v -> Future.exception(ex));
        }
        @Override
        public Future<T> onValue(final T value) {
          currentTransaction.set(Optional.empty());
          return c.commit().map(v -> value);
        }
      }).ensure(() -> pool.release(c));
    });
}

代码示例来源:origin: traneio/ndbc

public final Flow<Row> stream(final PreparedStatement query) {
 Future<Fetch> fetch = run(extendedQueryStreamExchange.apply(query.query(), query.params()));
 return Flow.batched(i -> {
  Future<Flow<Row>> fut = fetch.flatMap(f -> run(f.fetch(i.intValue()))).map(Flow::from);
  return Flow.from(fut);
 });
}

代码示例来源:origin: traneio/ndbc

@Override
public Flow<Row> stream(PreparedStatement query) {
 Future<Fetch> fetch = run(extendedQueryStreamExchange.apply(query.query(), query.params()));
 return Flow.batched(i -> {
  Future<Flow<Row>> fut = fetch.flatMap(f -> run(f.apply(i.intValue()))).map(Flow::from);
  return Flow.from(fut);
 });
}

相关文章