/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.jxpath;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * An object that aggregates {@link Functions} objects into a group Functions
 * object. Since {@link JXPathContext} can only register a single Functions
 * object, FunctionLibrary should always be used to group all Functions objects
 * that need to be registered.
 *
 * @author Dmitri Plotnikov
 * @version $Revision$ $Date$
 */
public class FunctionLibrary implements Functions {
    private final List allFunctions = new ArrayList();
    private Map byNamespace;

    /**
     * Add functions to the library
     * @param functions to add
     */
    public void addFunctions(Functions functions) {
        allFunctions.add(functions);
        byNamespace = null;
    }

    /**
     * Remove functions from the library.
     * @param functions to remove
     */
    public void removeFunctions(Functions functions) {
        allFunctions.remove(functions);
        byNamespace = null;
    }

    /**
     * Returns a set containing all namespaces used by the aggregated
     * Functions.
     * @return Set<String>
     */
    public Set getUsedNamespaces() {
        return functionCache().keySet();
    }

    /**
     * Returns a Function, if any, for the specified namespace,
     * name and parameter types.
     * @param namespace function namespace
     * @param name function name
     * @param parameters parameters
     * @return Function found
     */
    public Function getFunction(String namespace, String name,
            Object[] parameters) {
        Object candidates = functionCache().get(namespace);
        if (candidates instanceof Functions) {
            return ((Functions) candidates).getFunction(
                namespace,
                name,
                parameters);
        }
        if (candidates instanceof List) {
            List list = (List) candidates;
            int count = list.size();
            for (int i = 0; i < count; i++) {
                Function function =
                    ((Functions) list.get(i)).getFunction(
                        namespace,
                        name,
                        parameters);
                if (function != null) {
                    return function;
                }
            }
        }
        return null;
    }

    /**
     * Prepare the cache.
     */
    private 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);
                            }
                        }
                    }
                }
            }
        }
        return byNamespace;
    }
}

@@ -3263,24 +3263,37 @@
     private 
+synchronized 
 Map function
@@ -3341,122 +3341,8 @@
 ) %7B%0A
-            synchronized (this) %7B%0A                //read again%0A                if (byNamespace == null) %7B%0A        
     
@@ -3374,32 +3374,24 @@
  HashMap();%0A
-        
             
@@ -3431,32 +3431,24 @@
             
-  
-      
 for (int i =
@@ -3465,32 +3465,24 @@
 unt; i++) %7B%0A
-        
             
@@ -3552,24 +3552,16 @@
         
-     
-   
 Set name
@@ -3608,32 +3608,24 @@
             
-       
- 
 for (Iterato
@@ -3691,24 +3691,16 @@
         
-        
 String n
@@ -3739,32 +3739,24 @@
             
- 
-       
 Object candi
@@ -3800,32 +3800,24 @@
             
-        
 if (candidat
@@ -3850,32 +3850,24 @@
             
-    
-    
 byNamespace.
@@ -3878,32 +3878,24 @@
 ns, funcs);%0A
-        
             
@@ -3944,32 +3944,24 @@
 unctions) %7B%0A
-        
             
@@ -4020,32 +4020,24 @@
             
-        
 lst.add(cand
@@ -4065,32 +4065,24 @@
             
-        
 lst.add(func
@@ -4105,32 +4105,24 @@
             
-   
-     
 byNamespace.
@@ -4151,32 +4151,24 @@
             
-        
 %7D else %7B%0A   
@@ -4188,24 +4188,16 @@
         
-       
- 
 ((List) 
@@ -4224,64 +4224,8 @@
 s);%0A
-                            %7D%0A                        %7D%0A
     

simplify and bulletproof cache synchronization; thanks to Sebastian Bazley