From ec0da9f9b7a025277cd9ccacb6e49a7fe87b5bd2 Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Mon, 13 Nov 2017 12:05:49 +0300 Subject: [PATCH] inspection view: internalize problem levels for ref and problem nodes --- .../codeInspection/ui/InspectionTreeNode.java | 28 ++++++++++++-- .../codeInspection/ui/LevelAndCount.java | 37 +++++++++++-------- .../ui/SuppressableInspectionTreeNode.java | 5 +++ 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionTreeNode.java b/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionTreeNode.java index 251fda339454..ba184a679275 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionTreeNode.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionTreeNode.java @@ -7,7 +7,9 @@ import com.intellij.codeInspection.reference.RefEntity; import com.intellij.lang.annotation.HighlightSeverity; import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.util.AtomicClearableLazyValue; +import com.intellij.util.containers.Interner; import com.intellij.util.ui.tree.TreeUtil; +import gnu.trove.TObjectHashingStrategy; import gnu.trove.TObjectIntHashMap; import gnu.trove.TObjectIntProcedure; import org.jetbrains.annotations.NotNull; @@ -25,6 +27,18 @@ import java.util.Enumeration; * @author max */ public abstract class InspectionTreeNode extends DefaultMutableTreeNode { + private static final Interner LEVEL_AND_COUNT_INTERNER = new Interner<>(new TObjectHashingStrategy() { + @Override + public int computeHashCode(LevelAndCount[] object) { + return Arrays.hashCode(object); + } + + @Override + public boolean equals(LevelAndCount[] o1, LevelAndCount[] o2) { + return Arrays.equals(o1, o2); + } + }); + protected final AtomicClearableLazyValue myProblemLevels = new AtomicClearableLazyValue() { @NotNull @Override @@ -40,15 +54,21 @@ public abstract class InspectionTreeNode extends DefaultMutableTreeNode { return true; } }); - Arrays.sort(arr, Comparator.comparing(levelAndCount -> levelAndCount.getLevel().getSeverity()).reversed()); - return arr; + Arrays.sort(arr, Comparator.comparing(levelAndCount -> levelAndCount.getLevel().getSeverity()) + .reversed()); + return doesNeedInternProblemLevels() ? LEVEL_AND_COUNT_INTERNER.intern(arr) : arr; } }; protected volatile InspectionTreeUpdater myUpdater; - protected InspectionTreeNode (Object userObject) { + + protected InspectionTreeNode(Object userObject) { super(userObject); } + protected boolean doesNeedInternProblemLevels() { + return false; + } + @Nullable public Icon getIcon(boolean expanded) { return null; @@ -145,7 +165,7 @@ public abstract class InspectionTreeNode extends DefaultMutableTreeNode { } } int index = TreeUtil.indexedBinarySearch(this, child, InspectionResultsViewComparator.getInstance()); - if (!allowDuplication && index >= 0){ + if (!allowDuplication && index >= 0) { return (InspectionTreeNode)getChildAt(index); } insert(child, Math.abs(index + 1)); diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ui/LevelAndCount.java b/platform/lang-impl/src/com/intellij/codeInspection/ui/LevelAndCount.java index 0962bcbeca75..527f64734531 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ui/LevelAndCount.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ui/LevelAndCount.java @@ -1,24 +1,11 @@ -/* - * Copyright 2000-2017 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInspection.ui; import com.intellij.codeHighlighting.HighlightDisplayLevel; import org.jetbrains.annotations.NotNull; class LevelAndCount { + @NotNull private final HighlightDisplayLevel myLevel; private final int myCount; @@ -32,6 +19,26 @@ class LevelAndCount { return myLevel; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + LevelAndCount count = (LevelAndCount)o; + + if (myCount != count.myCount) return false; + if (myLevel != null ? !myLevel.equals(count.myLevel) : count.myLevel != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = myLevel != null ? myLevel.hashCode() : 0; + result = 31 * result + myCount; + return result; + } + public int getCount() { return myCount; } diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ui/SuppressableInspectionTreeNode.java b/platform/lang-impl/src/com/intellij/codeInspection/ui/SuppressableInspectionTreeNode.java index 5b536345d1b6..0e5cf32a6523 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ui/SuppressableInspectionTreeNode.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ui/SuppressableInspectionTreeNode.java @@ -34,6 +34,11 @@ public abstract class SuppressableInspectionTreeNode extends InspectionTreeNode myPresentation = presentation; } + @Override + protected boolean doesNeedInternProblemLevels() { + return true; + } + @NotNull public InspectionToolPresentation getPresentation() { return myPresentation;