View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.metaschema.core.metapath.function;
28  
29  import gov.nist.secauto.metaschema.core.metapath.IExpression;
30  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
31  
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  import java.util.stream.Stream;
36  
37  import javax.xml.namespace.QName;
38  
39  import edu.umd.cs.findbugs.annotations.NonNull;
40  import edu.umd.cs.findbugs.annotations.Nullable;
41  
42  public class FunctionLibrary implements IFunctionLibrary {
43  
44    @NonNull
45    private final Map<QName, NamedFunctionSet> libraryByQName = new HashMap<>(); // NOPMD - intentional
46    @NonNull
47    private final Map<String, NamedFunctionSet> libraryByName = new HashMap<>(); // NOPMD - intentional
48  
49    /**
50     * Register the provided function signature.
51     *
52     * @param function
53     *          the function signature to register
54     * @throws IllegalArgumentException
55     *           if the provided function has the same arity as a previously
56     *           registered function with the same name
57     */
58    public void registerFunction(@NonNull IFunction function) {
59      registerFunctionByQName(function);
60      registerFunctionByName(function);
61    }
62  
63    protected void registerFunctionByQName(@NonNull IFunction function) {
64      QName qname = function.getQName();
65      IFunction duplicate;
66      synchronized (this) {
67        NamedFunctionSet functions = libraryByQName.get(qname);
68        if (functions == null) {
69          functions = new NamedFunctionSet();
70          libraryByQName.put(qname, functions);
71        }
72        duplicate = functions.addFunction(function);
73      }
74      if (duplicate != null) {
75        throw new IllegalArgumentException(String.format("Duplicate functions with same arity: %s shadows %s",
76            duplicate.toSignature(), function.toSignature()));
77      }
78    }
79  
80    protected void registerFunctionByName(@NonNull IFunction function) {
81      String name = function.getName();
82      synchronized (this) {
83        NamedFunctionSet functions = libraryByName.get(name);
84        if (functions == null) {
85          functions = new NamedFunctionSet();
86          libraryByName.put(name, functions);
87        }
88        // replace duplicates
89        functions.addFunction(function);
90      }
91    }
92  
93    @Override
94    public Stream<IFunction> getFunctionsAsStream() {
95      synchronized (this) {
96        return ObjectUtils.notNull(
97            libraryByQName.values().stream().flatMap(set -> {
98              return set.getFunctionsAsStream();
99            }));
100     }
101   }
102 
103   @Override
104   public IFunction getFunction(@NonNull String name, @NonNull List<IExpression> args) {
105     IFunction retval;
106     synchronized (this) {
107       NamedFunctionSet functions = libraryByName.get(name);
108       retval = functions == null ? null : functions.getFunctionWithArity(args.size());
109     }
110     return retval;
111   }
112 
113   @Override
114   public IFunction getFunction(@NonNull QName name, @NonNull List<IExpression> args) {
115     IFunction retval;
116     synchronized (this) {
117       NamedFunctionSet functions = libraryByQName.get(name);
118       retval = functions == null ? null : functions.getFunctionWithArity(args.size());
119     }
120     return retval;
121   }
122 
123   private static class NamedFunctionSet {
124     private final Map<Integer, IFunction> arityToFunctionMap;
125 
126     public NamedFunctionSet() {
127       this.arityToFunctionMap = new HashMap<>();
128     }
129 
130     @SuppressWarnings("null")
131     @NonNull
132     public Stream<IFunction> getFunctionsAsStream() {
133       return arityToFunctionMap.values().stream();
134     }
135 
136     @Nullable
137     public IFunction getFunctionWithArity(int arity) {
138       return arityToFunctionMap.get(arity);
139     }
140 
141     @Nullable
142     public IFunction addFunction(@NonNull IFunction function) {
143       return arityToFunctionMap.put(function.arity(), function);
144     }
145   }
146 }