Example usage for com.google.common.collect ImmutableList forEach

List of usage examples for com.google.common.collect ImmutableList forEach

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableList forEach.

Prototype

default void forEach(Consumer<? super T> action) 

Source Link

Document

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

Usage

From source file:com.facebook.buck.rage.InteractiveReport.java

@Override
public ImmutableSet<BuildLogEntry> promptForBuildSelection() throws IOException {
    ImmutableList<BuildLogEntry> buildLogs = buildLogHelper.getBuildLogs();

    // Commands with unknown args and buck rage should be excluded.
    List<BuildLogEntry> interestingBuildLogs = new ArrayList<>();
    buildLogs.forEach(entry -> {
        if (entry.getCommandArgs().isPresent() && !entry.getCommandArgs().get().matches("rage|doctor")) {
            interestingBuildLogs.add(entry);
        }// ww w .  j  av a2 s.  c o m
    });

    if (interestingBuildLogs.isEmpty()) {
        return ImmutableSet.of();
    }

    // Sort the interesting builds based on time, reverse order so the most recent is first.
    Collections.sort(interestingBuildLogs,
            Ordering.natural().onResultOf(BuildLogEntry::getLastModifiedTime).reverse());

    return input.selectRange("Which buck invocations would you like to report?", interestingBuildLogs,
            input1 -> {
                Pair<Double, SizeUnit> humanReadableSize = SizeUnit.getHumanReadableSize(input1.getSize(),
                        SizeUnit.BYTES);
                return String.format("\t%s\tbuck [%s] %s (%.2f %s)", input1.getLastModifiedTime(),
                        input1.getCommandArgs().orElse("unknown command"),
                        prettyPrintExitCode(input1.getExitCode()), humanReadableSize.getFirst(),
                        humanReadableSize.getSecond().getAbbreviation());
            });
}

From source file:com.facebook.buck.remoteexecution.util.MultiThreadedBlobUploader.java

private void processUploads() {
    processMissing();//from  ww w.  ja  v  a2  s.  co m
    ImmutableMap.Builder<String, PendingUpload> dataBuilder = ImmutableMap.builder();
    int size = 0;
    while (size < uploadSizeLimit && !waitingUploads.isEmpty()) {
        PendingUpload data = waitingUploads.poll();
        if (data == null) {
            break;
        }
        dataBuilder.put(data.getHash(), data);
        size += data.uploadData.digest.getSize();
    }
    ImmutableMap<String, PendingUpload> data = dataBuilder.build();

    if (!data.isEmpty()) {
        try {
            ImmutableList.Builder<UploadData> blobsBuilder = ImmutableList.builder();
            for (PendingUpload entry : data.values()) {
                blobsBuilder.add(entry.uploadData);
            }

            ImmutableList<UploadData> blobs = data.values().stream().map(e -> e.uploadData)
                    .collect(ImmutableList.toImmutableList());

            ImmutableList<UploadResult> results = asyncBlobUploader.batchUpdateBlobs(blobs);
            Preconditions.checkState(results.size() == blobs.size());
            results.forEach(result -> {
                PendingUpload pendingUpload = Objects.requireNonNull(data.get(result.digest.getHash()));
                if (result.status == 0) {
                    pendingUpload.future.set(null);
                } else {
                    pendingUpload.future.setException(new IOException(
                            String.format("Failed uploading with message: %s. When uploading blob: %s.",
                                    result.message, pendingUpload.uploadData.data.describe())));
                }
            });
            data.forEach((k, pending) -> pending.future.setException(new RuntimeException("idk")));
        } catch (Exception e) {
            data.forEach((k, pending) -> pending.future.setException(e));
        }
    }
    if (!waitingMissingCheck.isEmpty() || !waitingUploads.isEmpty()) {
        uploadService.submit(this::processUploads);
    }
}

From source file:com.facebook.buck.rules.modern.ModernBuildRule.java

@Override
public final ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    ImmutableList.Builder<Path> outputsBuilder = ImmutableList.builder();
    recordOutputs(outputsBuilder::add);//from w  w  w  .  j a v a  2  s. co m
    ImmutableList<Path> outputs = outputsBuilder.build();
    outputs.forEach(buildableContext::recordArtifact);
    return stepsForBuildable(context, buildable, getProjectFilesystem(), getBuildTarget(), outputs);
}

From source file:com.facebook.buck.rules.modern.builders.MultiThreadedBlobUploader.java

private void processUploads() {
    processMissing();/*  www  . ja va2  s .  c o  m*/
    ImmutableMap.Builder<String, PendingUpload> dataBuilder = ImmutableMap.builder();
    int size = 0;
    while (size < uploadSizeLimit && !waitingUploads.isEmpty()) {
        PendingUpload data = waitingUploads.poll();
        if (data == null) {
            break;
        }
        dataBuilder.put(data.getHash(), data);
        size += data.uploadData.digest.getSize();
    }
    ImmutableMap<String, PendingUpload> data = dataBuilder.build();

    if (!data.isEmpty()) {
        try {
            ImmutableList.Builder<UploadData> blobsBuilder = ImmutableList.builder();
            for (PendingUpload entry : data.values()) {
                blobsBuilder.add(entry.uploadData);
            }

            ImmutableList<UploadData> blobs = data.values().stream().map(e -> e.uploadData)
                    .collect(ImmutableList.toImmutableList());

            ImmutableList<UploadResult> results = asyncBlobUploader.batchUpdateBlobs(blobs);
            Preconditions.checkState(results.size() == blobs.size());
            results.forEach(result -> {
                PendingUpload pendingUpload = Preconditions.checkNotNull(data.get(result.digest.getHash()));
                if (result.status == 0) {
                    pendingUpload.future.set(null);
                } else {
                    pendingUpload.future.setException(new IOException(
                            String.format("Failed uploading with message: %s", result.message)));
                }
            });
            data.forEach((k, pending) -> pending.future.setException(new RuntimeException("idk")));
        } catch (Exception e) {
            data.forEach((k, pending) -> pending.future.setException(e));
        }
    }
    if (!waitingMissingCheck.isEmpty() || !waitingUploads.isEmpty()) {
        uploadService.submit(this::processUploads);
    }
}

From source file:com.facebook.buck.cli.VerifyCachesCommand.java

private boolean verifyRuleKeyCache(CellProvider cellProvider, PrintStream stdOut,
        RuleKeyConfiguration ruleKeyConfiguration, FileHashCache fileHashCache,
        RuleKeyCacheRecycler<RuleKey> recycler) {
    ImmutableList<Map.Entry<BuildRule, RuleKey>> contents = recycler.getCachedBuildRules();
    RuleKeyFieldLoader fieldLoader = new RuleKeyFieldLoader(ruleKeyConfiguration);
    ActionGraphBuilder graphBuilder = new SingleThreadedActionGraphBuilder(TargetGraph.EMPTY,
            new DefaultTargetNodeToBuildRuleTransformer(), cellProvider);
    contents.forEach(e -> graphBuilder.addToIndex(e.getKey()));
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(graphBuilder);
    SourcePathResolver pathResolver = DefaultSourcePathResolver.from(ruleFinder);
    DefaultRuleKeyFactory defaultRuleKeyFactory = new DefaultRuleKeyFactory(fieldLoader, fileHashCache,
            pathResolver, ruleFinder);/*from  ww w.j a v  a2 s  .c  o m*/
    stdOut.println(String.format("Examining %d build rule keys.", contents.size()));
    ImmutableList<BuildRule> mismatches = RichStream.from(contents)
            .filter(entry -> !defaultRuleKeyFactory.build(entry.getKey()).equals(entry.getValue()))
            .map(Map.Entry::getKey).toImmutableList();
    if (mismatches.isEmpty()) {
        stdOut.println("No rule key cache errors found.");
    } else {
        stdOut.println("Found rule key cache errors:");
        for (BuildRule rule : mismatches) {
            stdOut.println(String.format("  %s", rule));
        }
    }
    return true;
}

From source file:com.facebook.buck.core.graph.transformation.executor.impl.ToposortDepsAwareWorker.java

@Override
protected boolean eval(ToposortBasedDepsAwareTask<T> task) throws InterruptedException {
    if (!task.compareAndSetStatus(ToposortBasedDepsAwareTask.TaskStatus.SCHEDULED,
            ToposortBasedDepsAwareTask.TaskStatus.STARTED)) {
        return false;
    }/* w  w  w. j a  v  a  2s.  c o  m*/

    ImmutableSet<? extends ToposortBasedDepsAwareTask<T>> prereqs;
    try {
        prereqs = task.getPrereqs();
    } catch (Exception e) {
        task.getFuture().completeExceptionally(e);
        return true;
    }

    ImmutableList<ToposortBasedDepsAwareTask<T>> notDoneTasks;
    try {
        notDoneTasks = checkTasksReadyOrReschedule(prereqs);
    } catch (ExecutionException e) {
        completeWithException(task, e.getCause());
        return true;
    }

    if (!notDoneTasks.isEmpty()) {
        notDoneTasks.forEach(depTask -> depTask.registerDependant(task));
        // I1. task becomes NOT_SCHEDULED only when all its deps are registered
        Verify.verify(task.compareAndSetStatus(TaskStatus.STARTED, TaskStatus.NOT_SCHEDULED));

        if (task.numOutStandingDependencies.get() != 0
                || !task.compareAndSetStatus(TaskStatus.NOT_SCHEDULED, TaskStatus.STARTED)) {
            return true;
        }
    }

    ImmutableSet<? extends ToposortBasedDepsAwareTask<T>> deps;
    try {
        deps = task.getDependencies();
    } catch (Exception e) {
        task.getFuture().completeExceptionally(e);
        return true;
    }

    try {
        notDoneTasks = checkTasksReadyOrReschedule(deps);
    } catch (ExecutionException e) {
        completeWithException(task, e.getCause());
        return true;
    }

    if (!notDoneTasks.isEmpty()) {
        notDoneTasks.forEach(depTask -> depTask.registerDependant(task));
        // I1. task becomes NOT_SCHEDULED only when all its deps are registered
        Verify.verify(task.compareAndSetStatus(TaskStatus.STARTED, TaskStatus.NOT_SCHEDULED));

        if (task.numOutStandingDependencies.get() != 0
                || !task.compareAndSetStatus(TaskStatus.NOT_SCHEDULED, TaskStatus.STARTED)) {
            return true;
        }
    }

    task.call();
    ImmutableList<ToposortBasedDepsAwareTask<T>> toReschedule = task.reportCompletionToDependents();

    for (ToposortBasedDepsAwareTask<T> taskToSchedule : toReschedule) {
        if (taskToSchedule.compareAndSetStatus(TaskStatus.NOT_SCHEDULED, TaskStatus.SCHEDULED)) {
            // at this point, we know that all its deps have been registered, which means its
            // numOutStandingDependencies can only decrease (see I1)
            if (taskToSchedule.numOutStandingDependencies.get() != 0) {
                // the task may have been incorrectly returned one of its dependents were not done
                // registering dependents before the completion of this task. Check the task count again
                // and don't reschedule if it still has dependents left.
                Verify.verify(
                        taskToSchedule.compareAndSetStatus(TaskStatus.SCHEDULED, TaskStatus.NOT_SCHEDULED));
                // before setting the taskToSchedule back to NOT_SCHEDULED, its possible that its
                // dependents have been completed, but got blocked from scheduling it due to this task
                // having set its status. Now that its unblocked, we should recheck its
                // numOuStandingDependencies.
                if (taskToSchedule.numOutStandingDependencies.get() != 0) {
                    continue;
                }
                if (!taskToSchedule.compareAndSetStatus(TaskStatus.NOT_SCHEDULED, TaskStatus.SCHEDULED)) {
                    continue;
                }
            }
            sharedQueue.putFirst(taskToSchedule);
        }
    }
    return true;
}

From source file:eu.numberfour.n4js.runner.RuntimeEnvironmentsHelper.java

/**
 * Maps passed collection of {@link IN4JSSourceContainerAware} to list of {@link IN4JSProject}, that is instances of
 * {@link IN4JSProject} project are returned as they are, while instances of {@link IN4JSArchive} have contained
 * project extracted. For each result of that transformation, examines its
 * {@link IN4JSProject#getProvidedRuntimeLibraries()} to check if they pass predicate test. Instances that do are
 * stored in the passed collection./* w w  w.j  av  a2 s.  c  o  m*/
 *
 * Calls itself recursively on each processed element, accumulating results in collection passed along call chains.
 *
 * @param runtimeLibraries
 *            list of source containers to analyze (usually of type {@link ProjectType#RUNTIME_LIBRARY})
 * @param collection
 *            where provided runtime libraries are collected
 * @param predicate
 *            to test if provided project is of type {@link ProjectType#RUNTIME_LIBRARY}
 */
private void recursiveProvidedRuntimeLibrariesCollector(
        com.google.common.collect.ImmutableList<? extends IN4JSSourceContainerAware> runtimeLibraries,
        Collection<IN4JSProject> collection, Predicate<IN4JSProject> predicate) {

    runtimeLibraries.forEach(runtimeLibrary -> {
        IN4JSProject project = (extractProject(runtimeLibrary));
        if (predicate.test(project))
            collection.add(project);
        recursiveProvidedRuntimeLibrariesCollector(project.getProvidedRuntimeLibraries(), collection,
                predicate);
    });
}

From source file:com.facebook.buck.cli.Daemon.java

Daemon(Cell rootCell, Optional<WebServer> webServerToReuse) {
    this.rootCell = rootCell;
    this.fileEventBus = new EventBus("file-change-events");

    ImmutableList<Cell> allCells = rootCell.getAllCells();

    // Setup the stacked file hash cache from all cells.
    ImmutableList.Builder<ProjectFileHashCache> hashCachesBuilder = ImmutableList.builder();
    allCells.forEach(subCell -> {
        WatchedFileHashCache watchedCache = new WatchedFileHashCache(subCell.getFilesystem());
        fileEventBus.register(watchedCache);
        hashCachesBuilder.add(watchedCache);
    });//w  w  w .  j  ava2s.c o m
    hashCachesBuilder.add(DefaultFileHashCache.createBuckOutFileHashCache(
            rootCell.getFilesystem().replaceBlacklistedPaths(ImmutableSet.of()),
            rootCell.getFilesystem().getBuckPaths().getBuckOut()));
    this.hashCaches = hashCachesBuilder.build();

    this.broadcastEventListener = new BroadcastEventListener();
    this.actionGraphCache = new ActionGraphCache(broadcastEventListener);
    this.versionedTargetGraphCache = new VersionedTargetGraphCache();

    typeCoercerFactory = new DefaultTypeCoercerFactory();
    this.parser = new Parser(this.broadcastEventListener, rootCell.getBuckConfig().getView(ParserConfig.class),
            typeCoercerFactory, new ConstructorArgMarshaller(typeCoercerFactory));
    fileEventBus.register(parser);
    fileEventBus.register(actionGraphCache);

    // Build the the rule key cache recycler.
    this.defaultRuleKeyFactoryCacheRecycler = RuleKeyCacheRecycler.createAndRegister(fileEventBus,
            new DefaultRuleKeyCache<>(), RichStream.from(allCells).map(Cell::getFilesystem).toImmutableSet());

    if (webServerToReuse.isPresent()) {
        webServer = webServerToReuse;
    } else {
        webServer = createWebServer(rootCell.getBuckConfig(), rootCell.getFilesystem());
    }
    if (!initWebServer()) {
        LOG.warn("Can't start web server");
    }
    if (rootCell.getBuckConfig().getView(ParserConfig.class)
            .getWatchmanCursor() == WatchmanWatcher.CursorType.CLOCK_ID
            && !rootCell.getWatchman().getClockIds().isEmpty()) {
        cursor = rootCell.getWatchman().buildClockWatchmanCursorMap();
    } else {
        LOG.debug("Falling back to named cursors: %s", rootCell.getWatchman().getProjectWatches());
        cursor = rootCell.getWatchman().buildNamedWatchmanCursorMap();
    }
    LOG.debug("Using Watchman Cursor: %s", cursor);
    persistentWorkerPools = new ConcurrentHashMap<>();
    JavaUtilsLoggingBuildListener.ensureLogFileIsWritten(rootCell.getFilesystem());
}

From source file:com.facebook.presto.operator.exchange.LocalExchange.java

private void checkAllSourcesFinished() {
    checkNotHoldsLock(this);

    if (!sources.stream().allMatch(LocalExchangeSource::isFinished)) {
        return;/*from  w  ww. j ava 2 s  .  c  o  m*/
    }

    // all sources are finished, so finish the sinks
    ImmutableList<LocalExchangeSink> openSinks;
    synchronized (this) {
        allSourcesFinished = true;

        openSinks = ImmutableList.copyOf(sinks);
        sinks.clear();
    }

    // since all sources are finished there is no reason to allow new pages to be added
    // this can happen with a limit query
    openSinks.forEach(LocalExchangeSink::finish);
    checkAllSinksComplete();
}

From source file:rocks.devonthe.stickychunk.chunkload.ChunkLoadCallback.java

/**
 * Callback for loading Tickets during world load.
 * <p>//from   ww  w  .  j  av a2 s.  c  om
 * During this callback you cannot associate chunks to tickets. This
 * callback gets all loaded non-player tickets. The returned list will
 * be truncated to maxTickets after this callback is called, and and
 * tickets absent from the list will be released.
 *
 * @param tickets    The list of loaded tickets
 * @param world      The world tickets were loaded for
 * @param maxTickets The maximum tickets allowed for this plugin
 * @return A list of all tickets you wish to keep
 */
@Override
public List<LoadingTicket> onLoaded(ImmutableList<LoadingTicket> tickets, World world, int maxTickets) {
    List<LoadingTicket> personalTickets = Lists.newArrayList();
    List<LoadingTicket> worldTickets = Lists.newArrayList();
    List<LoadingTicket> toKeep = Lists.newArrayList();

    StickyChunk.getInstance().getLogger().info("sorting tickets");

    if (tickets.size() > maxTickets || unassignedRegions.size() > maxTickets) {
        tickets.forEach(ticket -> ticket.getCompanionData().getString(DataQuery.of("type")).ifPresent(type -> {
            switch (type) {
            case "world":
                worldTickets.add(ticket);
                break;
            case "personal":
                personalTickets.add(ticket);
                break;
            default:
                ticket.release();
                break;
            }

            StickyChunk.getInstance().getLogger().info("sorting ticket");
        }));

        toKeep.addAll(worldTickets);
        toKeep.addAll(personalTickets);

        StickyChunk.getInstance().getLogger()
                .info(String.format("Returning sorted tickets of size: %s", toKeep.size()));

        return toKeep;
    } else {
        StickyChunk.getInstance().getLogger()
                .info(String.format("Returning all tickets of size: %s", tickets.size()));

        return tickets;
    }
}