From f3f591bcabe8ef3636e2ca268d74e6e2dc00a56a Mon Sep 17 00:00:00 2001 From: Nadya Zabrodina Date: Mon, 28 Nov 2016 13:59:25 +0300 Subject: [PATCH] [git]: cleanUp - to stream API --- .../git4idea/ui/branch/GitBranchPopup.java | 39 ++++++++++++------- .../ui/branch/GitBranchPopupActions.java | 26 ++++++------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopup.java b/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopup.java index ef81babc5a51..e2a5bf07fbb1 100644 --- a/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopup.java +++ b/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopup.java @@ -33,6 +33,9 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.util.List; +import java.util.Objects; + +import static java.util.stream.Collectors.toList; /** * The popup which allows to quickly switch and control Git branches. @@ -106,17 +109,25 @@ class GitBranchPopup extends DvcsBranchPopup { popupGroup.addAll(createRepositoriesActions()); popupGroup.addSeparator("Common Local Branches"); - for (String branch : myMultiRootBranchConfig.getLocalBranchNames()) { - List repositories = filterRepositoriesNotOnThisBranch(branch, allRepositories); - if (!repositories.isEmpty()) { - popupGroup.add(new GitBranchPopupActions.LocalBranchActions(myProject, repositories, branch, myCurrentRepository)); - } - } + List localBranchActions = + myMultiRootBranchConfig.getLocalBranchNames().stream().map(l -> createLocalBranchActions(allRepositories, l)).filter(Objects::nonNull) + .collect(toList()); + popupGroup.addAll(localBranchActions); popupGroup.addSeparator("Common Remote Branches"); - for (String branch : ((GitMultiRootBranchConfig)myMultiRootBranchConfig).getRemoteBranches()) { - popupGroup.add(new GitBranchPopupActions.RemoteBranchActions(myProject, allRepositories, branch, myCurrentRepository)); - } + List remoteBranchActions = + ((GitMultiRootBranchConfig)myMultiRootBranchConfig).getRemoteBranches().stream() + .map(r -> new GitBranchPopupActions.RemoteBranchActions(myProject, allRepositories, r, myCurrentRepository)).collect(toList()); + popupGroup.addAll(remoteBranchActions); + } + + @Nullable + private GitBranchPopupActions.LocalBranchActions createLocalBranchActions(@NotNull List allRepositories, + @NotNull String branch) { + List repositories = filterRepositoriesNotOnThisBranch(branch, allRepositories); + return repositories.isEmpty() + ? null + : new GitBranchPopupActions.LocalBranchActions(myProject, repositories, branch, myCurrentRepository); } @NotNull @@ -124,11 +135,11 @@ class GitBranchPopup extends DvcsBranchPopup { protected DefaultActionGroup createRepositoriesActions() { DefaultActionGroup popupGroup = new DefaultActionGroup(null, false); popupGroup.addSeparator("Repositories"); - for (GitRepository repository : DvcsUtil.sortRepositories(myRepositoryManager.getRepositories())) { - popupGroup.add(new RootAction<>(repository, highlightCurrentRepo() ? myCurrentRepository : null, - new GitBranchPopupActions(repository.getProject(), repository).createActions(), - GitBranchUtil.getDisplayableBranchText(repository))); - } + List> rootActions = DvcsUtil.sortRepositories(myRepositoryManager.getRepositories()).stream() + .map(repo -> new RootAction<>(repo, highlightCurrentRepo() ? myCurrentRepository : null, + new GitBranchPopupActions(repo.getProject(), repo).createActions(), + GitBranchUtil.getDisplayableBranchText(repo))).collect(toList()); + popupGroup.addAll(rootActions); return popupGroup; } diff --git a/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopupActions.java b/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopupActions.java index 661a0efdb7dd..5e2a7909969f 100644 --- a/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopupActions.java +++ b/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopupActions.java @@ -27,7 +27,6 @@ import com.intellij.openapi.project.DumbAwareAction; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Messages; import com.intellij.util.containers.ContainerUtil; -import git4idea.GitBranch; import git4idea.branch.GitBranchUtil; import git4idea.branch.GitBrancher; import git4idea.repo.GitRepository; @@ -35,9 +34,9 @@ import git4idea.validators.GitNewBranchNameValidator; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; import static git4idea.GitStatisticsCollectorKt.reportUsage; @@ -65,22 +64,19 @@ class GitBranchPopupActions { if (toInsert != null) { popupGroup.addAll(toInsert); } + popupGroup.addSeparator("Local Branches" + repoInfo); - List localBranches = new ArrayList<>(myRepository.getBranches().getLocalBranches()); - Collections.sort(localBranches); - for (GitBranch localBranch : localBranches) { - if (!localBranch.equals(myRepository.getCurrentBranch())) { // don't show current branch in the list - popupGroup.add(new LocalBranchActions(myProject, repositoryList, localBranch.getName(), myRepository)); - } - } + List localBranchActions = + myRepository.getBranches().getLocalBranches().stream().sorted().filter(l -> !l.equals(myRepository.getCurrentBranch())) + .map(l -> new LocalBranchActions(myProject, repositoryList, l.getName(), myRepository)).collect( + Collectors.toList()); + popupGroup.addAll(localBranchActions); popupGroup.addSeparator("Remote Branches" + repoInfo); - List remoteBranches = new ArrayList<>(myRepository.getBranches().getRemoteBranches()); - Collections.sort(remoteBranches); - for (GitBranch remoteBranch : remoteBranches) { - popupGroup.add(new RemoteBranchActions(myProject, repositoryList, remoteBranch.getName(), myRepository)); - } - + List remoteBranchActions = myRepository.getBranches().getRemoteBranches().stream().sorted() + .map(r -> new RemoteBranchActions(myProject, repositoryList, r.getName(), myRepository)) + .collect(Collectors.toList()); + popupGroup.addAll(remoteBranchActions); return popupGroup; }