Skip to content
Permalink
Browse files
simplify and bulletproof cache synchronization; thanks to Sebastian B…
…azley

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/jxpath/trunk@1133414 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mbenson committed Jun 8, 2011
1 parent 0b52de7 commit 050ab0b02c54c0790a41aa9d815546235a2c4bfd
Showing 1 changed file with 18 additions and 23 deletions.
@@ -100,30 +100,25 @@ public Function getFunction(String namespace, String name,
/**
* Prepare the cache.
*/
private Map functionCache() {
private synchronized Map functionCache() {
if (byNamespace == null) {
synchronized (this) {
//read again
if (byNamespace == null) {
byNamespace = new HashMap();
int count = allFunctions.size();
for (int i = 0; i < count; i++) {
Functions funcs = (Functions) allFunctions.get(i);
Set namespaces = funcs.getUsedNamespaces();
for (Iterator it = namespaces.iterator(); it.hasNext();) {
String ns = (String) it.next();
Object candidates = byNamespace.get(ns);
if (candidates == null) {
byNamespace.put(ns, funcs);
} else if (candidates instanceof Functions) {
List lst = new ArrayList();
lst.add(candidates);
lst.add(funcs);
byNamespace.put(ns, lst);
} else {
((List) candidates).add(funcs);
}
}
byNamespace = new HashMap();
int count = allFunctions.size();
for (int i = 0; i < count; i++) {
Functions funcs = (Functions) allFunctions.get(i);
Set namespaces = funcs.getUsedNamespaces();
for (Iterator it = namespaces.iterator(); it.hasNext();) {
String ns = (String) it.next();
Object candidates = byNamespace.get(ns);
if (candidates == null) {
byNamespace.put(ns, funcs);
} else if (candidates instanceof Functions) {
List lst = new ArrayList();
lst.add(candidates);
lst.add(funcs);
byNamespace.put(ns, lst);
} else {
((List) candidates).add(funcs);
}
}
}

0 comments on commit 050ab0b

Please sign in to comment.