8238648: Rename and simplify Utils.WeakSoftHashMap
Reviewed-by: hannesw
This commit is contained in:
parent
214edaf9c2
commit
c33107053b
@ -32,9 +32,28 @@ import java.text.CollationKey;
|
|||||||
import java.text.Collator;
|
import java.text.Collator;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.RuleBasedCollator;
|
import java.text.RuleBasedCollator;
|
||||||
import java.util.*;
|
import java.util.ArrayDeque;
|
||||||
import java.util.AbstractMap.SimpleEntry;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.EnumSet;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.SortedSet;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
import java.util.TreeSet;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -2546,7 +2565,7 @@ public class Utils {
|
|||||||
}.visit(e);
|
}.visit(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
EnumSet<ElementKind> nestedKinds = EnumSet.of(ANNOTATION_TYPE, CLASS, ENUM, INTERFACE);
|
Set<ElementKind> nestedKinds = EnumSet.of(ANNOTATION_TYPE, CLASS, ENUM, INTERFACE);
|
||||||
void recursiveGetItems(Collection<Element> list, Element e, boolean filter, ElementKind... select) {
|
void recursiveGetItems(Collection<Element> list, Element e, boolean filter, ElementKind... select) {
|
||||||
list.addAll(getItems0(e, filter, select));
|
list.addAll(getItems0(e, filter, select));
|
||||||
List<Element> classes = getItems0(e, filter, nestedKinds);
|
List<Element> classes = getItems0(e, filter, nestedKinds);
|
||||||
@ -2559,7 +2578,7 @@ public class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<Element> getItems0(Element te, boolean filter, ElementKind... select) {
|
private List<Element> getItems0(Element te, boolean filter, ElementKind... select) {
|
||||||
EnumSet<ElementKind> kinds = EnumSet.copyOf(Arrays.asList(select));
|
Set<ElementKind> kinds = EnumSet.copyOf(Arrays.asList(select));
|
||||||
return getItems0(te, filter, kinds);
|
return getItems0(te, filter, kinds);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3002,14 +3021,14 @@ public class Utils {
|
|||||||
return doctree.getKind() == match;
|
return doctree.getKind() == match;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final WeakSoftHashMap wksMap = new WeakSoftHashMap(this);
|
private final CommentHelperCache commentHelperCache = new CommentHelperCache(this);
|
||||||
|
|
||||||
public CommentHelper getCommentHelper(Element element) {
|
public CommentHelper getCommentHelper(Element element) {
|
||||||
return wksMap.computeIfAbsent(element);
|
return commentHelperCache.computeIfAbsent(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeCommentHelper(Element element) {
|
public void removeCommentHelper(Element element) {
|
||||||
wksMap.remove(element);
|
commentHelperCache.remove(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<? extends DocTree> getBlockTags(Element element) {
|
public List<? extends DocTree> getBlockTags(Element element) {
|
||||||
@ -3177,13 +3196,13 @@ public class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public DocCommentTree getDocCommentTree(Element element) {
|
public DocCommentTree getDocCommentTree(Element element) {
|
||||||
CommentHelper ch = wksMap.get(element);
|
CommentHelper ch = commentHelperCache.get(element);
|
||||||
if (ch != null) {
|
if (ch != null) {
|
||||||
return ch.dctree;
|
return ch.dctree;
|
||||||
}
|
}
|
||||||
DocCommentTree dcTree = getDocCommentTree0(element);
|
DocCommentTree dcTree = getDocCommentTree0(element);
|
||||||
if (dcTree != null) {
|
if (dcTree != null) {
|
||||||
wksMap.put(element, new CommentHelper(configuration, element, getTreePath(element), dcTree));
|
commentHelperCache.put(element, new CommentHelper(configuration, element, getTreePath(element), dcTree));
|
||||||
}
|
}
|
||||||
return dcTree;
|
return dcTree;
|
||||||
}
|
}
|
||||||
@ -3297,106 +3316,48 @@ public class Utils {
|
|||||||
return outer;
|
return outer;
|
||||||
}
|
}
|
||||||
|
|
||||||
static class WeakSoftHashMap implements Map<Element, CommentHelper> {
|
/**
|
||||||
|
* A memory-sensitive cache for {@link CommentHelper} objects,
|
||||||
|
* which are expensive to compute.
|
||||||
|
*/
|
||||||
|
private static class CommentHelperCache {
|
||||||
|
|
||||||
private final WeakHashMap<Element, SoftReference<CommentHelper>> wkMap;
|
private final Map<Element, SoftReference<CommentHelper>> map;
|
||||||
private final Utils utils;
|
private final Utils utils;
|
||||||
public WeakSoftHashMap(Utils utils) {
|
|
||||||
wkMap = new WeakHashMap<>();
|
public CommentHelperCache(Utils utils) {
|
||||||
|
map = new HashMap<>();
|
||||||
this.utils = utils;
|
this.utils = utils;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public CommentHelper remove(Element key) {
|
||||||
public boolean containsKey(Object key) {
|
SoftReference<CommentHelper> value = map.remove(key);
|
||||||
return wkMap.containsKey(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Collection<CommentHelper> values() {
|
|
||||||
Set<CommentHelper> out = new LinkedHashSet<>();
|
|
||||||
for (SoftReference<CommentHelper> v : wkMap.values()) {
|
|
||||||
out.add(v.get());
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean containsValue(Object value) {
|
|
||||||
return wkMap.containsValue(new SoftReference<>((CommentHelper)value));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public CommentHelper remove(Object key) {
|
|
||||||
SoftReference<CommentHelper> value = wkMap.remove(key);
|
|
||||||
return value == null ? null : value.get();
|
return value == null ? null : value.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public CommentHelper put(Element key, CommentHelper value) {
|
public CommentHelper put(Element key, CommentHelper value) {
|
||||||
SoftReference<CommentHelper> nvalue = wkMap.put(key, new SoftReference<>(value));
|
SoftReference<CommentHelper> prev = map.put(key, new SoftReference<>(value));
|
||||||
return nvalue == null ? null : nvalue.get();
|
return prev == null ? null : prev.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public CommentHelper get(Object key) {
|
public CommentHelper get(Object key) {
|
||||||
SoftReference<CommentHelper> value = wkMap.get(key);
|
SoftReference<CommentHelper> value = map.get(key);
|
||||||
return value == null ? null : value.get();
|
return value == null ? null : value.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int size() {
|
|
||||||
return wkMap.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isEmpty() {
|
|
||||||
return wkMap.isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void clear() {
|
|
||||||
wkMap.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommentHelper computeIfAbsent(Element key) {
|
public CommentHelper computeIfAbsent(Element key) {
|
||||||
if (wkMap.containsKey(key)) {
|
SoftReference<CommentHelper> refValue = map.get(key);
|
||||||
SoftReference<CommentHelper> value = wkMap.get(key);
|
if (refValue != null) {
|
||||||
|
CommentHelper value = refValue.get();
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
CommentHelper cvalue = value.get();
|
return value;
|
||||||
if (cvalue != null) {
|
|
||||||
return cvalue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CommentHelper newValue = new CommentHelper(utils.configuration, key, utils.getTreePath(key),
|
CommentHelper newValue = new CommentHelper(utils.configuration, key, utils.getTreePath(key),
|
||||||
utils.getDocCommentTree(key));
|
utils.getDocCommentTree(key));
|
||||||
wkMap.put(key, new SoftReference<>(newValue));
|
map.put(key, new SoftReference<>(newValue));
|
||||||
return newValue;
|
return newValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void putAll(Map<? extends Element, ? extends CommentHelper> map) {
|
|
||||||
for (Map.Entry<? extends Element, ? extends CommentHelper> entry : map.entrySet()) {
|
|
||||||
put(entry.getKey(), entry.getValue());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<Element> keySet() {
|
|
||||||
return wkMap.keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<Entry<Element, CommentHelper>> entrySet() {
|
|
||||||
Set<Entry<Element, CommentHelper>> out = new LinkedHashSet<>();
|
|
||||||
for (Element e : wkMap.keySet()) {
|
|
||||||
SimpleEntry<Element, CommentHelper> n = new SimpleEntry<>(e, get(e));
|
|
||||||
out.add(n);
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user