From 8ccd13d2be256870cffae36f015106402c983a83 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Wed, 17 Jun 2015 18:48:56 +0300 Subject: [PATCH] [git] simplify: initialize mySaver in the constructor, use AtomicBoolean instead of synchronized block --- .../git4idea/util/GitPreservingProcess.java | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/plugins/git4idea/src/git4idea/util/GitPreservingProcess.java b/plugins/git4idea/src/git4idea/util/GitPreservingProcess.java index b42c1133e89c..61d5354ca0c0 100644 --- a/plugins/git4idea/src/git4idea/util/GitPreservingProcess.java +++ b/plugins/git4idea/src/git4idea/util/GitPreservingProcess.java @@ -37,14 +37,13 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Collection; +import java.util.concurrent.atomic.AtomicBoolean; import static com.intellij.openapi.util.text.StringUtil.join; /** * Executes a Git operation on a number of repositories surrounding it by stash-unstash procedure. * I.e. stashes changes, executes the operation and then unstashes it. - * - * @author Kirill Likhodedov */ public class GitPreservingProcess { @@ -59,11 +58,9 @@ public class GitPreservingProcess { @NotNull private final ProgressIndicator myProgressIndicator; @NotNull private final Runnable myOperation; @NotNull private final String myStashMessage; + @NotNull private final GitStashChangesSaver mySaver; - // suppressed, because only the load() method needs to be synchronized not to load twice - @SuppressWarnings("FieldAccessedSynchronizedAndUnsynchronized") private GitStashChangesSaver mySaver; - private boolean myLoaded; - private final Object LOAD_LOCK = new Object(); + @NotNull private final AtomicBoolean myLoaded = new AtomicBoolean(); public GitPreservingProcess(@NotNull Project project, @NotNull GitPlatformFacade facade, @NotNull Git git, @NotNull Collection repositories, @@ -79,6 +76,7 @@ public class GitPreservingProcess { myOperation = operation; myStashMessage = String.format("%s %s at %s", StringUtil.capitalize(myOperationTitle), myDestinationName, DateFormatUtil.formatDateTime(Clock.getTime())); + mySaver = configureSaver(); } public void execute() { @@ -90,7 +88,6 @@ public class GitPreservingProcess { @Override public void run() { LOG.debug("starting"); - mySaver = configureSaver(); boolean savedSuccessfully = save(); LOG.debug("save result: " + savedSuccessfully); if (savedSuccessfully) { @@ -114,7 +111,7 @@ public class GitPreservingProcess { } /** - * Configures the saver, actually notifications and texts in the GitConflictResolver used inside. + * Configures the saver: i.e. notifications and texts for the GitConflictResolver used inside. */ private GitStashChangesSaver configureSaver() { GitStashChangesSaver saver = new GitStashChangesSaver(myProject, myFacade, myGit, myProgressIndicator, myStashMessage); @@ -164,13 +161,9 @@ public class GitPreservingProcess { } public void load() { - synchronized (LOAD_LOCK) { - if (myLoaded) { - return; - } + if (myLoaded.compareAndSet(false, true)) { try { mySaver.load(); - myLoaded = true; } catch (VcsException e) { LOG.info("Couldn't load local changes", e); @@ -179,6 +172,8 @@ public class GitPreservingProcess { join(e.getMessages()))); } } + else { + LOG.warn("The changes were already loaded", new Throwable()); + } } - }