From 89feb50e831203c56db54ac5d2ef1eb959258c83 Mon Sep 17 00:00:00 2001 From: Konstantin Kolosovsky Date: Tue, 27 Sep 2016 17:57:35 +0300 Subject: [PATCH] svn: Refactored "GeneralRunner", "SeparatePiecesRunner" and "TaskDescriptor" Code simplified --- .../util/continuation/TaskDescriptor.java | 25 +-------- .../util/continuation/GeneralRunner.java | 54 ++++--------------- .../continuation/SeparatePiecesRunner.java | 30 ++--------- 3 files changed, 16 insertions(+), 93 deletions(-) diff --git a/platform/vcs-api/src/com/intellij/util/continuation/TaskDescriptor.java b/platform/vcs-api/src/com/intellij/util/continuation/TaskDescriptor.java index 170089250c78..73db0a79d07d 100644 --- a/platform/vcs-api/src/com/intellij/util/continuation/TaskDescriptor.java +++ b/platform/vcs-api/src/com/intellij/util/continuation/TaskDescriptor.java @@ -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 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() { diff --git a/platform/vcs-impl/src/com/intellij/util/continuation/GeneralRunner.java b/platform/vcs-impl/src/com/intellij/util/continuation/GeneralRunner.java index e62873ec9eb8..9dd6e3a8f7c6 100644 --- a/platform/vcs-impl/src/com/intellij/util/continuation/GeneralRunner.java +++ b/platform/vcs-impl/src/com/intellij/util/continuation/GeneralRunner.java @@ -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 myDisasters; - private final List> myTasksPatchers; private final Map, Consumer> 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 void addExceptionHandler(final Class clazz, final Consumer consumer) { synchronized (myQueueLock) { - myHandlersMap.put(clazz, new Consumer() { - @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 next) { - for (TaskDescriptor descriptor : next) { - for (Consumer tasksPatcher : myTasksPatchers) { - tasksPatcher.consume(descriptor); - } - } - } - @CalledInAny public void next(TaskDescriptor... next) { synchronized (myQueueLock) { - final List asList = Arrays.asList(next); - patchTasks(asList); - myQueue.addAll(0, asList); + myQueue.addAll(0, Arrays.asList(next)); } } public void next(List 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 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; } } } diff --git a/platform/vcs-impl/src/com/intellij/util/continuation/SeparatePiecesRunner.java b/platform/vcs-impl/src/com/intellij/util/continuation/SeparatePiecesRunner.java index d4b61d45c224..29c5c2fed9f5 100644 --- a/platform/vcs-impl/src/com/intellij/util/continuation/SeparatePiecesRunner.java +++ b/platform/vcs-impl/src/com/intellij/util/continuation/SeparatePiecesRunner.java @@ -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 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; }