Skip to content
Permalink
Browse files

8238648: Rename and simplify Utils.WeakSoftHashMap

Reviewed-by: hannesw
  • Loading branch information
jonathan-gibbons committed Feb 8, 2020
1 parent 214edaf commit c33107053b6bd22e16a86d25d414a92b496348ed
Showing with 48 additions and 87 deletions.
  1. +48 −87 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Utils.java
@@ -32,9 +32,28 @@
import java.text.Collator;
import java.text.ParseException;
import java.text.RuleBasedCollator;
import java.util.*;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayDeque;
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.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.stream.Collectors;

@@ -937,7 +956,7 @@ public TypeMirror getDeclaredType(TypeElement enclosing, TypeMirror target) {
* @return
*/
public TypeMirror getDeclaredType(Collection<TypeMirror> values,
TypeElement enclosing, TypeMirror target) {
TypeElement enclosing, TypeMirror target) {
TypeElement targetElement = asTypeElement(target);
List<? extends TypeParameterElement> targetTypeArgs = targetElement.getTypeParameters();
if (targetTypeArgs.isEmpty()) {
@@ -2546,7 +2565,7 @@ public long getLineNumber(Element 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) {
list.addAll(getItems0(e, filter, select));
List<Element> classes = getItems0(e, filter, nestedKinds);
@@ -2559,7 +2578,7 @@ void recursiveGetItems(Collection<Element> list, Element e, boolean filter, Elem
}

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);
}

@@ -3002,14 +3021,14 @@ private boolean isKind(DocTree doctree, DocTree.Kind match) {
return doctree.getKind() == match;
}

private final WeakSoftHashMap wksMap = new WeakSoftHashMap(this);
private final CommentHelperCache commentHelperCache = new CommentHelperCache(this);

public CommentHelper getCommentHelper(Element element) {
return wksMap.computeIfAbsent(element);
return commentHelperCache.computeIfAbsent(element);
}

public void removeCommentHelper(Element element) {
wksMap.remove(element);
commentHelperCache.remove(element);
}

public List<? extends DocTree> getBlockTags(Element element) {
@@ -3177,13 +3196,13 @@ boolean isValidDuo(DocCommentDuo duo) {
}

public DocCommentTree getDocCommentTree(Element element) {
CommentHelper ch = wksMap.get(element);
CommentHelper ch = commentHelperCache.get(element);
if (ch != null) {
return ch.dctree;
}
DocCommentTree dcTree = getDocCommentTree0(element);
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;
}
@@ -3297,106 +3316,48 @@ public TypeElement getTopMostContainingTypeElement(Element e) {
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;
public WeakSoftHashMap(Utils utils) {
wkMap = new WeakHashMap<>();
this.utils = utils;
}

@Override
public boolean containsKey(Object 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));
public CommentHelperCache(Utils utils) {
map = new HashMap<>();
this.utils = utils;
}

@Override
public CommentHelper remove(Object key) {
SoftReference<CommentHelper> value = wkMap.remove(key);
public CommentHelper remove(Element key) {
SoftReference<CommentHelper> value = map.remove(key);
return value == null ? null : value.get();
}


@Override
public CommentHelper put(Element key, CommentHelper value) {
SoftReference<CommentHelper> nvalue = wkMap.put(key, new SoftReference<>(value));
return nvalue == null ? null : nvalue.get();
SoftReference<CommentHelper> prev = map.put(key, new SoftReference<>(value));
return prev == null ? null : prev.get();
}

@Override
public CommentHelper get(Object key) {
SoftReference<CommentHelper> value = wkMap.get(key);
SoftReference<CommentHelper> value = map.get(key);
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) {
if (wkMap.containsKey(key)) {
SoftReference<CommentHelper> value = wkMap.get(key);
SoftReference<CommentHelper> refValue = map.get(key);
if (refValue != null) {
CommentHelper value = refValue.get();
if (value != null) {
CommentHelper cvalue = value.get();
if (cvalue != null) {
return cvalue;
}
return value;
}
}
CommentHelper newValue = new CommentHelper(utils.configuration, key, utils.getTreePath(key),
utils.getDocCommentTree(key));
wkMap.put(key, new SoftReference<>(newValue));
map.put(key, new SoftReference<>(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;
}
}

/**

0 comments on commit c331070

Please sign in to comment.