001/* 002 * Portions of this software was developed by employees of the National Institute 003 * of Standards and Technology (NIST), an agency of the Federal Government and is 004 * being made available as a public service. Pursuant to title 17 United States 005 * Code Section 105, works of NIST employees are not subject to copyright 006 * protection in the United States. This software may be subject to foreign 007 * copyright. Permission in the United States and in foreign countries, to the 008 * extent that NIST may hold copyright, to use, copy, modify, create derivative 009 * works, and distribute this software and its documentation without fee is hereby 010 * granted on a non-exclusive basis, provided that this notice and disclaimer 011 * of warranty appears in all copies. 012 * 013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER 014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY 015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF 016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM 017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE 018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT 019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, 020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, 021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, 022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR 023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT 024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. 025 */ 026 027package gov.nist.secauto.metaschema.core.metapath.function; 028 029import gov.nist.secauto.metaschema.core.metapath.IExpression; 030import gov.nist.secauto.metaschema.core.util.ObjectUtils; 031 032import java.util.HashMap; 033import java.util.List; 034import java.util.Map; 035import java.util.stream.Stream; 036 037import javax.xml.namespace.QName; 038 039import edu.umd.cs.findbugs.annotations.NonNull; 040import edu.umd.cs.findbugs.annotations.Nullable; 041 042public class FunctionLibrary implements IFunctionLibrary { 043 044 @NonNull 045 private final Map<QName, NamedFunctionSet> libraryByQName = new HashMap<>(); // NOPMD - intentional 046 @NonNull 047 private final Map<String, NamedFunctionSet> libraryByName = new HashMap<>(); // NOPMD - intentional 048 049 /** 050 * Register the provided function signature. 051 * 052 * @param function 053 * the function signature to register 054 * @throws IllegalArgumentException 055 * if the provided function has the same arity as a previously 056 * registered function with the same name 057 */ 058 public void registerFunction(@NonNull IFunction function) { 059 registerFunctionByQName(function); 060 registerFunctionByName(function); 061 } 062 063 protected void registerFunctionByQName(@NonNull IFunction function) { 064 QName qname = function.getQName(); 065 IFunction duplicate; 066 synchronized (this) { 067 NamedFunctionSet functions = libraryByQName.get(qname); 068 if (functions == null) { 069 functions = new NamedFunctionSet(); 070 libraryByQName.put(qname, functions); 071 } 072 duplicate = functions.addFunction(function); 073 } 074 if (duplicate != null) { 075 throw new IllegalArgumentException(String.format("Duplicate functions with same arity: %s shadows %s", 076 duplicate.toSignature(), function.toSignature())); 077 } 078 } 079 080 protected void registerFunctionByName(@NonNull IFunction function) { 081 String name = function.getName(); 082 synchronized (this) { 083 NamedFunctionSet functions = libraryByName.get(name); 084 if (functions == null) { 085 functions = new NamedFunctionSet(); 086 libraryByName.put(name, functions); 087 } 088 // replace duplicates 089 functions.addFunction(function); 090 } 091 } 092 093 @Override 094 public Stream<IFunction> getFunctionsAsStream() { 095 synchronized (this) { 096 return ObjectUtils.notNull( 097 libraryByQName.values().stream().flatMap(set -> { 098 return set.getFunctionsAsStream(); 099 })); 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}