OSDN Git Service

クラス名変更
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / manager / TaskManage.java
1 package yukihane.inqubus.manager;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.concurrent.ExecutorService;
6 import java.util.concurrent.Executors;
7 import java.util.concurrent.Future;
8 import java.util.concurrent.FutureTask;
9 import saccubus.worker.convert.Convert;
10 import saccubus.worker.convert.ConvertResult;
11 import saccubus.worker.download.Download;
12 import saccubus.worker.download.DownloadResult;
13 import saccubus.worker.profile.ConvertProfile;
14 import saccubus.worker.profile.DownloadProfile;
15
16
17 /**
18  *
19  * @author yuki
20  */
21 public class TaskManage {
22
23     private final ExecutorService downloadExecutorService;
24     private final ExecutorService convertExecutorService;
25     private final Map<Integer, ManageTarget<DownloadResult>> downloadTargets = new HashMap<>();
26     private final Map<Integer, ManageTarget<ConvertResult>> convertTargets = new HashMap<>();
27
28     public TaskManage(int maxDownload, int maxConvert) {
29         downloadExecutorService = Executors.newFixedThreadPool(maxDownload);
30         convertExecutorService = Executors.newFixedThreadPool(maxConvert);
31     }
32
33     public synchronized boolean add(Request request) {
34         final DownloadProfile dp = request.getDownloadProfile();
35         final ConvertProfile cp = request.getConvertProfile();
36         if (dp != null && !dp.getVideoProfile().isDownload() && !dp.getCommentProfile().isDownload()) {
37             final Download task = new Download(dp, request.getVideoId(),
38                     request.getDownloadProgressListener());
39             final Future<DownloadResult> future = downloadExecutorService.submit(task);
40             downloadTargets.put(request.getRowId(), new ManageTarget<>(request, future));
41             return true;
42
43         } else if (cp != null && cp.isConvert()) {
44             final Convert task = new Convert(cp, dp.getVideoProfile().getLocalFile(), dp.getCommentProfile().
45                     getLocalFile(), request.getConvertProgressListener());
46             final Future<ConvertResult> future = convertExecutorService.submit(task);
47             convertTargets.put(request.getRowId(), new ManageTarget<>(request, future));
48             return true;
49         }
50         return false;
51     }
52 }
53 class ManageTarget<T> {
54
55     private final Request request;
56     private final Future<T> future;
57
58     ManageTarget(Request request, Future<T> future) {
59         this.request = request;
60         this.future = future;
61     }
62
63     Future<T> getFuture() {
64         return future;
65     }
66
67     Request getRequest() {
68         return request;
69     }
70 }