Lighter 'CachedValue' for storing class's method hierarchies.

This commit is contained in:
Maxim Shafirov
2010-11-12 17:43:14 +03:00
parent a5f127a083
commit 9fa2f25972
2 changed files with 60 additions and 28 deletions
@@ -15,14 +15,13 @@
*/
package com.intellij.psi.impl;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.UserDataHolderEx;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.HierarchicalMethodSignatureImpl;
import com.intellij.psi.search.searches.DeepestSuperMethodsSearch;
import com.intellij.psi.search.searches.SuperMethodsSearch;
import com.intellij.psi.util.*;
import com.intellij.util.NotNullFunction;
import com.intellij.util.SmartList;
import gnu.trove.THashMap;
import gnu.trove.THashSet;
@@ -33,7 +32,13 @@ import org.jetbrains.annotations.Nullable;
import java.util.*;
public class PsiSuperMethodImplUtil {
private static final Key<CachedValue<Map<MethodSignature, HierarchicalMethodSignature>>> SIGNATURES_KEY = Key.create("MAP_KEY");
private static final StructureDependentNotNullLazyKey<Map<MethodSignature, HierarchicalMethodSignature>, PsiClass> SIGNATURES_KEY = StructureDependentNotNullLazyKey.create("SIGNATURES_KEY", new NotNullFunction<PsiClass, Map<MethodSignature, HierarchicalMethodSignature>>() {
@NotNull
@Override
public Map<MethodSignature, HierarchicalMethodSignature> fun(PsiClass dom) {
return buildMethodHierarchy(dom, PsiSubstitutor.EMPTY, true, new THashSet<PsiClass>(), false);
}
});
private PsiSuperMethodImplUtil() {
}
@@ -298,30 +303,6 @@ public class PsiSuperMethodImplUtil {
}
private static Map<MethodSignature, HierarchicalMethodSignature> getSignaturesMap(final PsiClass aClass) {
CachedValue<Map<MethodSignature, HierarchicalMethodSignature>> value = aClass.getUserData(SIGNATURES_KEY);
if (value == null) {
BySignaturesCachedValueProvider provider = new BySignaturesCachedValueProvider(aClass);
UserDataHolderEx dataHolder = (UserDataHolderEx)aClass;
value = dataHolder.putUserDataIfAbsent(SIGNATURES_KEY,
CachedValuesManager.getManager(aClass.getProject()).createCachedValue(provider, false));
}
return value.getValue();
}
private static class BySignaturesCachedValueProvider implements CachedValueProvider<Map<MethodSignature, HierarchicalMethodSignature>> {
private final PsiClass myClass;
private BySignaturesCachedValueProvider(final PsiClass aClass) {
myClass = aClass;
}
public Result<Map<MethodSignature, HierarchicalMethodSignature>> compute() {
Map<MethodSignature, HierarchicalMethodSignature> result = buildMethodHierarchy(myClass, PsiSubstitutor.EMPTY, true, new THashSet<PsiClass>(), false);
assert result != null;
return new Result<Map<MethodSignature, HierarchicalMethodSignature>>(result, PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT);
}
return SIGNATURES_KEY.getValue(aClass);
}
}
@@ -0,0 +1,51 @@
/*
* Copyright 2000-2010 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.
*/
/*
* @author max
*/
package com.intellij.psi.util;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.PsiElement;
import com.intellij.util.NotNullFunction;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class StructureDependentNotNullLazyKey <T,H extends PsiElement> extends Key<Pair<Long, T>> {
private final NotNullFunction<H,T> myFunction;
private StructureDependentNotNullLazyKey(@NonNls String name, final NotNullFunction<H, T> function) {
super(name);
myFunction = function;
}
@NotNull
public final T getValue(H h) {
Pair<Long, T> data = h.getUserData(this);
final long count = h.getManager().getModificationTracker().getJavaStructureModificationCount();
if (data == null || data.getFirst() != count) {
h.putUserData(this, data = new Pair<Long, T>(count, myFunction.fun(h)));
}
return data.getSecond();
}
public static <T,H extends PsiElement> StructureDependentNotNullLazyKey<T,H> create(@NonNls String name, final NotNullFunction<H, T> function) {
return new StructureDependentNotNullLazyKey<T,H>(name, function);
}
}