svn: Refactored "GeneralRunner", "SeparatePiecesRunner" and "TaskDescriptor"

Code simplified
This commit is contained in:
Konstantin Kolosovsky
2016-10-11 04:28:23 +03:00
parent 289f68e966
commit 89feb50e83
3 changed files with 16 additions and 93 deletions
@@ -20,35 +20,18 @@ import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
public abstract class TaskDescriptor {
// this also means that it would be called in case of chain cancel()
private boolean myHaveMagicCure;
private final String myName;
@NotNull
private final Where myWhere;
private final Map<Object, Object> mySurviveKit;
@NotNull private final Where myWhere;
public TaskDescriptor(final String name, @NotNull final Where where) {
myName = name;
myWhere = where;
mySurviveKit = new HashMap<>();
}
public abstract void run(final ContinuationContext context);
public final void addCure(final Object disaster, final Object cure) {
mySurviveKit.put(disaster, cure);
}
@Nullable
public final Object hasCure(final Object disaster) {
return mySurviveKit.get(disaster);
}
public String getName() {
return myName;
}
@@ -59,11 +42,7 @@ public abstract class TaskDescriptor {
}
public boolean isHaveMagicCure() {
return myHaveMagicCure;
}
public void setHaveMagicCure(boolean haveMagicCure) {
myHaveMagicCure = haveMagicCure;
return false;
}
public void canceled() {
@@ -23,11 +23,6 @@ import org.jetbrains.annotations.Nullable;
import java.util.*;
/**
* @author irengrig
* Date: 4/7/11
* Time: 2:44 PM
*/
abstract class GeneralRunner implements ContinuationContext {
protected final Project myProject;
protected final boolean myCancellable;
@@ -35,8 +30,6 @@ abstract class GeneralRunner implements ContinuationContext {
protected final Object myQueueLock;
private boolean myTriggerSuspend;
private ProgressIndicator myIndicator;
protected final Map<Object, Object> myDisasters;
private final List<Consumer<TaskDescriptor>> myTasksPatchers;
private final Map<Class<? extends Exception>, Consumer<Exception>> myHandlersMap;
GeneralRunner(final Project project, boolean cancellable) {
@@ -44,23 +37,18 @@ abstract class GeneralRunner implements ContinuationContext {
myCancellable = cancellable;
myQueueLock = new Object();
myQueue = new LinkedList<>();
myDisasters = new HashMap<>();
myHandlersMap = new HashMap<>();
myTasksPatchers = new ArrayList<>();
myTriggerSuspend = false;
}
public <T extends Exception> void addExceptionHandler(final Class<T> clazz, final Consumer<T> consumer) {
synchronized (myQueueLock) {
myHandlersMap.put(clazz, new Consumer<Exception>() {
@Override
public void consume(Exception e) {
if (!clazz.isAssignableFrom(e.getClass())) {
throw new RuntimeException(e);
}
//noinspection unchecked
consumer.consume((T)e);
myHandlersMap.put(clazz, e -> {
if (!clazz.isAssignableFrom(e.getClass())) {
throw new RuntimeException(e);
}
//noinspection unchecked
consumer.consume((T)e);
});
}
}
@@ -99,9 +87,7 @@ abstract class GeneralRunner implements ContinuationContext {
@CalledInAny
public void cancelEverything() {
synchronized (myQueueLock) {
for (TaskDescriptor descriptor : myQueue) {
descriptor.canceled();
}
myQueue.forEach(TaskDescriptor::canceled);
myQueue.clear();
myIndicator = null;
}
@@ -125,26 +111,15 @@ abstract class GeneralRunner implements ContinuationContext {
}
}
private void patchTasks(final List<TaskDescriptor> next) {
for (TaskDescriptor descriptor : next) {
for (Consumer<TaskDescriptor> tasksPatcher : myTasksPatchers) {
tasksPatcher.consume(descriptor);
}
}
}
@CalledInAny
public void next(TaskDescriptor... next) {
synchronized (myQueueLock) {
final List<TaskDescriptor> asList = Arrays.asList(next);
patchTasks(asList);
myQueue.addAll(0, asList);
myQueue.addAll(0, Arrays.asList(next));
}
}
public void next(List<TaskDescriptor> next) {
synchronized (myQueueLock) {
patchTasks(next);
myQueue.addAll(0, next);
}
}
@@ -158,20 +133,9 @@ abstract class GeneralRunner implements ContinuationContext {
synchronized (myQueueLock) {
if (myQueue.isEmpty()) return null;
TaskDescriptor current = myQueue.remove(0);
// check if some tasks were scheduled after disaster was thrown, anyway, they should also be checked for cure
if (! current.isHaveMagicCure()) {
if (myIndicator != null && myIndicator.isCanceled()) {
continue;
} else {
for (Map.Entry<Object, Object> entry : myDisasters.entrySet()) {
if (! entry.getValue().equals(current.hasCure(entry.getKey()))) {
current = null;
break;
}
}
}
if (current.isHaveMagicCure() || myIndicator == null || !myIndicator.isCanceled()) {
return current;
}
if (current != null) return current;
}
}
}
@@ -15,25 +15,20 @@
*/
package com.intellij.util.continuation;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.CalledInAwt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author irengrig
* Date: 4/7/11
* Time: 2:46 PM
*/
import static com.intellij.openapi.application.ApplicationManager.getApplication;
public class SeparatePiecesRunner extends GeneralRunner {
private final AtomicReference<TaskWrapper> myCurrentWrapper;
@@ -45,17 +40,7 @@ public class SeparatePiecesRunner extends GeneralRunner {
@CalledInAwt
public void ping() {
clearSuspend();
final Application application = ApplicationManager.getApplication();
if (! application.isDispatchThread()) {
Runnable command = new Runnable() {
public void run() {
pingImpl();
}
};
SwingUtilities.invokeLater(command);
} else {
pingImpl();
}
UIUtil.invokeLaterIfNeeded(this::pingImpl);
}
@CalledInAwt
@@ -82,12 +67,7 @@ public class SeparatePiecesRunner extends GeneralRunner {
else {
final TaskWrapper task = new TaskWrapper(myProject, current.getName(), myCancellable, current);
myCurrentWrapper.set(task);
if (ApplicationManager.getApplication().isUnitTestMode()) {
setIndicator(new EmptyProgressIndicator());
}
else {
setIndicator(new BackgroundableProcessIndicator(task));
}
setIndicator(getApplication().isUnitTestMode() ? new EmptyProgressIndicator() : new BackgroundableProcessIndicator(task));
ProgressManager.getInstance().runProcessWithProgressAsynchronously(task, getIndicator());
return;
}