Example usage for com.google.common.collect Streams mapWithIndex

List of usage examples for com.google.common.collect Streams mapWithIndex

Introduction

In this page you can find the example usage for com.google.common.collect Streams mapWithIndex.

Prototype

public static <R> Stream<R> mapWithIndex(DoubleStream stream, DoubleFunctionWithIndex<R> function) 

Source Link

Document

Returns a stream consisting of the results of applying the given function to the elements of stream and their indexes in the stream.

Usage

From source file:com.google.errorprone.bugpatterns.argumentselectiondefects.Parameter.java

static ImmutableList<Parameter> createListFromVarSymbols(List<VarSymbol> varSymbols) {
    return Streams
            .mapWithIndex(varSymbols.stream(), (s, i) -> new AutoValue_Parameter(s.getSimpleName().toString(),
                    s.asType(), (int) i, s.getSimpleName().toString(), Kind.IDENTIFIER, false))
            .collect(toImmutableList());
}

From source file:com.google.errorprone.bugpatterns.argumentselectiondefects.Parameter.java

static ImmutableList<Parameter> createListFromExpressionTrees(List<? extends ExpressionTree> trees) {
    return Streams
            .mapWithIndex(trees.stream(),
                    (t, i) -> new AutoValue_Parameter(getArgumentName(t),
                            Optional.ofNullable(ASTHelpers.getResultType(t)).orElse(Type.noType), (int) i,
                            t.toString(), t.getKind(), ASTHelpers.constValue(t) != null))
            .collect(toImmutableList());
}

From source file:io.prestosql.execution.scheduler.FixedSourcePartitionedScheduler.java

@Override
public ScheduleResult schedule() {
    // schedule a task on every node in the distribution
    List<RemoteTask> newTasks = ImmutableList.of();
    if (!scheduledTasks) {
        OptionalInt totalPartitions = OptionalInt.of(nodes.size());
        newTasks = Streams
                .mapWithIndex(nodes.stream(),
                        (node, id) -> stage.scheduleTask(node, toIntExact(id), totalPartitions))
                .filter(Optional::isPresent).map(Optional::get).collect(toImmutableList());
        scheduledTasks = true;/*from  w  ww .j  ava2  s. c o  m*/
    }

    boolean allBlocked = true;
    List<ListenableFuture<?>> blocked = new ArrayList<>();
    BlockedReason blockedReason = BlockedReason.NO_ACTIVE_DRIVER_GROUP;

    if (groupedLifespanScheduler.isPresent()) {
        // Start new driver groups on the first scheduler if necessary,
        // i.e. when previous ones have finished execution (not finished scheduling).
        //
        // Invoke schedule method to get a new SettableFuture every time.
        // Reusing previously returned SettableFuture could lead to the ListenableFuture retaining too many listeners.
        blocked.add(groupedLifespanScheduler.get().schedule(sourceSchedulers.get(0)));
    }

    int splitsScheduled = 0;
    Iterator<SourceScheduler> schedulerIterator = sourceSchedulers.iterator();
    List<Lifespan> driverGroupsToStart = ImmutableList.of();
    boolean shouldInvokeNoMoreDriverGroups = false;
    while (schedulerIterator.hasNext()) {
        SourceScheduler sourceScheduler = schedulerIterator.next();

        for (Lifespan lifespan : driverGroupsToStart) {
            sourceScheduler.startLifespan(lifespan, partitionHandleFor(lifespan));
        }
        if (shouldInvokeNoMoreDriverGroups) {
            sourceScheduler.noMoreLifespans();
        }

        ScheduleResult schedule = sourceScheduler.schedule();
        splitsScheduled += schedule.getSplitsScheduled();
        if (schedule.getBlockedReason().isPresent()) {
            blocked.add(schedule.getBlocked());
            blockedReason = blockedReason.combineWith(schedule.getBlockedReason().get());
        } else {
            verify(schedule.getBlocked().isDone(), "blockedReason not provided when scheduler is blocked");
            allBlocked = false;
        }

        driverGroupsToStart = sourceScheduler.drainCompletedLifespans();

        if (schedule.isFinished()) {
            stage.schedulingComplete(sourceScheduler.getPlanNodeId());
            schedulerIterator.remove();
            sourceScheduler.close();
            shouldInvokeNoMoreDriverGroups = true;
        } else {
            shouldInvokeNoMoreDriverGroups = false;
        }
    }

    if (allBlocked) {
        return new ScheduleResult(sourceSchedulers.isEmpty(), newTasks, whenAnyComplete(blocked), blockedReason,
                splitsScheduled);
    } else {
        return new ScheduleResult(sourceSchedulers.isEmpty(), newTasks, splitsScheduled);
    }
}

From source file:com.google.googlejavaformat.java.JavaInputAstVisitor.java

private static <T> Stream<Long> indexes(Stream<T> stream, Predicate<T> predicate) {
    return Streams.mapWithIndex(stream, (x, i) -> predicate.apply(x) ? i : -1).filter(x -> x != -1);
}