+ final Table<FileKind, String, Supplier<String>> generatedFiles = generator.generateFileContent(
+ ignoreDuplicateFiles);
+
+ // Step two: create generation tasks for each target file and group them by parent directory
+ final ListMultimap<Path, GenerationTask> dirs = MultimapBuilder.hashKeys().arrayListValues().build();
+ for (Cell<FileKind, String, Supplier<String>> cell : generatedFiles.cellSet()) {
+ final File target;
+ switch (cell.getRowKey()) {
+ case PERSISTENT:
+ target = new File(persistentSourcesDir, cell.getColumnKey());
+ if (target.exists()) {
+ LOG.debug("Skipping existing persistent {}", target);
+ continue;
+ }
+ break;
+ case TRANSIENT:
+ target = new File(outputBaseDir, cell.getColumnKey());
+ break;
+ default:
+ throw new IllegalStateException("Unsupported file type in " + cell);
+ }
+
+ dirs.put(target.getParentFile().toPath(), new GenerationTask(buildContext, target, cell.getValue()));
+ }
+ LOG.info("Generating {} Binding source files into {} directories", dirs.size(), dirs.keySet().size());
+
+ // Step three: wrap common FJ pool, submit parent directory creation tasks and wait for them to complete
+ sw.reset().start();
+ final ListeningExecutorService service = MoreExecutors.listeningDecorator(ForkJoinPool.commonPool());
+ final List<ListenableFuture<Void>> parentFutures = new ArrayList<>(dirs.keySet().size());
+ for (Entry<Path, Collection<GenerationTask>> entry : dirs.asMap().entrySet()) {
+ parentFutures.add(service.submit(() -> {
+ Files.createDirectories(entry.getKey());
+ return null;
+ }));
+ }
+
+ try {
+ Futures.whenAllComplete(parentFutures).call(() -> {
+ for (ListenableFuture<Void> future : parentFutures) {
+ Futures.getDone(future);
+ }
+ return null;
+ }, MoreExecutors.directExecutor()).get();
+ } catch (InterruptedException e) {
+ throw new IOException("Interrupted while creating parent directories", e);
+ } catch (ExecutionException e) {
+ LOG.debug("Failed to create parent directories", e);
+ Throwables.throwIfInstanceOf(e.getCause(), IOException.class);
+ throw new IOException("Failed to create parent directories", e);
+ }
+ LOG.debug("Parent directories created in {}", sw);