From 705ac9a1e5ef5d214eeedd3b21c78f31a46cd14f Mon Sep 17 00:00:00 2001 From: Konstantin Kolosovsky Date: Thu, 13 Oct 2016 18:04:49 +0300 Subject: [PATCH] svn: Refactored "SvnCommittedChangesProvider" - code simplified, warnings fixed --- .../src/org/jetbrains/idea/svn/SvnVcs.java | 2 +- .../idea/svn/history/SvnChangeList.java | 7 +- .../history/SvnCommittedChangesProvider.java | 197 +++++++----------- 3 files changed, 80 insertions(+), 126 deletions(-) diff --git a/plugins/svn4idea/src/org/jetbrains/idea/svn/SvnVcs.java b/plugins/svn4idea/src/org/jetbrains/idea/svn/SvnVcs.java index 3c9550c2f0d9..3adfbdd707bd 100644 --- a/plugins/svn4idea/src/org/jetbrains/idea/svn/SvnVcs.java +++ b/plugins/svn4idea/src/org/jetbrains/idea/svn/SvnVcs.java @@ -723,7 +723,7 @@ public class SvnVcs extends AbstractVcs { @NotNull public CommittedChangesProvider getCommittedChangesProvider() { if (myCommittedChangesProvider == null) { - myCommittedChangesProvider = new SvnCommittedChangesProvider(myProject); + myCommittedChangesProvider = new SvnCommittedChangesProvider(this); } return myCommittedChangesProvider; } diff --git a/plugins/svn4idea/src/org/jetbrains/idea/svn/history/SvnChangeList.java b/plugins/svn4idea/src/org/jetbrains/idea/svn/history/SvnChangeList.java index 558aada36100..2cb287172d7c 100644 --- a/plugins/svn4idea/src/org/jetbrains/idea/svn/history/SvnChangeList.java +++ b/plugins/svn4idea/src/org/jetbrains/idea/svn/history/SvnChangeList.java @@ -795,13 +795,10 @@ public class SvnChangeList implements CommittedChangeList, VcsRevisionNumberAwar } } - public void forceReloadCachedInfo(final boolean reloadRoot) { + public void forceReloadCachedInfo() { myCachedInfoLoaded = false; myBranchUrl = null; - - if (reloadRoot) { - myWcRoot = null; - } + myWcRoot = null; } @NotNull diff --git a/plugins/svn4idea/src/org/jetbrains/idea/svn/history/SvnCommittedChangesProvider.java b/plugins/svn4idea/src/org/jetbrains/idea/svn/history/SvnCommittedChangesProvider.java index 0c1731da6240..f9e720a04c3d 100644 --- a/plugins/svn4idea/src/org/jetbrains/idea/svn/history/SvnCommittedChangesProvider.java +++ b/plugins/svn4idea/src/org/jetbrains/idea/svn/history/SvnCommittedChangesProvider.java @@ -16,13 +16,10 @@ package org.jetbrains.idea.svn.history; -import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.DefaultActionGroup; -import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.progress.ProgressManager; -import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.registry.Registry; @@ -39,8 +36,6 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.AsynchConsumer; import com.intellij.util.Consumer; import com.intellij.util.PairConsumer; -import com.intellij.util.ThrowableConsumer; -import com.intellij.util.containers.ContainerUtil; import com.intellij.util.messages.MessageBusConnection; import com.intellij.vcsUtil.VcsUtil; import org.jetbrains.annotations.NotNull; @@ -63,16 +58,20 @@ import java.io.DataInput; import java.io.DataOutput; import java.io.File; import java.io.IOException; -import java.util.*; +import java.util.Collection; +import java.util.Date; +import java.util.List; +import java.util.Set; + +import static com.intellij.openapi.application.ApplicationManager.getApplication; +import static com.intellij.util.containers.ContainerUtil.newArrayList; +import static com.intellij.util.containers.ContainerUtil.newHashSet; +import static java.util.Collections.singletonList; -/** - * @author yole - */ public class SvnCommittedChangesProvider implements CachingCommittedChangesProvider { private final static Logger LOG = Logger.getInstance(SvnCommittedChangesProvider.class); - @NotNull private final Project myProject; @NotNull private final SvnVcs myVcs; @NotNull private final MessageBusConnection myConnection; private MergeInfoUpdatesListener myMergeInfoUpdatesListener; @@ -81,30 +80,17 @@ public class SvnCommittedChangesProvider implements CachingCommittedChangesProvi public final static int VERSION_WITH_COPY_PATHS_ADDED = 2; public final static int VERSION_WITH_REPLACED_PATHS = 3; - public SvnCommittedChangesProvider(@NotNull Project project) { - myProject = project; - myVcs = SvnVcs.getInstance(myProject); + public SvnCommittedChangesProvider(@NotNull SvnVcs vcs) { + myVcs = vcs; myZipper = new SvnCommittedListsZipper(myVcs); - myConnection = myProject.getMessageBus().connect(); - - myConnection.subscribe(VcsConfigurationChangeListener.BRANCHES_CHANGED_RESPONSE, new VcsConfigurationChangeListener.DetailedNotification() { - public void execute(final Project project, final VirtualFile vcsRoot, final List cachedList) { - ApplicationManager.getApplication().invokeLater(new Runnable() { - public void run() { - if (project.isDisposed()) { - return; - } - for (CommittedChangeList committedChangeList : cachedList) { - if ((committedChangeList instanceof SvnChangeList) && - ((vcsRoot == null) || (vcsRoot.equals(((SvnChangeList)committedChangeList).getVcsRoot())))) { - ((SvnChangeList) committedChangeList).forceReloadCachedInfo(true); - } - } - } - }); - } - }); + myConnection = myVcs.getProject().getMessageBus().connect(); + myConnection.subscribe(VcsConfigurationChangeListener.BRANCHES_CHANGED_RESPONSE, + (project, vcsRoot, cachedList) -> getApplication().invokeLater(() -> { + cachedList.stream().filter(SvnChangeList.class::isInstance).map(SvnChangeList.class::cast) + .filter(list -> vcsRoot == null || vcsRoot.equals(list.getVcsRoot())) + .forEach(SvnChangeList::forceReloadCachedInfo); + }, project.getDisposed())); } @NotNull @@ -113,13 +99,13 @@ public class SvnCommittedChangesProvider implements CachingCommittedChangesProvi } @NotNull - public ChangesBrowserSettingsEditor createFilterUI(final boolean showDateFilter) { + public ChangesBrowserSettingsEditor createFilterUI(boolean showDateFilter) { return new SvnVersionFilterComponent(showDateFilter); } @Nullable public RepositoryLocation getLocationFor(@NotNull FilePath root) { - final String url = SvnUtil.getExactLocation(myVcs, root.getIOFile()); + String url = SvnUtil.getExactLocation(myVcs, root.getIOFile()); return url == null ? null : new SvnRepositoryLocation(url, root); } @@ -137,20 +123,20 @@ public class SvnCommittedChangesProvider implements CachingCommittedChangesProvi public void loadCommittedChanges(@NotNull ChangeBrowserSettings settings, @NotNull RepositoryLocation location, int maxCount, - @NotNull final AsynchConsumer consumer) throws VcsException { + @NotNull AsynchConsumer consumer) throws VcsException { try { - final SvnRepositoryLocation svnLocation = (SvnRepositoryLocation) location; - final String repositoryRoot = getRepositoryRoot(svnLocation); - final ChangeBrowserSettings.Filter filter = settings.createFilter(); - - getCommittedChangesImpl(settings, svnLocation, maxCount, new Consumer() { - public void consume(final LogEntry svnLogEntry) { - final SvnChangeList cl = new SvnChangeList(myVcs, svnLocation, svnLogEntry, repositoryRoot); - if (filter.accepts(cl)) { - consumer.consume(cl); - } + SvnRepositoryLocation svnLocation = (SvnRepositoryLocation)location; + String repositoryRoot = getRepositoryRoot(svnLocation); + ChangeBrowserSettings.Filter filter = settings.createFilter(); + Consumer resultConsumer = logEntry -> { + SvnChangeList list = new SvnChangeList(myVcs, svnLocation, logEntry, repositoryRoot); + if (filter.accepts(list)) { + consumer.consume(list); } - }, false, true); + }; + SvnTarget target = SvnTarget.fromURL(svnLocation.toSvnUrl(), createBeforeRevision(settings)); + + getCommittedChangesImpl(settings, target, maxCount, resultConsumer, false, true); } finally { consumer.finished(); @@ -161,15 +147,13 @@ public class SvnCommittedChangesProvider implements CachingCommittedChangesProvi public List getCommittedChanges(@NotNull ChangeBrowserSettings settings, @NotNull RepositoryLocation location, int maxCount) throws VcsException { - final SvnRepositoryLocation svnLocation = (SvnRepositoryLocation) location; - final ArrayList result = new ArrayList<>(); - final String repositoryRoot = getRepositoryRoot(svnLocation); + SvnRepositoryLocation svnLocation = (SvnRepositoryLocation)location; + List result = newArrayList(); + String repositoryRoot = getRepositoryRoot(svnLocation); + Consumer resultConsumer = logEntry -> result.add(new SvnChangeList(myVcs, svnLocation, logEntry, repositoryRoot)); + SvnTarget target = SvnTarget.fromURL(svnLocation.toSvnUrl(), createBeforeRevision(settings)); - getCommittedChangesImpl(settings, svnLocation, maxCount, new Consumer() { - public void consume(final LogEntry svnLogEntry) { - result.add(new SvnChangeList(myVcs, svnLocation, svnLogEntry, repositoryRoot)); - } - }, false, true); + getCommittedChangesImpl(settings, target, maxCount, resultConsumer, false, true); settings.filterChanges(result); return result; } @@ -177,31 +161,20 @@ public class SvnCommittedChangesProvider implements CachingCommittedChangesProvi public void getCommittedChangesWithMergedRevisons(@NotNull ChangeBrowserSettings settings, @NotNull RepositoryLocation location, int maxCount, - @NotNull final PairConsumer finalConsumer) + @NotNull PairConsumer finalConsumer) throws VcsException { - final SvnRepositoryLocation svnLocation = (SvnRepositoryLocation) location; - final String repositoryRoot = getRepositoryRoot(svnLocation); + SvnRepositoryLocation svnLocation = (SvnRepositoryLocation)location; + String repositoryRoot = getRepositoryRoot(svnLocation); + MergeSourceHierarchyBuilder builder = new MergeSourceHierarchyBuilder( + node -> finalConsumer.consume(new SvnChangeList(myVcs, svnLocation, node.getMe(), repositoryRoot), node)); + SvnMergeSourceTracker mergeSourceTracker = new SvnMergeSourceTracker(builder); - final MergeSourceHierarchyBuilder builder = new MergeSourceHierarchyBuilder(new Consumer() { - public void consume(LogHierarchyNode node) { - finalConsumer.consume(new SvnChangeList(myVcs, svnLocation, node.getMe(), repositoryRoot), node); + getCommittedChangesImpl(settings, SvnTarget.fromURL(svnLocation.toSvnUrl()), maxCount, logEntry -> { + try { + mergeSourceTracker.consume(logEntry); } - }); - final SvnMergeSourceTracker mergeSourceTracker = new SvnMergeSourceTracker(new ThrowableConsumer, SVNException>() { - public void consume(Pair svnLogEntryIntegerPair) throws SVNException { - builder.consume(svnLogEntryIntegerPair); - } - }); - - getCommittedChangesImpl(settings, SvnTarget.fromURL(svnLocation.toSvnUrl()), maxCount, new Consumer() { - public void consume(final LogEntry svnLogEntry) { - try { - mergeSourceTracker.consume(svnLogEntry); - } - catch (SVNException e) { - throw new RuntimeException(e); - // will not occur actually but anyway never eat them - } + catch (SVNException e) { + throw new RuntimeException(e); } }, true, false); @@ -222,17 +195,6 @@ public class SvnCommittedChangesProvider implements CachingCommittedChangesProvi return rootUrl.toDecodedString(); } - private void getCommittedChangesImpl(@NotNull ChangeBrowserSettings settings, - @NotNull SvnRepositoryLocation location, - int maxCount, - @NotNull Consumer resultConsumer, - boolean includeMergedRevisions, - boolean filterOutByDate) throws VcsException { - SvnTarget target = SvnTarget.fromURL(location.toSvnUrl(), createBeforeRevision(settings)); - - getCommittedChangesImpl(settings, target, maxCount, resultConsumer, includeMergedRevisions, filterOutByDate); - } - private void getCommittedChangesImpl(@NotNull ChangeBrowserSettings settings, @NotNull SvnTarget target, int maxCount, @@ -262,7 +224,7 @@ public class SvnCommittedChangesProvider implements CachingCommittedChangesProvi @NotNull private static SVNRevision createRevision(@Nullable Date date, @Nullable Long change, @NotNull SVNRevision defaultValue) { - final SVNRevision result; + SVNRevision result; if (date != null) { result = SVNRevision.create(date); @@ -278,22 +240,17 @@ public class SvnCommittedChangesProvider implements CachingCommittedChangesProvi } @NotNull - private LogEntryConsumer createLogHandler(@NotNull final Consumer resultConsumer, - final boolean filterOutByDate, - @Nullable final String author) { - return new LogEntryConsumer() { - @Override - public void consume(LogEntry logEntry) { - if (myProject.isDisposed()) throw new ProcessCanceledException(); + private LogEntryConsumer createLogHandler(@NotNull Consumer resultConsumer, boolean filterOutByDate, @Nullable String author) { + return logEntry -> { + if (myVcs.getProject().isDisposed()) throw new ProcessCanceledException(); - ProgressManager.progress2(SvnBundle.message("progress.text2.processing.revision", logEntry.getRevision())); - if (filterOutByDate && logEntry.getDate() == null) { - // do not add lists without info - this situation is possible for lists where there are paths that user has no rights to observe - return; - } - if (author == null || author.equalsIgnoreCase(logEntry.getAuthor())) { - resultConsumer.consume(logEntry); - } + ProgressManager.progress2(SvnBundle.message("progress.text2.processing.revision", logEntry.getRevision())); + if (filterOutByDate && logEntry.getDate() == null) { + // do not add lists without info - this situation is possible for lists where there are paths that user has no rights to observe + return; + } + if (author == null || author.equalsIgnoreCase(logEntry.getAuthor())) { + resultConsumer.consume(logEntry); } }; } @@ -305,7 +262,7 @@ public class SvnCommittedChangesProvider implements CachingCommittedChangesProvi @NotNull public ChangeListColumn[] getColumns() { - return new ChangeListColumn[] { + return new ChangeListColumn[]{ new ChangeListColumn.ChangeListNumberColumn(SvnBundle.message("revision.title")), ChangeListColumn.NAME, ChangeListColumn.DATE, ChangeListColumn.DESCRIPTION }; @@ -313,31 +270,29 @@ public class SvnCommittedChangesProvider implements CachingCommittedChangesProvi private void refreshMergeInfo(@NotNull RootsAndBranches action) { if (myMergeInfoUpdatesListener == null) { - myMergeInfoUpdatesListener = new MergeInfoUpdatesListener(myProject, myConnection); + myMergeInfoUpdatesListener = new MergeInfoUpdatesListener(myVcs.getProject(), myConnection); } myMergeInfoUpdatesListener.addPanel(action); } @NotNull public VcsCommittedViewAuxiliary createActions(@NotNull DecoratorManager manager, @Nullable RepositoryLocation location) { - final RootsAndBranches rootsAndBranches = new RootsAndBranches(myVcs, manager, location); + RootsAndBranches rootsAndBranches = new RootsAndBranches(myVcs, manager, location); refreshMergeInfo(rootsAndBranches); - final DefaultActionGroup popup = new DefaultActionGroup(myVcs.getDisplayName(), true); + DefaultActionGroup popup = new DefaultActionGroup(myVcs.getDisplayName(), true); popup.add(rootsAndBranches.getIntegrateAction()); popup.add(rootsAndBranches.getUndoIntegrateAction()); popup.add(new ConfigureBranchesAction()); - final ShowHideMergePanelAction action = new ShowHideMergePanelAction(manager, rootsAndBranches.getStrategy()); + ShowHideMergePanelAction action = new ShowHideMergePanelAction(manager, rootsAndBranches.getStrategy()); - return new VcsCommittedViewAuxiliary(Collections.singletonList(popup), new Runnable() { - public void run() { - if (myMergeInfoUpdatesListener != null) { - myMergeInfoUpdatesListener.removePanel(rootsAndBranches); - rootsAndBranches.dispose(); - } + return new VcsCommittedViewAuxiliary(singletonList(popup), () -> { + if (myMergeInfoUpdatesListener != null) { + myMergeInfoUpdatesListener.removePanel(rootsAndBranches); + rootsAndBranches.dispose(); } - }, Collections.singletonList(action)); + }, singletonList(action)); } public int getUnlimitedCountValue() { @@ -371,7 +326,7 @@ public class SvnCommittedChangesProvider implements CachingCommittedChangesProvi @NotNull public SvnChangeList readChangeList(@NotNull RepositoryLocation location, @NotNull DataInput stream) throws IOException { - final int version = getFormatVersion(); + int version = getFormatVersion(); return new SvnChangeList(myVcs, (SvnRepositoryLocation)location, stream, VERSION_WITH_COPY_PATHS_ADDED <= version, VERSION_WITH_REPLACED_PATHS <= version); } @@ -402,7 +357,7 @@ public class SvnCommittedChangesProvider implements CachingCommittedChangesProvi // TODO: could only be used with url targets - so we could not use "svn diff" here now for all cases (we could not use url with // TODO: concrete revision as there could be mixed revision working copy). - final Set result = ContainerUtil.newHashSet(); + Set result = newHashSet(); File rootFile = root.getIOFile(); myVcs.getFactory(rootFile).createStatusClient() @@ -415,7 +370,7 @@ public class SvnCommittedChangesProvider implements CachingCommittedChangesProvi isNotNone(status.getRemotePropertiesStatus()); if (file != null && changedOnServer) { - result.add(VcsUtil.getFilePath(file, file.isDirectory())); + result.add(VcsUtil.getFilePath(file)); } } }, null); @@ -435,8 +390,10 @@ public class SvnCommittedChangesProvider implements CachingCommittedChangesProvi return SvnBundle.message("changes.browser.revision.term"); } - public boolean isChangeLocallyAvailable(FilePath filePath, @Nullable VcsRevisionNumber localRevision, VcsRevisionNumber changeRevision, - final SvnChangeList changeList) { + public boolean isChangeLocallyAvailable(FilePath filePath, + @Nullable VcsRevisionNumber localRevision, + VcsRevisionNumber changeRevision, + SvnChangeList changeList) { return localRevision != null && localRevision.compareTo(changeRevision) >= 0; }