cleanup: do not create an empty array every time

GitOrigin-RevId: 742d3736adf5a84d190f38439f8462ee7fe229c5
This commit is contained in:
Sergey Malenkov
2019-07-02 06:52:16 +03:00
committed by intellij-monorepo-bot
parent 6a7cc72eab
commit c99ab77e14
17 changed files with 47 additions and 36 deletions
@@ -43,6 +43,8 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static com.intellij.ui.tree.TreePathUtil.toTreePathArray;
public abstract class BaseLibrariesConfigurable extends BaseStructureConfigurable {
@NotNull
protected final String myLevel;
@@ -248,7 +250,7 @@ public abstract class BaseLibrariesConfigurable extends BaseStructureConfigurabl
}
}
myContext.getDaemonAnalyzer().removeElements(libraries);
removePaths(pathsToRemove.toArray(new TreePath[0]));
removePaths(toTreePathArray(pathsToRemove));
}
@Override
@@ -56,6 +56,7 @@ import java.util.*;
import java.util.stream.Stream;
import static com.intellij.codeInspection.CommonProblemDescriptor.DESCRIPTOR_COMPARATOR;
import static com.intellij.ui.tree.TreePathUtil.toTreePathArray;
public class InspectionTree extends Tree {
private static final Logger LOG = Logger.getInstance(InspectionTree.class);
@@ -456,7 +457,7 @@ public class InspectionTree extends Tree {
for (InspectionTreeNode parent : parents) {
parent.dropProblemCountCaches();
}
TreeUtil.selectPath(this, TreeUtil.findCommonPath(pathsToSelect.toArray(new TreePath[0])));
TreeUtil.selectPath(this, TreeUtil.findCommonPath(toTreePathArray(pathsToSelect)));
revalidate();
repaint();
@@ -181,7 +181,7 @@ public abstract class AbstractProjectViewPSIPane extends AbstractProjectViewPane
TreeBuilderUtil.storePaths(builder, (DefaultMutableTreeNode)myTree.getModel().getRoot(), pathsToExpand, selectionPaths, true);
afterUpdate = () -> {
if (myTree != null && !builder.isDisposed()) {
myTree.setSelectionPaths(new TreePath[0]);
myTree.clearSelection();
TreeBuilderUtil.restorePaths(builder, pathsToExpand, selectionPaths, true);
}
cb.setDone();
@@ -91,6 +91,8 @@ import java.awt.*;
import java.util.List;
import java.util.*;
import static com.intellij.ui.tree.TreePathUtil.toTreePathArray;
@State(name = "ProjectView", storages = {
@Storage(StoragePathMacros.PRODUCT_WORKSPACE_FILE),
@Storage(value = StoragePathMacros.WORKSPACE_FILE, deprecated = true)
@@ -1896,7 +1898,7 @@ public class ProjectViewImpl extends ProjectView implements PersistentStateCompo
}
}
if (!paths.isEmpty()) {
tree.setSelectionPaths(paths.toArray(new TreePath[0]));
tree.setSelectionPaths(toTreePathArray(paths));
}
}
else {
@@ -22,7 +22,6 @@ import com.intellij.ui.treeStructure.Tree;
import com.intellij.util.PlatformIcons;
import com.intellij.util.containers.FactoryMap;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;
import com.intellij.util.ui.tree.TreeUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -38,6 +37,7 @@ import java.util.List;
import java.util.*;
import static com.intellij.openapi.keymap.KeymapUtil.getActiveKeymapShortcuts;
import static com.intellij.ui.tree.TreePathUtil.toTreePathArray;
public class MemberChooser<T extends ClassMember> extends DialogWrapper implements TypeSafeDataProvider {
private static final Logger LOG = Logger.getInstance("#com.intellij.ide.util.MemberChooser");
@@ -242,7 +242,7 @@ public class MemberChooser<T extends ClassMember> extends DialogWrapper implemen
selectionPaths.add(new TreePath(((DefaultMutableTreeNode)treeNode).getPath()));
}
}
final TreePath[] paths = selectionPaths.toArray(new TreePath[0]);
final TreePath[] paths = toTreePathArray(selectionPaths);
myTree.setSelectionPaths(paths);
if (paths.length > 0) {
@@ -633,7 +633,7 @@ public class MemberChooser<T extends ClassMember> extends DialogWrapper implemen
}
if (!toSelect.isEmpty()) {
myTree.setSelectionPaths(toSelect.toArray(new TreePath[0]));
myTree.setSelectionPaths(toTreePathArray(toSelect));
}
ElementNode leadNode = pair.first;
@@ -78,7 +78,7 @@ public class TreeBuilderUtil {
for (Object path : pathsToExpand) {
tree.expandPath((TreePath)path);
}
tree.addSelectionPaths(selectionPaths.toArray(new TreePath[0]));
tree.addSelectionPaths(selectionPaths.toArray(TreeUtil.EMPTY_TREE_PATH));
}
else{
for (Object element : pathsToExpand) {
@@ -280,7 +280,7 @@ public class TreeState implements JDOMExternalizable {
ContainerUtil.addIfNotNull(selection, treePath);
}
if (selection.isEmpty()) return;
tree.setSelectionPaths(selection.toArray(new TreePath[0]));
tree.setSelectionPaths(selection.toArray(TreeUtil.EMPTY_TREE_PATH));
if (myScrollToSelection) {
TreeUtil.showRowCentered(tree, tree.getRowForPath(selection.get(0)), true, true);
}
@@ -6,6 +6,7 @@ import com.intellij.ui.TableUtil;
import com.intellij.ui.scale.JBUIScale;
import com.intellij.ui.table.JBTable;
import com.intellij.util.ui.accessibility.ScreenReader;
import com.intellij.util.ui.tree.TreeUtil;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
@@ -312,7 +313,7 @@ public class TreeTable extends JBTable {
}
}
if (!selectionPaths.isEmpty()) {
addSelectionPaths(selectionPaths.toArray(new TreePath[0]));
addSelectionPaths(selectionPaths.toArray(TreeUtil.EMPTY_TREE_PATH));
}
}
}
@@ -52,6 +52,7 @@ import static com.intellij.util.ReflectionUtil.getDeclaredMethod;
import static java.util.stream.Collectors.toList;
public final class TreeUtil {
public static final TreePath[] EMPTY_TREE_PATH = new TreePath[0];
private static final Logger LOG = Logger.getInstance("#com.intellij.util.ui.tree.TreeUtil");
private static final String TREE_UTIL_SCROLL_TIME_STAMP = "TreeUtil.scrollTimeStamp";
private static final JBIterable<Integer> NUMBERS = JBIterable.generate(0, i -> i + 1);
@@ -358,20 +359,20 @@ public final class TreeUtil {
for (final TreePath path : paths) {
if (!result.contains(path)) result.add(path);
}
return result.toArray(new TreePath[0]);
return result.toArray(EMPTY_TREE_PATH);
}
@NotNull
public static TreePath[] selectMaximals(@Nullable final TreePath[] paths) {
if (paths == null) return new TreePath[0];
if (paths == null) return EMPTY_TREE_PATH;
final TreePath[] noDuplicates = removeDuplicates(paths);
final ArrayList<TreePath> result = new ArrayList<>();
for (final TreePath path : noDuplicates) {
final ArrayList<TreePath> otherPaths = new ArrayList<>(Arrays.asList(noDuplicates));
otherPaths.remove(path);
if (!isDescendants(path, otherPaths.toArray(new TreePath[0]))) result.add(path);
if (!isDescendants(path, otherPaths.toArray(EMPTY_TREE_PATH))) result.add(path);
}
return result.toArray(new TreePath[0]);
return result.toArray(EMPTY_TREE_PATH);
}
public static void sort(@NotNull final DefaultTreeModel model, @Nullable Comparator comparator) {
@@ -421,7 +422,7 @@ public final class TreeUtil {
public static void selectPaths(@NotNull JTree tree, @NotNull Collection<? extends TreePath> paths) {
if (paths.isEmpty()) return;
selectPaths(tree, paths.toArray(new TreePath[0]));
selectPaths(tree, paths.toArray(EMPTY_TREE_PATH));
}
public static void selectPaths(@NotNull JTree tree, @NotNull TreePath... paths) {
@@ -1173,7 +1174,7 @@ public final class TreeUtil {
if (toRetain == null) return;
TreePath[] selection = tree.getSelectionModel().getSelectionPaths();
selection = selection == null ? new TreePath[0] : selection;
selection = selection == null ? EMPTY_TREE_PATH : selection;
for (TreePath each : selection) {
if (toRetain.equals(each)) continue;
tree.getSelectionModel().removeSelectionPath(each);
@@ -1486,7 +1487,7 @@ public final class TreeUtil {
private static void internalSelectPaths(@NotNull JTree tree, @NotNull List<? extends TreePath> paths) {
assert EventQueue.isDispatchThread();
if (paths.isEmpty()) return;
tree.setSelectionPaths(paths.toArray(new TreePath[0]));
tree.setSelectionPaths(paths.toArray(EMPTY_TREE_PATH));
for (TreePath path : paths) {
if (scrollToVisible(tree, path, true)) {
break;
@@ -977,11 +977,7 @@ public class SettingsTreeView extends JComponent implements Accessible, Disposab
}
private void restoreExpandedState(List<Object> toRestore) {
TreePath[] selected = myTree.getSelectionPaths();
if (selected == null) {
selected = new TreePath[0];
}
List<TreePath> selected = TreeUtil.collectSelectedPaths(myTree);
List<TreePath> toCollapse = new ArrayList<>();
for (int eachRow = 0; eachRow < myTree.getRowCount(); eachRow++) {
@@ -14,6 +14,8 @@ import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.util.*;
import static com.intellij.ui.tree.TreePathUtil.toTreePathArray;
public abstract class TreeExpansionMonitor<T> {
public static TreeExpansionMonitor<DefaultMutableTreeNode> install(final JTree tree) {
@@ -75,7 +77,7 @@ public abstract class TreeExpansionMonitor<T> {
if (myFrozen) return;
TreePath path = event.getPath();
if (path != null) {
TreePath[] allPaths = myExpandedPaths.toArray(new TreePath[0]);
TreePath[] allPaths = toTreePathArray(myExpandedPaths);
for (TreePath treePath : allPaths) {
if (treePath.equals(path) || path.isDescendant(treePath)) {
myExpandedPaths.remove(treePath);
@@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import static com.intellij.ui.tree.TreePathUtil.toTreePathArray;
import static javax.swing.tree.TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION;
public class TreeSpeedSearch extends SpeedSearchBase<JTree> {
@@ -88,10 +89,9 @@ public class TreeSpeedSearch extends SpeedSearchBase<JTree> {
}
paths = JBIterable.of(arr);
}
List<TreePath> result = paths
return paths
.filter(o -> !(o.getLastPathComponent() instanceof LoadingNode))
.toList();
return result.toArray(new TreePath[0]);
.toArray(TreeUtil.EMPTY_TREE_PATH);
}
@Override
@@ -157,7 +157,7 @@ public class TreeSpeedSearch extends SpeedSearchBase<JTree> {
TreePath currentElement = (TreePath)mySearch.findElement(query);
TreePath anchor = ObjectUtils.chooseNotNull(currentElement, filtered.get(0));
sm.setSelectionPaths(filtered.toArray(new TreePath[0]));
sm.setSelectionPaths(toTreePathArray(filtered));
myTree.setAnchorSelectionPath(anchor);
}
}
@@ -9,10 +9,13 @@ import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Stream;
import static com.intellij.util.ui.tree.TreeUtil.EMPTY_TREE_PATH;
public class TreePathUtil {
/**
* @param parent the parent path or {@code null} to indicate the root
@@ -189,6 +192,10 @@ public class TreePathUtil {
return object == null ? null : converter.apply(object);
}
public static TreePath[] toTreePathArray(@NotNull Collection<TreePath> collection) {
return collection.isEmpty() ? EMPTY_TREE_PATH : collection.toArray(EMPTY_TREE_PATH);
}
public static TreeNode toTreeNode(TreePath path) {
Object component = path == null ? null : path.getLastPathComponent();
return component instanceof TreeNode ? (TreeNode)component : null;
@@ -28,6 +28,7 @@ import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.function.Predicate;
import static com.intellij.ui.tree.TreePathUtil.toTreePathArray;
import static javax.swing.tree.TreeSelectionModel.SINGLE_TREE_SELECTION;
/**
@@ -123,7 +124,7 @@ public class TreeSmartSelectProvider implements SmartSelectProvider<JTree> {
return true; // visit all descendants
});
if (list.isEmpty()) return false; // selection is not changed
consumer.accept(list.toArray(new TreePath[0]));
consumer.accept(toTreePathArray(list));
return true;
}
}
@@ -54,6 +54,7 @@ import java.util.*;
import static com.intellij.openapi.vcs.changes.ui.ChangesGroupingSupport.DIRECTORY_GROUPING;
import static com.intellij.openapi.vcs.changes.ui.ChangesGroupingSupport.MODULE_GROUPING;
import static com.intellij.ui.tree.TreePathUtil.toTreePathArray;
import static com.intellij.util.ObjectUtils.notNull;
import static com.intellij.util.containers.ContainerUtil.ar;
import static com.intellij.util.containers.ContainerUtil.set;
@@ -734,7 +735,7 @@ public abstract class ChangesTree extends Tree implements DataProvider {
}
return true;
});
setSelectionPaths(treeSelection.toArray(new TreePath[0]));
setSelectionPaths(toTreePathArray(treeSelection));
if (treeSelection.size() == 1) scrollPathToVisible(treeSelection.get(0));
}
@@ -102,7 +102,6 @@ class XDebuggerTreeSpeedSearch extends TreeSpeedSearch {
.expand(n -> myComponent.isExpanded(n) || n.getPathCount() - initialLevel < SEARCH_DEPTH)
.traverse()
.filter(o -> !(o.getLastPathComponent() instanceof LoadingNode))
.toList()
.toArray(new TreePath[0]);
.toArray(TreeUtil.EMPTY_TREE_PATH);
}
}
@@ -42,11 +42,12 @@ import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import static com.intellij.util.ui.tree.TreeUtil.collectSelectedPaths;
/**
* @author Vitaliy.Bibaev
*/
public class CollectionTree extends XDebuggerTree implements TraceContainer {
private static final TreePath[] EMPTY_PATHS = new TreePath[0];
private static final Map<Integer, Color> COLORS_CACHE = new HashMap<>();
private static final Object NULL_MARKER = ObjectUtils.sentinel("CollectionTree.NULL_MARKER");
@@ -117,11 +118,8 @@ public class CollectionTree extends XDebuggerTree implements TraceContainer {
if (myIgnoreInternalSelectionEvents) {
return;
}
final TreePath[] selectedPaths = getSelectionPaths();
final TreePath[] paths = selectedPaths == null ? EMPTY_PATHS : selectedPaths;
final List<TraceElement> selectedItems =
Arrays.stream(paths)
collectSelectedPaths(this).stream()
.map(this::getTopPath)
.map(myPath2Value::get)
.filter(Objects::nonNull)