Skip to content

Commit

Permalink
simplify and bulletproof cache synchronization; thanks to Sebastian B…
Browse files Browse the repository at this point in the history
…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 050ab0b
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions src/java/org/apache/commons/jxpath/FunctionLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 050ab0b

Please sign in to comment.