Example usage for com.google.common.util.concurrent Futures getChecked

List of usage examples for com.google.common.util.concurrent Futures getChecked

Introduction

In this page you can find the example usage for com.google.common.util.concurrent Futures getChecked.

Prototype

@GwtIncompatible("reflection")
public static <V, X extends Exception> V getChecked(Future<V> future, Class<X> exceptionClass) throws X 

Source Link

Document

Returns the result of Future#get() , converting most exceptions to a new instance of the given checked exception type.

Usage

From source file:com.android.tools.idea.gradle.structure.model.repositories.search.ArtifactRepositorySearch.java

@NotNull
public Callback start(@NotNull SearchRequest request) {
    Callback callback = new Callback();

    List<Future<SearchResult>> jobs = Lists.newArrayListWithExpectedSize(myRepositories.size());
    List<SearchResult> results = Lists.newArrayList();
    List<Exception> errors = Lists.newArrayList();

    Application application = ApplicationManager.getApplication();
    application.executeOnPooledThread(() -> {
        for (ArtifactRepository repository : myRepositories) {
            jobs.add(application.executeOnPooledThread(() -> repository.search(request)));
        }//  w w w.j a va2 s.  com

        for (Future<SearchResult> job : jobs) {
            try {
                results.add(Futures.getChecked(job, Exception.class));
            } catch (Exception e) {
                errors.add(e);
            }
        }
        callback.setDone(results, errors);
    });
    return callback;
}

From source file:com.android.tools.idea.run.DeviceFutures.java

/** @return the target devices, if all are now ready. Otherwise, null. */
@Nullable//  ww  w.j a v  a 2s.  co  m
public List<IDevice> getIfReady() {
    List<ListenableFuture<IDevice>> devices = get();

    for (ListenableFuture<IDevice> deviceFuture : devices) {
        if (!deviceFuture.isDone() || deviceFuture.isCancelled()) {
            return null;
        }
    }

    try {
        return Futures.getChecked(Futures.allAsList(devices), ExecutionException.class);
    } catch (Exception e) {
        // This can happen if the process behind the future threw an exception.
        return null;
    }
}

From source file:com.android.tools.idea.gradle.structure.daemon.PsLibraryUpdateCheckerDaemon.java

private void search(@NotNull Collection<ArtifactRepository> repositories,
        @NotNull Collection<LibraryUpdateId> ids) {
    myRunning.set(true);//from   w w  w.j av a2s.co  m
    getAvailableUpdates().clear();

    int resultCount = repositories.size() * ids.size();
    List<Future<SearchResult>> jobs = Lists.newArrayListWithExpectedSize(resultCount);

    Set<SearchRequest> requests = Sets.newHashSet();
    ids.forEach(id -> {
        SearchRequest request = new SearchRequest(id.getName(), id.getGroupId(), 1, 0);
        requests.add(request);
    });

    Set<SearchResult> results = Sets.newHashSet();
    List<Exception> errors = Lists.newArrayList();

    Application application = ApplicationManager.getApplication();
    application.executeOnPooledThread(() -> {
        for (ArtifactRepository repository : repositories) {
            for (SearchRequest request : requests) {
                jobs.add(application.executeOnPooledThread(() -> repository.search(request)));
            }
        }

        for (Future<SearchResult> job : jobs) {
            try {
                SearchResult result = Futures.getChecked(job, Exception.class);
                List<FoundArtifact> artifacts = result.getArtifacts();
                if (artifacts.size() == 1) {
                    FoundArtifact artifact = artifacts.get(0);
                    if (!artifact.getVersions().isEmpty()) {
                        results.add(result);
                    }
                }
            } catch (Exception e) {
                errors.add(e);
            }
        }

        AvailableLibraryUpdates updates = getAvailableUpdates();

        for (SearchResult result : results) {
            List<FoundArtifact> artifacts = result.getArtifacts();
            updates.add(artifacts.get(0));
        }

        updates.lastSearchTimeMillis = System.currentTimeMillis();

        myResultsUpdaterQueue.queue(new UpdatesAvailable());
    });
}