MultiDownload.java

package ui.common.download;

import javafx.scene.Scene;
import ui.common.UIUtil;
import ui.entities.config.Configuration;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.function.Consumer;

public class MultiDownload {

    private final UIUtil util;
    private final Configuration cfg;
    private final Scene scene;
    private final Consumer<File> downloadedFileConsumer;

    public MultiDownload(UIUtil util, Scene scene, Consumer<File> downloadedFileConsumer) {
        this.util = util;
        this.cfg = util.getConfig();
        this.scene = scene;
        this.downloadedFileConsumer = downloadedFileConsumer;
    }

    public void download(List<String> urls) {
        try {
            if (urls.isEmpty()) {
                return;
            }
            new DownloadThread(cfg, new ProgressListener(util, scene) {

                @Override
                public void downloaded(final File downloadedFile) {
                    if (urls.size() > 1) {
                        download(urls.subList(1, urls.size()));
                    } else {
                        downloadedFileConsumer.accept(downloadedFile);
                    }
                }

            }, new URI(urls.get(0)).toURL(), true).start();
        } catch (MalformedURLException | URISyntaxException e) {
            e.printStackTrace();
        }
    }
}