Repository navigation
Expand file tree
/
Copy pathConversionResult.java
More file actions
94 lines (80 loc) · 2.89 KB
/
Copy pathConversionResult.java
File metadata and controls
94 lines (80 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.convertapi;
import com.convertapi.model.ConversionResponse;
import com.convertapi.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<String> urls() {
@SuppressWarnings("unchecked") List<String> 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<InputStream> asStream() {
return getFile(0).asStream();
}
public CompletableFuture<Path> saveFile(Path file) {
return getFile(0).saveFile(file);
}
@SuppressWarnings("WeakerAccess")
public List<CompletableFuture<Path>> saveFiles(Path directory) {
if (!Files.isDirectory(directory)) throw new RuntimeException("Directory expected, but received: " + directory.toString());
List<CompletableFuture<Path>> paths = new ArrayList<>();
for (int i = 0; i < response.Files.length; i++) {
paths.add(getFile(i).saveFile(directory));
}
return paths;
}
@SuppressWarnings("unused")
public List<Path> saveFilesSync(Path directory) {
List<CompletableFuture<Path>> 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<CompletableFuture> delete() {
List<CompletableFuture> 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);
}
});
}
}