Reconstruct codes.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.tasks;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class DoubleTask extends Task {
|
||||
|
||||
Task a, b;
|
||||
|
||||
public DoubleTask(Task a, Task b) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void executeTask() throws Throwable {
|
||||
a.executeTask(); b.executeTask();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInfo() {
|
||||
return "Double Task";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -28,8 +28,7 @@ public class ParallelTask extends Task {
|
||||
Collection<Task> dependsTask = new HashSet<>();
|
||||
|
||||
@Override
|
||||
public boolean executeTask() {
|
||||
return true;
|
||||
public void executeTask() {
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.jackhuang.hellominecraft.tasks;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import org.jackhuang.hellominecraft.HMCLog;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -27,10 +28,8 @@ public abstract class Task {
|
||||
|
||||
/**
|
||||
* Run in a new thread(packed in TaskList).
|
||||
*
|
||||
* @return is task finished sucessfully.
|
||||
*/
|
||||
public abstract boolean executeTask();
|
||||
public abstract void executeTask() throws Throwable;
|
||||
|
||||
/**
|
||||
* if this func returns false, TaskList will force abort the thread. run in
|
||||
@@ -101,4 +100,20 @@ public abstract class Task {
|
||||
ppl = p;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Task after(Task t) {
|
||||
return new DoubleTask(this, t);
|
||||
}
|
||||
|
||||
public Task before(Task t) {
|
||||
return new DoubleTask(t, this);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
executeTask();
|
||||
} catch(Throwable t) {
|
||||
HMCLog.err("Failed to execute task", t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,13 +117,14 @@ public class TaskList extends Thread {
|
||||
for (DoingDoneListener<Task> d : t.getTaskListeners())
|
||||
d.onDoing(t);
|
||||
|
||||
boolean returns = false;
|
||||
boolean flag = true;
|
||||
try {
|
||||
returns = t.executeTask();
|
||||
t.executeTask();
|
||||
} catch (Throwable e) {
|
||||
t.setFailReason(e);
|
||||
flag = false;
|
||||
}
|
||||
if (returns) {
|
||||
if (flag) {
|
||||
HMCLog.log((t.isAborted() ? "Task aborted: " : "Task finished: ") + t.getInfo());
|
||||
for (DoingDoneListener<Task> d : taskListener)
|
||||
d.onDone(t);
|
||||
|
||||
@@ -29,15 +29,13 @@ public class TaskRunnable extends TaskInfo {
|
||||
this.r = r;
|
||||
}
|
||||
|
||||
public TaskRunnable(Runnable r) {
|
||||
this("TaskRunnable", r);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean executeTask() {
|
||||
try {
|
||||
r.run();
|
||||
return true;
|
||||
} catch (Throwable t) {
|
||||
setFailReason(t);
|
||||
return false;
|
||||
}
|
||||
public void executeTask() {
|
||||
r.run();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -36,16 +36,10 @@ public class TaskRunnableArg1<T> extends TaskInfo implements PreviousResultRegis
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean executeTask() {
|
||||
public void executeTask() throws Exception {
|
||||
if (al.size() != 1)
|
||||
throw new IllegalStateException("the count of args is not one.");
|
||||
try {
|
||||
r.accept(al.get(0).getResult());
|
||||
return true;
|
||||
} catch (Throwable t) {
|
||||
setFailReason(t);
|
||||
return false;
|
||||
}
|
||||
r.accept(al.get(0).getResult());
|
||||
}
|
||||
|
||||
ArrayList<PreviousResult<T>> al = new ArrayList();
|
||||
|
||||
@@ -31,9 +31,9 @@ public class ContentGetAndShowTask extends HTTPGetTask implements Event<String>
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean executeTask() {
|
||||
public void executeTask() throws Exception {
|
||||
tdtsl.register(this);
|
||||
return super.executeTask();
|
||||
super.executeTask();
|
||||
}
|
||||
|
||||
String info;
|
||||
|
||||
@@ -92,7 +92,7 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
|
||||
|
||||
// Download file.
|
||||
@Override
|
||||
public boolean executeTask() {
|
||||
public void executeTask() throws Throwable {
|
||||
for (PreviousResult<String> p : al)
|
||||
this.url = IOUtils.parseURL(p.getResult());
|
||||
|
||||
@@ -113,15 +113,13 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
|
||||
|
||||
// Make sure response code is in the 200 range.
|
||||
if (connection.getResponseCode() / 100 != 2) {
|
||||
setFailReason(new NetException(C.i18n("download.not_200") + " " + connection.getResponseCode()));
|
||||
return false;
|
||||
throw new NetException(C.i18n("download.not_200") + " " + connection.getResponseCode());
|
||||
}
|
||||
|
||||
// Check for valid content length.
|
||||
int contentLength = connection.getContentLength();
|
||||
if (contentLength < 1) {
|
||||
setFailReason(new NetException("The content length is invalid."));
|
||||
return false;
|
||||
throw new NetException("The content length is invalid.");
|
||||
}
|
||||
|
||||
filePath.getParentFile().mkdirs();
|
||||
@@ -173,17 +171,17 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
|
||||
}
|
||||
if (ppl != null)
|
||||
ppl.onProgressProviderDone(this);
|
||||
return true;
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
setFailReason(new NetException(C.i18n("download.failed") + " " + url, e));
|
||||
} finally {
|
||||
closeFiles();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
if (failReason != null) throw failReason;
|
||||
}
|
||||
|
||||
public static void download(String url, String file, DownloadListener dl) {
|
||||
public static void download(String url, String file, DownloadListener dl) throws Throwable {
|
||||
((Task) new FileDownloadTask(url, new File(file)).setProgressProviderListener(dl)).executeTask();
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,8 @@ public class HTTPGetTask extends TaskInfo implements PreviousResult<String> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean executeTask() {
|
||||
public void executeTask() throws Exception {
|
||||
Exception t = null;
|
||||
for (int repeat = 0; repeat < 6; repeat++) {
|
||||
if (repeat > 0)
|
||||
HMCLog.warn("Failed to download, repeat: " + repeat);
|
||||
@@ -65,16 +66,16 @@ public class HTTPGetTask extends TaskInfo implements PreviousResult<String> {
|
||||
if (ppl != null)
|
||||
ppl.setProgress(this, ++read, size);
|
||||
if (!shouldContinue)
|
||||
return true;
|
||||
return;
|
||||
}
|
||||
result = baos.toString();
|
||||
tdtsl.execute(result);
|
||||
return true;
|
||||
return;
|
||||
} catch (Exception ex) {
|
||||
setFailReason(new NetException("Failed to get " + url, ex));
|
||||
t = new NetException("Failed to get " + url, ex);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
if (t != null) throw t;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -21,5 +21,5 @@ package org.jackhuang.hellominecraft.utils.functions;
|
||||
*/
|
||||
public interface BiConsumer<V, V2> {
|
||||
|
||||
void onDone(V value, V2 value2);
|
||||
void call(V value, V2 value2);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user