Use soft ref to list instead of list of soft refs to cleanup code

* See https://github.com/JetBrains/intellij-community/commit/2b5487ff1a457c519be8f6bb9775c85ffc03c4cf#commitcomment-21133757
This commit is contained in:
Ilya.Kazakevich
2017-03-03 17:43:11 +03:00
parent 00fd03b53a
commit 9347aa5829
@@ -33,7 +33,7 @@ import java.util.stream.Collectors;
* @author yole
*/
public abstract class PythonPathCache {
private final Map<QualifiedName, List<SoftReference<PsiElement>>> myCache = ContainerUtil.newConcurrentMap();
private final Map<QualifiedName, SoftReference<List<PsiElement>>> myCache = ContainerUtil.newConcurrentMap();
private final Map<String, List<QualifiedName>> myQNameCache = ContainerUtil.newConcurrentMap();
public void clearCache() {
@@ -46,21 +46,21 @@ public abstract class PythonPathCache {
*/
@Nullable
public List<PsiElement> get(@NotNull final QualifiedName qualifiedName) {
final List<SoftReference<PsiElement>> references = myCache.get(qualifiedName);
final SoftReference<List<PsiElement>> references = myCache.get(qualifiedName);
if (references == null) {
return null;
}
final List<PsiElement> result = references.stream().map(r -> r.get()).collect(Collectors.toList());
if (result.stream().anyMatch(o -> o == null || ! o.isValid())) {
// Null in list means SoftReference has been collected meaning cache is not valid
final List<PsiElement> elements = references.get();
if(elements != null && ! elements.stream().allMatch(PsiElement::isValid)) {
// At least one element is invalid
return null;
}
return result;
return elements;
}
public void put(QualifiedName qualifiedName, List<PsiElement> results) {
if (results != null) {
myCache.put(qualifiedName, new ArrayList<>(results.stream().map(e -> new SoftReference<>(e)).collect(Collectors.toList())));
myCache.put(qualifiedName, new SoftReference<>(results));
}
}