All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.convertapi.client.ConversionResult Maven / Gradle / Ivy

package com.convertapi.client;

import com.convertapi.client.model.ConversionResponse;
import com.convertapi.client.model.ConversionResponseFile;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

@SuppressWarnings("WeakerAccess")
public class ConversionResult {
    private final ConversionResponse response;

    @SuppressWarnings("unused")
    public ConversionResult(ConversionResponse responseFuture) {
        this.response = responseFuture;
    }

    @SuppressWarnings("unused")
    public Integer fileCount() {
        return response.Files.length;
    }

    public List urls() {
        @SuppressWarnings("unchecked") List valueList = new ArrayList();
        for (ConversionResponseFile file : response.Files) valueList.add(file.Url);
        return valueList;
    }

    @SuppressWarnings("unused")
    public Integer conversionCost() {
        return response.ConversionCost;
    }

    public ConversionResultFile getFile(int index) {
        if (index < 0) index = response.Files.length + index;
        return new ConversionResultFile(response.Files[index]);
    }

    public CompletableFuture asStream() {
        return getFile(0).asStream();
    }

    public CompletableFuture saveFile(Path file) {
        return getFile(0).saveFile(file);
    }

    @SuppressWarnings("WeakerAccess")
    public List> saveFiles(Path directory) {
        if (!Files.isDirectory(directory))
            throw new RuntimeException("Directory expected, but received: " + directory.toString());
        List> paths = new ArrayList<>();
        for (int i = 0; i < response.Files.length; i++) {
            paths.add(getFile(i).saveFile(directory));
        }
        return paths;
    }

    @SuppressWarnings("unused")
    public List saveFilesSync(Path directory) {
        List> futures = saveFiles(directory);
        return futures.stream().map(p -> {
            try {
                return p.get();
            } catch (InterruptedException | ExecutionException e) {
                throw new RuntimeException(e);
            }
        }).collect(Collectors.toList());
    }

    @SuppressWarnings("WeakerAccess")
    public List delete() {
        List futures = new ArrayList<>();
        for (int i = 0; i < response.Files.length; i++) {
            futures.add(getFile(i).delete());
        }
        return futures;
    }

    @SuppressWarnings("WeakerAccess")
    public void deleteSync() {
        delete().forEach(d -> {
            try {
                d.get();
            } catch (InterruptedException | ExecutionException e) {
                throw new RuntimeException(e);
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy