Make 2 flavors of parametrized cache: nullable and notnull

This commit is contained in:
Ilya.Kazakevich
2017-09-12 18:29:36 +03:00
parent e791557ca1
commit 56d02ca28a
3 changed files with 14 additions and 4 deletions
@@ -843,7 +843,17 @@ public class PyUtil {
});
}
public static <T, P> T getParameterizedCachedValue(@NotNull PsiElement element, @Nullable P param, @NotNull NullableFunction<P, T> f) {
@NotNull
public static <T, P> T getParameterizedCachedValue(@NotNull PsiElement element, @Nullable P param, @NotNull NotNullFunction<P, T> f) {
final T result = getNullableParameterizedCachedValue(element, param, f);
assert result != null;
return result;
}
@Nullable
public static <T, P> T getNullableParameterizedCachedValue(@NotNull PsiElement element,
@Nullable P param,
@NotNull NullableFunction<P, T> f) {
final CachedValuesManager manager = CachedValuesManager.getManager(element.getProject());
final Map<Optional<P>, Optional<T>> cache = CachedValuesManager.getCachedValue(element, manager.getKeyForClass(f.getClass()), () -> {
// concurrent hash map is a null-hostile collection
@@ -899,7 +909,7 @@ public class PyUtil {
/**
* Returns the line comment that immediately precedes statement list of the given compound statement. Python parser ensures
* that it follows the statement header, i.e. it's directly after the colon, not on its own line.
* that it follows the statement header, i.e. it's directly after the colon, not on its own line.
*/
@Nullable
public static PsiComment getCommentOnHeaderLine(@NotNull PyStatementListContainer container) {
@@ -79,7 +79,7 @@ public class PyClassImpl extends PyBaseElementImpl<PyClassStub> implements PyCla
/**
* Engine to create list of ancestors based on context
*/
private final class CachedAncestorsProvider implements NullableFunction<TypeEvalContext, List<PyClassLikeType>> {
private final class CachedAncestorsProvider implements NotNullFunction<TypeEvalContext, List<PyClassLikeType>> {
@NotNull
@Override
public List<PyClassLikeType> fun(@NotNull TypeEvalContext context) {
@@ -115,7 +115,7 @@ public class QualifiedNameFinder {
*/
@Nullable
public static QualifiedName findCanonicalImportPath(@NotNull PsiElement symbol, @Nullable PsiElement foothold) {
return PyUtil.getParameterizedCachedValue(symbol, Couple.of(symbol, foothold), QualifiedNameFinder::doFindCanonicalImportPath);
return PyUtil.getNullableParameterizedCachedValue(symbol, Couple.of(symbol, foothold), QualifiedNameFinder::doFindCanonicalImportPath);
}
@Nullable