blob: 426efd11a50ae98071621df63a9dae489d869c0e [file] [log] [blame]
/*
* Copyright 2000-2009 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.
*/
package com.intellij.slicer;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.TextRange;
import com.intellij.usageView.UsageInfo;
import gnu.trove.THashMap;
import gnu.trove.TObjectHashingStrategy;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
/**
* @author cdr
*/ // rehash map on each PSI modification since SmartPsiPointer's hashCode() and equals() are changed
public class DuplicateMap {
private static final TObjectHashingStrategy<SliceUsage> USAGE_INFO_EQUALITY = new TObjectHashingStrategy<SliceUsage>() {
@Override
public int computeHashCode(SliceUsage object) {
UsageInfo info = object.getUsageInfo();
TextRange range = info.getRangeInElement();
return range == null ? 0 : range.hashCode();
}
@Override
public boolean equals(SliceUsage o1, SliceUsage o2) {
return o1.getUsageInfo().equals(o2.getUsageInfo());
}
};
private final Map<SliceUsage, SliceNode> myDuplicates = new THashMap<SliceUsage, SliceNode>(USAGE_INFO_EQUALITY);
public SliceNode putNodeCheckDupe(@NotNull final SliceNode node) {
return ApplicationManager.getApplication().runReadAction(new Computable<SliceNode>() {
@Override
public SliceNode compute() {
SliceUsage usage = node.getValue();
SliceNode eq = myDuplicates.get(usage);
if (eq == null) {
myDuplicates.put(usage, node);
}
return eq;
}
});
}
public void clear() {
myDuplicates.clear();
}
}