Example #1
0
  /**
   * Trigger a lazy stream and return the results in the Collection created by the collector
   *
   * @param collector Supplier that creates a collection to store results in
   * @return Collection of results
   */
  default <A, R> R run(Collector<U, A, R> collector) {
    if (getLastActive().isSequential()) {
      // if single threaded we can simply push from each Future into the collection to be returned
      if (collector.supplier().get() == null) {
        forEach(r -> {});
        return null;
      }
      A col = collector.supplier().get();
      forEach(r -> collector.accumulator().accept(col, r));
      return collector.finisher().apply(col);
    }

    Function<FastFuture<U>, U> safeJoin =
        (FastFuture<U> cf) -> (U) BlockingStreamHelper.getSafe(cf, getErrorHandler());
    LazyResultConsumer<U> batcher =
        collector.supplier().get() != null
            ? getLazyCollector().get().withResults(new ArrayList<>())
            : new EmptyCollector<>(this.getMaxActive(), safeJoin);

    try {

      this.getLastActive()
          .injectFutures()
          .forEach(
              n -> {
                batcher.accept(n);
              });
    } catch (SimpleReactProcessingException e) {

    }
    if (collector.supplier().get() == null) {
      batcher.block(safeJoin);
      return null;
    }

    return (R)
        batcher
            .getAllResults()
            .stream()
            .map(cf -> BlockingStreamHelper.getSafe(cf, getErrorHandler()))
            .filter(v -> v != MissingValue.MISSING_VALUE)
            .collect((Collector) collector);
  }