Using Stream API in several places

This commit is contained in:
Tagir Valeev
2016-09-06 10:33:23 +07:00
parent ae57ed8e7e
commit 32a39c64ee
13 changed files with 45 additions and 93 deletions
@@ -254,13 +254,7 @@ public abstract class DebuggerUtilsEx extends DebuggerUtils {
}
public static int getEnabledNumber(ClassFilter[] classFilters) {
int res = 0;
for (ClassFilter filter : classFilters) {
if (filter.isEnabled()) {
res++;
}
}
return res;
return (int)Arrays.stream(classFilters).filter(ClassFilter::isEnabled).count();
}
public static ClassFilter[] readFilters(List<Element> children) throws InvalidDataException {
@@ -204,13 +204,8 @@ public class UnusedDeclarationPresentation extends DefaultInspectionToolPresenta
if (showFixes) {
final TreePath[] paths = tree.getSelectionPaths();
if (paths != null) {
int count = 0;
for (TreePath path : paths) {
final Object component = path.getLastPathComponent();
if (component instanceof ProblemDescriptionNode) {
count++;
}
}
long count = Arrays.stream(paths).map(TreePath::getLastPathComponent)
.filter(component -> component instanceof ProblemDescriptionNode).count();
if (count > 0) {
final QuickFixAction[] fixes = super.getQuickFixes(refElements, tree);
if (fixes != null) {
@@ -813,11 +813,7 @@ public class ShowUsagesAction extends AnAction implements PopupAction {
}
private static int filtered(@NotNull List<Usage> usages, @NotNull UsageViewImpl usageView) {
int count=0;
for (Usage usage : usages) {
if (!usageView.isVisible(usage)) count++;
}
return count;
return (int)usages.stream().filter(usage -> !usageView.isVisible(usage)).count();
}
private static int getUsageOffset(@NotNull Usage usage) {
@@ -32,6 +32,7 @@ import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public abstract class AbstractPushDownDialog<MemberInfo extends MemberInfoBase<Member>,
Member extends PsiElement,
@@ -60,13 +61,9 @@ public abstract class AbstractPushDownDialog<MemberInfo extends MemberInfoBase<M
}
public ArrayList<MemberInfo> getSelectedMemberInfos() {
ArrayList<MemberInfo> list = new ArrayList<>(myMemberInfos.size());
for (MemberInfo info : myMemberInfos) {
if (info.isChecked() && myMemberInfoModel.isMemberEnabled(info)) {
list.add(info);
}
}
return list;
return myMemberInfos.stream()
.filter(info -> info.isChecked() && myMemberInfoModel.isMemberEnabled(info))
.collect(Collectors.toCollection(ArrayList::new));
}
protected String getDimensionServiceKey() {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import com.intellij.usages.UsageView;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.util.Arrays;
public class UsageViewManagerImpl extends UsageViewManager {
private final Key<Boolean> REUSABLE_CONTENT_KEY = Key.create("UsageTreeManager.REUSABLE_CONTENT_KEY");
@@ -99,14 +100,8 @@ public class UsageViewManagerImpl extends UsageViewManager {
private int getContentCount(boolean reusable) {
Key<Boolean> contentKey = reusable ? REUSABLE_CONTENT_KEY : NOT_REUSABLE_CONTENT_KEY;
int count = 0;
Content[] contents = myFindContentManager.getContents();
for (Content content : contents) {
if (content.getUserData(contentKey) != null) {
count++;
}
}
return count;
return (int)Arrays.stream(contents).filter(content -> content.getUserData(contentKey) != null).count();
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -209,10 +209,7 @@ public class SingleRowLayout extends TabLayout {
}
protected void updateMoreIconVisibility(SingleRowPassInfo data) {
int counter = 0;
for (TabInfo tabInfo : data.myVisibleInfos) {
if (isTabHidden(tabInfo)) counter++;
}
int counter = (int)data.myVisibleInfos.stream().filter(this::isTabHidden).count();
myMoreIcon.updateCounter(counter);
}
@@ -31,7 +31,6 @@ import com.intellij.openapi.roots.ContentIterator;
import com.intellij.openapi.roots.ModuleFileIndex;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.vfs.ReadonlyStatusHandler;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
@@ -243,12 +242,8 @@ public abstract class AbstractFileProcessor {
}
private void handleFiles(final List<PsiFile> files) {
final List<VirtualFile> vFiles = new ArrayList<>();
for (PsiFile psiFile : files) {
vFiles.add(psiFile.getVirtualFile());
}
if (!ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(VfsUtilCore.toVirtualFileArray(vFiles))
.hasReadonlyFiles()) {
final VirtualFile[] vFiles = files.stream().map(PsiFile::getVirtualFile).toArray(VirtualFile[]::new);
if (!ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(vFiles).hasReadonlyFiles()) {
if (!files.isEmpty()) {
final Runnable[] resultRunnable = new Runnable[1];
execute(() -> resultRunnable[0] = prepareFiles(files), () -> {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@ import org.jetbrains.idea.devkit.DevKitBundle;
import org.jetbrains.idea.devkit.module.PluginModuleType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class PrepareAllToDeployAction extends PrepareToDeployAction {
@@ -51,14 +52,11 @@ public class PrepareAllToDeployAction extends PrepareToDeployAction {
}
public void update(AnActionEvent e) {
int moduleCount = 0;
long moduleCount = 0;
final Project project = e.getData(CommonDataKeys.PROJECT);
if (project != null) {
for (Module aModule : (ModuleManager.getInstance(project).getModules())) {
if (PluginModuleType.isOfType(aModule)) {
moduleCount++;
}
}
moduleCount = Arrays.stream(ModuleManager.getInstance(project).getModules()).filter(PluginModuleType::isOfType)
.limit(2).count();
}
boolean enabled = false;
if (moduleCount > 1) {
@@ -36,10 +36,11 @@ import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Dmitry Krasilschikov
@@ -76,12 +77,9 @@ public class AbstractFolderNode extends AbstractMvcPsiNodeDescriptor {
return Collections.emptyList();
}
final List<AbstractTreeNode> children = new ArrayList<>();
// scan folder's children
for (PsiDirectory subDir : directory.getSubdirectories()) {
children.add(createFolderNode(subDir));
}
final List<AbstractTreeNode> children = Arrays.stream(directory.getSubdirectories())
.map(this::createFolderNode).collect(Collectors.toList());
for (PsiFile file : directory.getFiles()) {
processNotDirectoryFile(children, file);
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@ import org.jetbrains.idea.svn.actions.SvnMergeProvider;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
public abstract class AbstractSvnUpdateIntegrateEnvironment implements UpdateEnvironment {
protected final SvnVcs myVcs;
@@ -133,13 +134,8 @@ public abstract class AbstractSvnUpdateIntegrateEnvironment implements UpdateEnv
// update switched/ignored status of directories
private void dirtyRoots() {
final Collection<VirtualFile> vfColl = new ArrayList<>(myContentRoots.length);
for (FilePath contentRoot: myContentRoots) {
final VirtualFile vf = contentRoot.getVirtualFile();
if (vf != null) {
vfColl.add(vf);
}
}
final Collection<VirtualFile> vfColl = Arrays.stream(myContentRoots).map(FilePath::getVirtualFile)
.filter(Objects::nonNull).collect(Collectors.toList());
myDirtyScopeManager.filesDirty(vfColl, null);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,7 +25,9 @@ import com.intellij.util.xml.DomUtil;
import javax.swing.*;
import java.lang.reflect.Type;
import java.util.*;
import java.util.Comparator;
import java.util.Map;
import java.util.Optional;
abstract public class AbstractDomElementNode extends SimpleNode {
@@ -69,17 +71,9 @@ abstract public class AbstractDomElementNode extends SimpleNode {
final Class aClass = ReflectionUtil.getRawType(type);
List<Class> allParents = new ArrayList<>();
for (Map.Entry<Class, Boolean> entry : hiders.entrySet()) {
if (entry.getKey().isAssignableFrom(aClass)) {
allParents.add(entry.getKey());
}
}
if (allParents.size() == 0) return false;
Collections.sort(allParents, INHERITORS_COMPARATOR);
return hiders.get(allParents.get(0)).booleanValue();
Optional<Class> parent = hiders.keySet().stream()
.filter(klass -> klass.isAssignableFrom(aClass)).min(INHERITORS_COMPARATOR);
return parent.map(hiders::get).orElse(false);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -136,12 +136,9 @@ public class ConfigFilesTreeBuilder {
return fileType.getName() + " context files" ;
}
private boolean hasNonEmptyGroups(MultiMap<FileType, PsiFile> filesByType) {
byte nonEmptyGroups = 0;
for (Map.Entry<FileType, Collection<PsiFile>> entry : filesByType.entrySet()) {
Collection<PsiFile> files = entry.getValue();
if (files != null && files.size() > 0) nonEmptyGroups++;
}
private static boolean hasNonEmptyGroups(MultiMap<FileType, PsiFile> filesByType) {
long nonEmptyGroups = filesByType.entrySet().stream().map(Map.Entry::getValue)
.filter(files -> files != null && !files.isEmpty()).limit(2).count();
return nonEmptyGroups > 1;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@ import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class XmlTagValueImpl implements XmlTagValue{
@@ -56,11 +57,10 @@ public class XmlTagValueImpl implements XmlTagValue{
public XmlText[] getTextElements() {
XmlText[] textElements = myTextElements;
if(textElements != null) return textElements;
final List<XmlText> textElementsList = new ArrayList<>();
for (final XmlTagChild element : myElements) {
if (element instanceof XmlText) textElementsList.add((XmlText)element);
}
return myTextElements = textElementsList.isEmpty() ? XmlText.EMPTY_ARRAY : ContainerUtil.toArray(textElementsList, new XmlText[textElementsList.size()]);
myTextElements = Arrays.stream(myElements)
.filter(element -> element instanceof XmlText)
.map(element -> (XmlText)element).toArray(XmlText[]::new);
return myTextElements;
}
@Override