OSDN Git Service

共通化
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / manager / TaskManage.java
index b38531d..52c0c4e 100644 (file)
@@ -61,52 +61,41 @@ public class TaskManage {
         return false;
     }
 
-    private class DownloadListener implements WorkerListener<DownloadResult, DownloadProgress> {
-
-        private final int rowId;
+    private class DownloadListener extends TaskManageInnerListener<DownloadResult, DownloadProgress> {
 
         private DownloadListener(int rowId) {
-            this.rowId = rowId;
-        }
-
-        private void notify(TaskStatus status) {
-            notify(status, 0.0, "");
-        }
-
-        private void notify(TaskStatus status, double percentage, String message) {
-            if (clientListener == null) {
-                return;
-            }
-            clientListener.process(rowId, TaskKind.DOWNLOAD, status, percentage, message);
+            super(rowId);
         }
 
         @Override
-        public void process(DownloadProgress progress) {
-            // TOOD
-            notify(TaskStatus.DOING, 0.0, "");
+        public void done(DownloadResult result) {
+            super.done(result);
+            // TODO 変換が必要なら変換キューに入れる
         }
 
         @Override
-        public void cancelled() {
-            notify(TaskStatus.CANCELLED);
+        protected TaskKind getKind() {
+            return TaskKind.DOWNLOAD;
         }
+    }
 
-        @Override
-        public void done(DownloadResult result) {
-            notify(TaskStatus.DONE);
+    private class ConvertListener extends TaskManageInnerListener<ConvertResult, ConvertProgress> {
+
+        private ConvertListener(int rowId) {
+            super(rowId);
         }
 
         @Override
-        public void error(Throwable th) {
-            notify(TaskStatus.ERROR, 0.0, th.getMessage());
+        protected TaskKind getKind() {
+            return TaskKind.CONVERT;
         }
     }
 
-    private class ConvertListener implements WorkerListener<ConvertResult, ConvertProgress> {
+    abstract class TaskManageInnerListener<T, V> implements WorkerListener<T, V> {
 
         private final int rowId;
 
-        private ConvertListener(int rowId) {
+        protected TaskManageInnerListener(int rowId) {
             this.rowId = rowId;
         }
 
@@ -115,14 +104,20 @@ public class TaskManage {
         }
 
         private void notify(TaskStatus status, double percentage, String message) {
-            if (clientListener == null) {
+            if (getListener() == null) {
                 return;
             }
-            clientListener.process(rowId, TaskKind.DOWNLOAD, status, percentage, message);
+            getListener().process(rowId, getKind(), status, percentage, message);
         }
 
+        private TaskManageListener getListener() {
+            return clientListener;
+        }
+
+        protected abstract TaskKind getKind();
+
         @Override
-        public void process(ConvertProgress progress) {
+        public void process(V progress) {
             // TOOD
             notify(TaskStatus.DOING, 0.0, "");
         }
@@ -133,7 +128,7 @@ public class TaskManage {
         }
 
         @Override
-        public void done(ConvertResult result) {
+        public void done(T result) {
             notify(TaskStatus.DONE);
         }
 
@@ -142,23 +137,23 @@ public class TaskManage {
             notify(TaskStatus.ERROR, 0.0, th.getMessage());
         }
     }
-}
 
-class ManageTarget<T> {
+    class ManageTarget<T> {
 
-    private final RequestProcess request;
-    private final Future<T> future;
+        private final RequestProcess request;
+        private final Future<T> future;
 
-    ManageTarget(RequestProcess request, Future<T> future) {
-        this.request = request;
-        this.future = future;
-    }
+        ManageTarget(RequestProcess request, Future<T> future) {
+            this.request = request;
+            this.future = future;
+        }
 
-    Future<T> getFuture() {
-        return future;
-    }
+        Future<T> getFuture() {
+            return future;
+        }
 
-    RequestProcess getRequest() {
-        return request;
+        RequestProcess getRequest() {
+            return request;
+        }
     }
-}
+}
\ No newline at end of file